forked from arpitbbhayani/spoj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CAM5.cpp
85 lines (54 loc) · 1.17 KB
/
CAM5.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
#include <iostream>
#include <vector>
#include <queue>
#include <list>
using namespace std;
int main ( int argc , char * argv[] ) {
int t;
cin >> t;
while ( t-- ) {
int n;
cin >> n;
list<int> node[n];
int visited[n];
int p;
cin >> p;
for ( int i = 0 ; i < n ; i++ ) {
visited[i] = 0;
/*cout << "Node : " << i << " -> ";
for ( vector<int>::iterator itr = node[i].begin() ; itr != node[i].end() ; itr++ ) {
cout << *itr << " ";
}
cout << endl;
*/
}
while ( p-- ) {
int a , b;
cin >> a >> b;
node[a].push_back(b);
node[b].push_back(a);
}
queue<int> q;
int count = 0;
for ( int i = 0 ; i < n ; i++ ) {
if ( visited[i] == 1 )
continue;
visited[i] = 1;
q.push(i);
while ( !q.empty() ) {
int t = q.front();
q.pop();
for ( list<int>::iterator itr = node[t].begin() ; itr != node[t].end() ; itr++ ) {
int child = *itr;
if ( visited[child] == 1 )
continue;
visited[child] = 1;
q.push(child);
}
}
count ++;
}
cout << count << endl;
}
return 0;
}