-
Notifications
You must be signed in to change notification settings - Fork 1
/
CHUNK2.cpp
84 lines (71 loc) · 1.21 KB
/
CHUNK2.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
#include <bits/stdc++.h>
using namespace std;
int DFSAssign(vector<int> &tree, vector<vector<int> > &adj, int ind)
{
int count = 1;
tree[ind] = 1;
for(int i = 0; i < adj[ind].size(); ++i)
{
if(tree[adj[ind][i]] == 0)
{
count += DFSAssign(tree, adj,adj[ind][i]);
}
}
return count;
}
int main()
{
int prime[100001];
int p[1500000], n = 1500000;
for(int i = 2; i < n; ++i)
{
p[i] = 0;
}
p[0] = 1; p[1] = 1;
for(int i = 0; i < n/2; ++i)
{
if(p[i] == 0)
{
for(int j = i*2; j < n ; j += i)
{
p[j] = 1;
}
}
}
for(int i = 0, j = 1; i < n && j < 100001; ++i)
{
if(p[i] == 0)
{
prime[j] = i;
++j;
}
}
int t;
cin>>t;
while(t--)
{
int n,m, a, b;
cin>>n>>m;
vector<vector<int> > adj(n);
for(int i = 0; i < m; ++i)
{
cin>>a>>b;
a--; b--;
adj[a].push_back(b);
adj[b].push_back(a);
}
vector<int> tree(n);
for(int i = 0; i < n; ++i) tree[i] = 0;
int treenum = 1;
int last=0, maxi = 0, cnt;
while(last < n)
{
for(; (tree[last] != 0) && (last < n); last++);
if(last == n) break;
cnt = DFSAssign(tree, adj, last);
if(cnt > maxi) maxi = cnt;
}
if(maxi == 1) cout<<-1<<endl;
else cout<<prime[maxi]<<endl;
}
}