-
Notifications
You must be signed in to change notification settings - Fork 1
/
连通图.cpp
77 lines (66 loc) · 1.11 KB
/
连通图.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<iostream>
#include<cstdlib>
#include<algorithm>
using namespace std;
int main()
{
int solve();
int m;
cin >> m;
while (m--)
{
solve();
}
}
int solve()
{
bool mysort(int a, int b);
int *vertex, *result;
int n_ver, n_edge;
cin >> n_ver >> n_edge;
vertex = new int[n_ver];
result = new int[n_ver];
for (int i0 = 0; i0 < n_ver; i0++)
{
vertex[i0] = i0;
result[i0] = 0;
}
int tem1, tem2, ori, dst;
for (int i1 = 0; i1<n_edge; i1++)
{
cin >> tem1 >> tem2;
ori = vertex[tem1 - 1];
dst = vertex[tem2 - 1];
for (int i2 = 0; i2 < n_ver; i2++)
{
if (vertex[i2] == ori)
vertex[i2] = dst;
}
}
sort(vertex,vertex+n_ver,mysort);
int n_res = 0, last = vertex[0], pos = 0;
for (int i3 = 0; i3 < n_ver+1; i3++){
if (i3 == n_ver){
result[pos++] = n_res;
break;
}
if (last == vertex[i3]){
n_res++;
}
else{
result[pos++] = n_res;
last = vertex[i3];
n_res = 1;
}
}
cout<<pos<<endl;
sort(result,result+pos,mysort);
for (int i4 = 0; i4 < pos; i4++){
cout << result[i4] << ' ';
}
cout << endl;
return 0;
}
bool mysort(int a, int b){
return a < b;
}