#include <bits/stdc++.h>
using namespace std;
vector<int> rep;
vector<set<int>> repgraph;
vector<set<int>> revgraph;
vector<long long> rsize;
vector<vector<int>> rin;
long long sol = 0;
int finde(int i)
{
if (i != rep[i]) rep[i] = finde(rep[i]);
return rep[i];
}
int main()
{
int n, m;
cin >> n >> m;
rep.resize(n);
repgraph.assign(n, set<int>());
revgraph.assign(n, set<int>());
rin.assign(n, vector<int>());
rsize.assign(n, 1);
iota(rep.begin(), rep.end(), 0);
for (int i = 0; i < n; i++)
{
rin[i].push_back(i);
}
for (int i = 0; i < m; i++)
{
int a, b;
cin >> a >> b;
a--; b--;
if (finde(a)==finde(b))
{
cout << sol << "\n";
continue;
}
if (repgraph[finde(b)].find(finde(a))==repgraph[finde(b)].end())
{
//a is not contained in graph from b;
//connect a to all nodes in b
sol -= rsize[finde(b)]*revgraph[finde(b)].size();
repgraph[finde(a)].insert(finde(b));
revgraph[finde(b)].insert(a);
sol += rsize[finde(b)]*revgraph[finde(b)].size();
cout << sol << "\n";
continue;
}
queue<int> q;
q.push(a);
while (q.size())
{
a = q.front();
q.pop();
if (finde(a)==finde(b)) continue;
repgraph[finde(b)].erase(finde(a));
if (repgraph[finde(a)].size()+rin[finde(a)].size()+revgraph[finde(a)].size()>repgraph[finde(b)].size()+rin[finde(b)].size()+revgraph[finde(b)].size())
{
swap(a, b);
}
sol -= revgraph[finde(a)].size()*rsize[finde(a)];
sol -= revgraph[finde(b)].size()*rsize[finde(b)];
for (int ele:repgraph[finde(a)])
{
if(finde(ele)==finde(b)) continue;
if (repgraph[finde(ele)].find(finde(b))!= repgraph[finde(ele)].end())
{
repgraph[finde(b)].erase(finde(ele));
q.push(ele);
}
repgraph[finde(b)].insert(finde(ele));
}
if (rin[finde(a)].size()+revgraph[finde(a)].size() > rin[finde(b)].size()+revgraph[finde(b)].size())
{
//repgraph[finde(a)].swap(repgraph[finde(b)]);
//swap(a, b);
}
for (int ele:rin[finde(a)])
{
rin[finde(b)].push_back(ele);
revgraph[finde(b)].erase(ele);
}
for (int ele:revgraph[finde(a)])
{
repgraph[finde(ele)].erase(finde(a));
if (repgraph[finde(b)].find(finde(ele))!= repgraph[finde(b)].end())
{
repgraph[finde(b)].erase(finde(ele));
q.push(ele);
}
else if (finde(ele) != finde(b) && finde(ele)!=finde(a))
{
repgraph[finde(ele)].insert(finde(b));
revgraph[finde(b)].insert(ele);
}
}
sol += rsize[finde(a)]*rsize[finde(b)]*2;
int sum = rsize[finde(a)]+rsize[finde(b)];
rsize[finde(a)] = sum;
rsize[finde(b)] = sum;
rep[finde(a)] = rep[finde(b)];
sol += revgraph[finde(b)].size()*rsize[finde(b)];
}
cout << sol << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
4 ms |
480 KB |
Output is correct |
8 |
Correct |
4 ms |
348 KB |
Output is correct |
9 |
Correct |
4 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
3 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
3 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
3 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
3 ms |
440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
4 ms |
480 KB |
Output is correct |
8 |
Correct |
4 ms |
348 KB |
Output is correct |
9 |
Correct |
4 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
3 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
3 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
3 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
3 ms |
440 KB |
Output is correct |
34 |
Correct |
16 ms |
348 KB |
Output is correct |
35 |
Correct |
398 ms |
5848 KB |
Output is correct |
36 |
Correct |
442 ms |
8044 KB |
Output is correct |
37 |
Correct |
472 ms |
7936 KB |
Output is correct |
38 |
Correct |
449 ms |
7836 KB |
Output is correct |
39 |
Correct |
7 ms |
860 KB |
Output is correct |
40 |
Correct |
8 ms |
924 KB |
Output is correct |
41 |
Correct |
12 ms |
860 KB |
Output is correct |
42 |
Correct |
7 ms |
856 KB |
Output is correct |
43 |
Correct |
8 ms |
860 KB |
Output is correct |
44 |
Correct |
8 ms |
788 KB |
Output is correct |
45 |
Correct |
7 ms |
1112 KB |
Output is correct |
46 |
Correct |
7 ms |
1108 KB |
Output is correct |
47 |
Correct |
8 ms |
856 KB |
Output is correct |
48 |
Correct |
8 ms |
932 KB |
Output is correct |
49 |
Correct |
20 ms |
1368 KB |
Output is correct |
50 |
Correct |
416 ms |
8056 KB |
Output is correct |
51 |
Correct |
20 ms |
1112 KB |
Output is correct |
52 |
Correct |
409 ms |
7000 KB |
Output is correct |
53 |
Correct |
20 ms |
1368 KB |
Output is correct |
54 |
Correct |
412 ms |
7572 KB |
Output is correct |
55 |
Correct |
9 ms |
1116 KB |
Output is correct |
56 |
Correct |
9 ms |
1116 KB |
Output is correct |
57 |
Correct |
11 ms |
1036 KB |
Output is correct |
58 |
Correct |
8 ms |
1112 KB |
Output is correct |
59 |
Correct |
8 ms |
860 KB |
Output is correct |
60 |
Correct |
412 ms |
5756 KB |
Output is correct |
61 |
Correct |
9 ms |
856 KB |
Output is correct |
62 |
Correct |
446 ms |
7536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
4 ms |
480 KB |
Output is correct |
8 |
Correct |
4 ms |
348 KB |
Output is correct |
9 |
Correct |
4 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
3 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
3 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
3 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
3 ms |
440 KB |
Output is correct |
34 |
Correct |
16 ms |
348 KB |
Output is correct |
35 |
Correct |
398 ms |
5848 KB |
Output is correct |
36 |
Correct |
442 ms |
8044 KB |
Output is correct |
37 |
Correct |
472 ms |
7936 KB |
Output is correct |
38 |
Correct |
449 ms |
7836 KB |
Output is correct |
39 |
Correct |
7 ms |
860 KB |
Output is correct |
40 |
Correct |
8 ms |
924 KB |
Output is correct |
41 |
Correct |
12 ms |
860 KB |
Output is correct |
42 |
Correct |
7 ms |
856 KB |
Output is correct |
43 |
Correct |
8 ms |
860 KB |
Output is correct |
44 |
Correct |
8 ms |
788 KB |
Output is correct |
45 |
Correct |
7 ms |
1112 KB |
Output is correct |
46 |
Correct |
7 ms |
1108 KB |
Output is correct |
47 |
Correct |
8 ms |
856 KB |
Output is correct |
48 |
Correct |
8 ms |
932 KB |
Output is correct |
49 |
Correct |
20 ms |
1368 KB |
Output is correct |
50 |
Correct |
416 ms |
8056 KB |
Output is correct |
51 |
Correct |
20 ms |
1112 KB |
Output is correct |
52 |
Correct |
409 ms |
7000 KB |
Output is correct |
53 |
Correct |
20 ms |
1368 KB |
Output is correct |
54 |
Correct |
412 ms |
7572 KB |
Output is correct |
55 |
Correct |
9 ms |
1116 KB |
Output is correct |
56 |
Correct |
9 ms |
1116 KB |
Output is correct |
57 |
Correct |
11 ms |
1036 KB |
Output is correct |
58 |
Correct |
8 ms |
1112 KB |
Output is correct |
59 |
Correct |
8 ms |
860 KB |
Output is correct |
60 |
Correct |
412 ms |
5756 KB |
Output is correct |
61 |
Correct |
9 ms |
856 KB |
Output is correct |
62 |
Correct |
446 ms |
7536 KB |
Output is correct |
63 |
Correct |
655 ms |
50100 KB |
Output is correct |
64 |
Correct |
604 ms |
50000 KB |
Output is correct |
65 |
Correct |
673 ms |
50092 KB |
Output is correct |
66 |
Correct |
376 ms |
25900 KB |
Output is correct |
67 |
Correct |
427 ms |
29116 KB |
Output is correct |
68 |
Correct |
391 ms |
25940 KB |
Output is correct |
69 |
Correct |
535 ms |
26572 KB |
Output is correct |
70 |
Correct |
394 ms |
25952 KB |
Output is correct |
71 |
Correct |
391 ms |
25944 KB |
Output is correct |
72 |
Correct |
472 ms |
27624 KB |
Output is correct |
73 |
Correct |
549 ms |
27852 KB |
Output is correct |
74 |
Correct |
956 ms |
45720 KB |
Output is correct |
75 |
Correct |
907 ms |
32236 KB |
Output is correct |
76 |
Correct |
762 ms |
38348 KB |
Output is correct |
77 |
Correct |
723 ms |
38552 KB |
Output is correct |
78 |
Correct |
350 ms |
26832 KB |
Output is correct |
79 |
Correct |
640 ms |
31176 KB |
Output is correct |
80 |
Correct |
357 ms |
28112 KB |
Output is correct |
81 |
Correct |
602 ms |
31436 KB |
Output is correct |
82 |
Correct |
575 ms |
40696 KB |
Output is correct |
83 |
Correct |
548 ms |
40888 KB |
Output is correct |
84 |
Correct |
563 ms |
40280 KB |
Output is correct |
85 |
Correct |
587 ms |
40348 KB |
Output is correct |
86 |
Correct |
377 ms |
25764 KB |
Output is correct |
87 |
Correct |
572 ms |
27996 KB |
Output is correct |
88 |
Correct |
428 ms |
27596 KB |
Output is correct |
89 |
Correct |
692 ms |
37412 KB |
Output is correct |