#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int Nmax = 1e5 + 5;
set<int> in[Nmax], cIn[Nmax], cOut[Nmax];
bool dead[Nmax];
vector<int> who[Nmax];
int n, m;
int where[Nmax];
ll ans = 0;
ll take2(int x)
{
return (ll) x * (x-1);
}
void unite(int x, int y)
{
if(x == y) return;
if(dead[x] || dead[y]) return;
if(cIn[x].find(y) == cIn[x].end() || cOut[x].find(y) == cOut[x].end()) return;
if(who[x].size() + in[x].size() + cIn[x].size() + cOut[x].size() < who[y].size() + in[y].size() + cIn[y].size())
swap(x, y);
ans -= take2(who[x].size());
ans -= take2(who[y].size());
ans += take2(who[x].size() + who[y].size());
ans -= (ll) who[x].size() * in[x].size();
ans -= (ll) who[y].size() * in[y].size();
// for(auto it : in[x]) cerr << it << ' '; cerr << "#\n";
// for(auto it : in[y]) cerr << it << ' '; cerr << "#\n";
for(auto it : who[y])
if(in[x].find(it) != in[x].end())
in[x].erase(it);
for(auto it : in[y])
if(where[it] != x)
in[x].insert(it);
for(auto it : who[y])
{
who[x].push_back(it);
where[it] = x;
}
for(auto it : cOut[y])
if(it != x)
{
cIn[it].erase(y);
cIn[it].insert(x);
cOut[x].insert(it);
}
for(auto it : cIn[y])
if(it != x)
{
cOut[it].erase(y);
cOut[it].insert(x);
cIn[x].insert(it);
}
ans += (ll) who[x].size() * in[x].size();
dead[y] = 1;
for(auto it : cIn[y])
unite(x, it);
for(auto it : cOut[y])
unite(x, it);
who[y].clear(); in[y].clear(); cIn[y].clear(); cOut[y].clear();
}
void compute(int X, int Y)
{
int x, y;
x = where[X];
y = where[Y];
if(x == y) return;
auto itt = in[y].insert(X);
if(itt.second)
{
ans += who[y].size();
cIn[y].insert(x);
cOut[x].insert(y);
}
unite(x, y);
}
int main()
{
// freopen("input", "r", stdin);
cin.sync_with_stdio(false); cin.tie(0);
cin >> n >> m;
for(int i=1; i<=n; ++i)
{
where[i] = i;
who[i].push_back(i);
}
for(int i=1; i<=m; ++i)
{
int x, y;
cin >> x >> y;
compute(x, y);
cout << ans << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
16768 KB |
Output is correct |
2 |
Correct |
14 ms |
16768 KB |
Output is correct |
3 |
Correct |
14 ms |
16768 KB |
Output is correct |
4 |
Correct |
14 ms |
16768 KB |
Output is correct |
5 |
Correct |
14 ms |
16768 KB |
Output is correct |
6 |
Correct |
14 ms |
16768 KB |
Output is correct |
7 |
Correct |
15 ms |
16768 KB |
Output is correct |
8 |
Correct |
15 ms |
16768 KB |
Output is correct |
9 |
Correct |
19 ms |
16896 KB |
Output is correct |
10 |
Correct |
14 ms |
16768 KB |
Output is correct |
11 |
Correct |
15 ms |
16768 KB |
Output is correct |
12 |
Correct |
14 ms |
16768 KB |
Output is correct |
13 |
Correct |
14 ms |
16768 KB |
Output is correct |
14 |
Correct |
14 ms |
16768 KB |
Output is correct |
15 |
Correct |
15 ms |
16768 KB |
Output is correct |
16 |
Correct |
15 ms |
16768 KB |
Output is correct |
17 |
Correct |
14 ms |
16768 KB |
Output is correct |
18 |
Correct |
14 ms |
16768 KB |
Output is correct |
19 |
Correct |
15 ms |
16768 KB |
Output is correct |
20 |
Correct |
15 ms |
16768 KB |
Output is correct |
21 |
Correct |
15 ms |
16896 KB |
Output is correct |
22 |
Correct |
14 ms |
16768 KB |
Output is correct |
23 |
Correct |
15 ms |
16848 KB |
Output is correct |
24 |
Correct |
16 ms |
16768 KB |
Output is correct |
25 |
Correct |
14 ms |
16768 KB |
Output is correct |
26 |
Correct |
14 ms |
16768 KB |
Output is correct |
27 |
Correct |
14 ms |
16768 KB |
Output is correct |
28 |
Correct |
14 ms |
16768 KB |
Output is correct |
29 |
Correct |
14 ms |
16768 KB |
Output is correct |
30 |
Correct |
14 ms |
16768 KB |
Output is correct |
31 |
Correct |
15 ms |
16768 KB |
Output is correct |
32 |
Correct |
14 ms |
16768 KB |
Output is correct |
33 |
Correct |
15 ms |
16896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
16768 KB |
Output is correct |
2 |
Correct |
14 ms |
16768 KB |
Output is correct |
3 |
Correct |
14 ms |
16768 KB |
Output is correct |
4 |
Correct |
14 ms |
16768 KB |
Output is correct |
5 |
Correct |
14 ms |
16768 KB |
Output is correct |
6 |
Correct |
14 ms |
16768 KB |
Output is correct |
7 |
Correct |
15 ms |
16768 KB |
Output is correct |
8 |
Correct |
15 ms |
16768 KB |
Output is correct |
9 |
Correct |
19 ms |
16896 KB |
Output is correct |
10 |
Correct |
14 ms |
16768 KB |
Output is correct |
11 |
Correct |
15 ms |
16768 KB |
Output is correct |
12 |
Correct |
14 ms |
16768 KB |
Output is correct |
13 |
Correct |
14 ms |
16768 KB |
Output is correct |
14 |
Correct |
14 ms |
16768 KB |
Output is correct |
15 |
Correct |
15 ms |
16768 KB |
Output is correct |
16 |
Correct |
15 ms |
16768 KB |
Output is correct |
17 |
Correct |
14 ms |
16768 KB |
Output is correct |
18 |
Correct |
14 ms |
16768 KB |
Output is correct |
19 |
Correct |
15 ms |
16768 KB |
Output is correct |
20 |
Correct |
15 ms |
16768 KB |
Output is correct |
21 |
Correct |
15 ms |
16896 KB |
Output is correct |
22 |
Correct |
14 ms |
16768 KB |
Output is correct |
23 |
Correct |
15 ms |
16848 KB |
Output is correct |
24 |
Correct |
16 ms |
16768 KB |
Output is correct |
25 |
Correct |
14 ms |
16768 KB |
Output is correct |
26 |
Correct |
14 ms |
16768 KB |
Output is correct |
27 |
Correct |
14 ms |
16768 KB |
Output is correct |
28 |
Correct |
14 ms |
16768 KB |
Output is correct |
29 |
Correct |
14 ms |
16768 KB |
Output is correct |
30 |
Correct |
14 ms |
16768 KB |
Output is correct |
31 |
Correct |
15 ms |
16768 KB |
Output is correct |
32 |
Correct |
14 ms |
16768 KB |
Output is correct |
33 |
Correct |
15 ms |
16896 KB |
Output is correct |
34 |
Correct |
17 ms |
16896 KB |
Output is correct |
35 |
Correct |
97 ms |
22648 KB |
Output is correct |
36 |
Correct |
123 ms |
25464 KB |
Output is correct |
37 |
Correct |
125 ms |
25592 KB |
Output is correct |
38 |
Correct |
122 ms |
25208 KB |
Output is correct |
39 |
Correct |
18 ms |
17152 KB |
Output is correct |
40 |
Correct |
22 ms |
17408 KB |
Output is correct |
41 |
Correct |
21 ms |
17408 KB |
Output is correct |
42 |
Correct |
17 ms |
17152 KB |
Output is correct |
43 |
Correct |
18 ms |
17152 KB |
Output is correct |
44 |
Correct |
18 ms |
17152 KB |
Output is correct |
45 |
Correct |
18 ms |
17152 KB |
Output is correct |
46 |
Correct |
18 ms |
17152 KB |
Output is correct |
47 |
Correct |
20 ms |
17280 KB |
Output is correct |
48 |
Correct |
20 ms |
17280 KB |
Output is correct |
49 |
Correct |
26 ms |
18048 KB |
Output is correct |
50 |
Correct |
121 ms |
25592 KB |
Output is correct |
51 |
Correct |
24 ms |
17408 KB |
Output is correct |
52 |
Correct |
111 ms |
24060 KB |
Output is correct |
53 |
Correct |
26 ms |
17920 KB |
Output is correct |
54 |
Correct |
115 ms |
24896 KB |
Output is correct |
55 |
Correct |
21 ms |
17536 KB |
Output is correct |
56 |
Correct |
23 ms |
17664 KB |
Output is correct |
57 |
Correct |
20 ms |
17920 KB |
Output is correct |
58 |
Correct |
21 ms |
17920 KB |
Output is correct |
59 |
Correct |
21 ms |
17408 KB |
Output is correct |
60 |
Correct |
102 ms |
22396 KB |
Output is correct |
61 |
Correct |
21 ms |
17280 KB |
Output is correct |
62 |
Correct |
122 ms |
25204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
16768 KB |
Output is correct |
2 |
Correct |
14 ms |
16768 KB |
Output is correct |
3 |
Correct |
14 ms |
16768 KB |
Output is correct |
4 |
Correct |
14 ms |
16768 KB |
Output is correct |
5 |
Correct |
14 ms |
16768 KB |
Output is correct |
6 |
Correct |
14 ms |
16768 KB |
Output is correct |
7 |
Correct |
15 ms |
16768 KB |
Output is correct |
8 |
Correct |
15 ms |
16768 KB |
Output is correct |
9 |
Correct |
19 ms |
16896 KB |
Output is correct |
10 |
Correct |
14 ms |
16768 KB |
Output is correct |
11 |
Correct |
15 ms |
16768 KB |
Output is correct |
12 |
Correct |
14 ms |
16768 KB |
Output is correct |
13 |
Correct |
14 ms |
16768 KB |
Output is correct |
14 |
Correct |
14 ms |
16768 KB |
Output is correct |
15 |
Correct |
15 ms |
16768 KB |
Output is correct |
16 |
Correct |
15 ms |
16768 KB |
Output is correct |
17 |
Correct |
14 ms |
16768 KB |
Output is correct |
18 |
Correct |
14 ms |
16768 KB |
Output is correct |
19 |
Correct |
15 ms |
16768 KB |
Output is correct |
20 |
Correct |
15 ms |
16768 KB |
Output is correct |
21 |
Correct |
15 ms |
16896 KB |
Output is correct |
22 |
Correct |
14 ms |
16768 KB |
Output is correct |
23 |
Correct |
15 ms |
16848 KB |
Output is correct |
24 |
Correct |
16 ms |
16768 KB |
Output is correct |
25 |
Correct |
14 ms |
16768 KB |
Output is correct |
26 |
Correct |
14 ms |
16768 KB |
Output is correct |
27 |
Correct |
14 ms |
16768 KB |
Output is correct |
28 |
Correct |
14 ms |
16768 KB |
Output is correct |
29 |
Correct |
14 ms |
16768 KB |
Output is correct |
30 |
Correct |
14 ms |
16768 KB |
Output is correct |
31 |
Correct |
15 ms |
16768 KB |
Output is correct |
32 |
Correct |
14 ms |
16768 KB |
Output is correct |
33 |
Correct |
15 ms |
16896 KB |
Output is correct |
34 |
Correct |
17 ms |
16896 KB |
Output is correct |
35 |
Correct |
97 ms |
22648 KB |
Output is correct |
36 |
Correct |
123 ms |
25464 KB |
Output is correct |
37 |
Correct |
125 ms |
25592 KB |
Output is correct |
38 |
Correct |
122 ms |
25208 KB |
Output is correct |
39 |
Correct |
18 ms |
17152 KB |
Output is correct |
40 |
Correct |
22 ms |
17408 KB |
Output is correct |
41 |
Correct |
21 ms |
17408 KB |
Output is correct |
42 |
Correct |
17 ms |
17152 KB |
Output is correct |
43 |
Correct |
18 ms |
17152 KB |
Output is correct |
44 |
Correct |
18 ms |
17152 KB |
Output is correct |
45 |
Correct |
18 ms |
17152 KB |
Output is correct |
46 |
Correct |
18 ms |
17152 KB |
Output is correct |
47 |
Correct |
20 ms |
17280 KB |
Output is correct |
48 |
Correct |
20 ms |
17280 KB |
Output is correct |
49 |
Correct |
26 ms |
18048 KB |
Output is correct |
50 |
Correct |
121 ms |
25592 KB |
Output is correct |
51 |
Correct |
24 ms |
17408 KB |
Output is correct |
52 |
Correct |
111 ms |
24060 KB |
Output is correct |
53 |
Correct |
26 ms |
17920 KB |
Output is correct |
54 |
Correct |
115 ms |
24896 KB |
Output is correct |
55 |
Correct |
21 ms |
17536 KB |
Output is correct |
56 |
Correct |
23 ms |
17664 KB |
Output is correct |
57 |
Correct |
20 ms |
17920 KB |
Output is correct |
58 |
Correct |
21 ms |
17920 KB |
Output is correct |
59 |
Correct |
21 ms |
17408 KB |
Output is correct |
60 |
Correct |
102 ms |
22396 KB |
Output is correct |
61 |
Correct |
21 ms |
17280 KB |
Output is correct |
62 |
Correct |
122 ms |
25204 KB |
Output is correct |
63 |
Correct |
578 ms |
68020 KB |
Output is correct |
64 |
Correct |
600 ms |
68016 KB |
Output is correct |
65 |
Correct |
602 ms |
68188 KB |
Output is correct |
66 |
Correct |
277 ms |
34672 KB |
Output is correct |
67 |
Correct |
1075 ms |
49496 KB |
Output is correct |
68 |
Correct |
267 ms |
34672 KB |
Output is correct |
69 |
Correct |
492 ms |
35824 KB |
Output is correct |
70 |
Correct |
280 ms |
34800 KB |
Output is correct |
71 |
Correct |
273 ms |
34672 KB |
Output is correct |
72 |
Correct |
771 ms |
43756 KB |
Output is correct |
73 |
Correct |
821 ms |
44656 KB |
Output is correct |
74 |
Correct |
1423 ms |
64172 KB |
Output is correct |
75 |
Correct |
693 ms |
40688 KB |
Output is correct |
76 |
Correct |
1010 ms |
52084 KB |
Output is correct |
77 |
Correct |
1020 ms |
52460 KB |
Output is correct |
78 |
Correct |
276 ms |
35828 KB |
Output is correct |
79 |
Correct |
467 ms |
39792 KB |
Output is correct |
80 |
Correct |
318 ms |
35700 KB |
Output is correct |
81 |
Correct |
516 ms |
39536 KB |
Output is correct |
82 |
Correct |
855 ms |
52984 KB |
Output is correct |
83 |
Correct |
862 ms |
52856 KB |
Output is correct |
84 |
Correct |
699 ms |
76016 KB |
Output is correct |
85 |
Correct |
671 ms |
76016 KB |
Output is correct |
86 |
Correct |
925 ms |
49388 KB |
Output is correct |
87 |
Correct |
946 ms |
51692 KB |
Output is correct |
88 |
Correct |
818 ms |
43640 KB |
Output is correct |
89 |
Correct |
1035 ms |
47176 KB |
Output is correct |