-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11504.cpp
More file actions
101 lines (91 loc) · 1.43 KB
/
11504.cpp
File metadata and controls
101 lines (91 loc) · 1.43 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include<bits/stdc++.h>
using namespace std;
/*
* This does not work while sorting why?!
struct node{
int x;
int f;
node(int a,int b){
x = a;
f = b;
}
bool operator < (const node& p) const{
return this->f > p.f;
}
};
*/
map<int,bool>visited;
map<int,vector<int> > adj;
stack<int>topoVertices;
//int t = 0;
int cnt = 0;
inline void clear(){
cnt = 0;
adj.clear();
//t = 0;
}
inline void dfs(int x){
visited[x] = true;
//t += 1;
for(auto i:adj[x]){
if(visited[i] == false){
dfs(i);
}
}
visited[x] = true;
//t += 1;
topoVertices.push(x);
}
inline void dfs1(int x){
visited[x] = true;
for(auto i:adj[x]){
if(visited[i] == false){
dfs(i);
}
}
visited[x] = true;
}
int main(){
int tt;
cin>>tt;
int nodes,edges;
while(tt--){
cin>>nodes>>edges;
for(int i = 1;i<=nodes;i++){
visited[i] = false;
}
while(edges--){
int x,y;
cin>>x>>y;
adj[x].push_back(y); /**This is the break through I was hoping for**/
}
for(int i=1;i<=nodes;i++){
if(visited[i] == false){
dfs(i);
}
}
/*sort(topoVertices.begin(),topoVertices.end());*/
for(int i = 1;i<=nodes;i++){
visited[i] = false;
}
/*
for(auto j : topoVertices){
int i = j.x;
if(visited[i] == false){
cnt++;
dfs1(i);
}
}
*/
while(!topoVertices.empty()){
int i = topoVertices.top();
topoVertices.pop();
if(visited[i] == false){
cnt++;
dfs(i);
}
}
cout<<cnt<<endl;
clear();
}
}