-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmbt.cpp
More file actions
executable file
·59 lines (50 loc) · 912 Bytes
/
mbt.cpp
File metadata and controls
executable file
·59 lines (50 loc) · 912 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// CODE ONLY WORKS IF THE GIVEN EDGES ARE NOT REPEATED
#include <bits/stdc++.h>
#define ll long long int
#define vi vector<int>
#define ii pair<int, int>
using namespace std;
bool visited[1001];
vi adj[1001];
ll dfs(ll u)
{
ll ans=0;
stack<ll> s;
s.push(u);
visited[u]=true;
while(!s.empty())
{
ll v = s.top();
s.pop();
for(ll j=0; j<(ll)adj[v].size(); j++)
{
if(!visited[adj[v][j]])
s.push(adj[v][j]);
}
//cout<<"added for "<<v<<"\n";
ans++;
visited[v]=true;
}
//cout<<"ans for this iteration is "<<ans<<"\n";
return ans;
}
int main(void){
ll n, d, ans=INT_MAX, cur_count;
cin>>n>>d;
//memset(adj, 0, sizeof(adj));
for(int k=0; k<d; k++)
{
ll u, v;
cin>>u>>v;
adj[u].push_back(v);
}
for(int node=1; node<=n; node++)
{
memset(visited, 0, sizeof(visited));
cur_count=dfs(node);
if(ans>cur_count)
ans=cur_count;
}
cout<<ans<<"\n";
return 0;
}