-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodemonkc.cpp
86 lines (74 loc) · 975 Bytes
/
codemonkc.cpp
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <bits/stdc++.h>
using namespace std;
int c;int mini;
int *visited;
class Graph
{
int v;
vector <int> *adj;
public:
Graph(int v);
void addedge (int v,int w);
void dfs(int s);
};
Graph :: Graph(int v)
{
this->v=v;
adj=new vector <int> [v+1];
}
void Graph :: addedge(int v,int w)
{
adj[v].push_back(w);
}
void Graph :: dfs(int s)
{
stack <int> q;
q.push(s);
visited[s]=1;
while(!q.empty())
{
int p = q.top();
q.pop();
for(int i=0;i<adj[p].size();i++)
{
if(visited[adj[p][i]]==0)
{
++c;
visited[adj[p][i]]=1;
q.push(adj[p][i]);
}
}
}
if(c<mini)
mini=c;
}
int main()
{
int n,d,x,y;
cin>>n>>d;
Graph g(n);
while(d--)
{
cin>>x>>y;
g.addedge(x,y);
}
c=1;mini=100000;
visited = new int [n+1];
for(int i=1;i<=n;i++)
{
for(int j=0;j<=n;j++)
visited[j]=0;
if(visited[i]==0)
{
g.dfs(i);
}
}
if(mini==0)
cout<<mini+1;
else
if(mini==n)
cout<<"1";
else
cout<<mini;
return 0;
}