-
Notifications
You must be signed in to change notification settings - Fork 0
/
bfsdfs - A.cpp
77 lines (71 loc) · 1.42 KB
/
bfsdfs - A.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
#include<bits/stdc++.h>
using namespace std;
#define sz 20010
set<int> allInput, s1, s2;
vector<int> adjList[sz];
int a, b;
int test;
set<int>::iterator it;
bool visited[sz];
int n;
int ans;
int cnt;
void dfs(int node, bool inSet1)
{
//cout << "Node: " << node << endl;
if(visited[node])
{
return;
}
visited[node] = true;
if(inSet1)
{
s1.insert(node);
}
else
{
s2.insert(node);
}
int lim = adjList[node].size();
for(int i=0; i<lim; i++)
{
dfs(adjList[node][i], !inSet1);
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> test;
while(test--)
{
cin >> n;
for(int i=0; i<n; i++)
{
cin >> a >> b;
adjList[a].push_back(b);
adjList[b].push_back(a);
allInput.insert(a);
allInput.insert(b);
}
for(int i=0; i<sz; i++)
{
visited[i] = false;
}
//memset(visited, false, sizeof(visited)*sz);
for(it = allInput.begin(); it!=allInput.end(); it++)
{
dfs(*it, true);
}
ans = max(s1.size(), s2.size());
cout << "Case " << ++cnt << ": " <<ans << "\n";
for(it = allInput.begin(); it!=allInput.end(); it++)
{
adjList[*it].clear();
}
allInput.clear();
s1.clear();
s2.clear();
}
return 0;
}