#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define endl '\n'
using namespace std;
const int maxn = 1e5 + 3;
int n, m;
void read()
{
cin >> n >> m;
}
int par[maxn], sz[maxn];
set <int> adj[maxn], rev_adj[maxn], members[maxn];
int find_root(int ver)
{
if (ver == par[ver])
return ver;
return par[ver] = find_root(par[ver]);
}
long long ans = 0;
void merge(int a, int b)
{
a = find_root(a), b = find_root(b);
if (a == b)
return;
ans -= 1ll * sz[a] * (sz[a] - 1);
ans -= 1ll * sz[b] * (sz[b] - 1);
ans -= 1ll * (int)rev_adj[a].size() * sz[a];
ans -= 1ll * (int)rev_adj[b].size() * sz[b];
if (sz[a] < sz[b])
swap(a, b);
for (auto i: members[b])
members[a].insert(i);
for (auto j: adj[b])
if (j != a)
adj[a].insert(j);
for (auto j: rev_adj[b])
{
if (find_root(j) == a)
continue;
adj[find_root(j)].erase(b);
adj[find_root(j)].insert(a);
rev_adj[a].insert(j);
}
adj[a].erase(b);
vector <int> to_remove;
for (auto i: members[b])
if (rev_adj[a].find(i) != rev_adj[a].end())
to_remove.push_back(i);
for (auto i: to_remove)
rev_adj[a].erase(i);
/*
cout << a << ":";
for (auto i: adj[a])
cout << i << " ";
cout << endl;
cout << endl;
for (auto i: rev_adj[a])
cout << i << " ";
// cout << endl;
// cout << rev_adj[1].size() << endl;
// cout << endl;
exit(0);
*/
par[b] = a;
sz[a] += sz[b];
ans += 1ll * sz[a] * (sz[a] - 1);
ans += 1ll * (int)rev_adj[a].size() * sz[a];
}
pair <int, int> e[maxn * 3];
void solve()
{
for (int i = 1; i <= n; i++)
{
par[i] = i, sz[i] = 1;
members[i].insert(i);
}
for (int i = 1; i <= m; i++)
{
cin >> e[i].first >> e[i].second;
int a = find_root(e[i].first), b = find_root(e[i].second);
if (a != b)
{
ans -= 1ll * (int)rev_adj[b].size() * sz[b];
adj[a].insert(b);
rev_adj[b].insert(e[i].first);
ans += 1ll * (int)rev_adj[b].size() * sz[b];
int curr = a;
for (;;)
{
bool can = false;
for (auto j: adj[curr])
if (adj[j].find(curr) != adj[j].end())
{
merge(j, curr);
curr = find_root(curr);
can = true;
break;
}
if (!can)
break;
}
}
//cout << i << "shit" << (int)rev_adj[2].size() << endl;
// cout << find_ans(i) << endl;
cout << ans << endl;
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
read();
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
14444 KB |
Output is correct |
2 |
Correct |
10 ms |
14444 KB |
Output is correct |
3 |
Correct |
10 ms |
14444 KB |
Output is correct |
4 |
Correct |
10 ms |
14444 KB |
Output is correct |
5 |
Correct |
10 ms |
14444 KB |
Output is correct |
6 |
Correct |
11 ms |
14444 KB |
Output is correct |
7 |
Correct |
13 ms |
14444 KB |
Output is correct |
8 |
Correct |
11 ms |
14444 KB |
Output is correct |
9 |
Correct |
11 ms |
14444 KB |
Output is correct |
10 |
Correct |
11 ms |
14444 KB |
Output is correct |
11 |
Correct |
11 ms |
14444 KB |
Output is correct |
12 |
Correct |
11 ms |
14444 KB |
Output is correct |
13 |
Correct |
11 ms |
14444 KB |
Output is correct |
14 |
Correct |
11 ms |
14592 KB |
Output is correct |
15 |
Correct |
11 ms |
14444 KB |
Output is correct |
16 |
Correct |
11 ms |
14572 KB |
Output is correct |
17 |
Correct |
11 ms |
14444 KB |
Output is correct |
18 |
Correct |
10 ms |
14444 KB |
Output is correct |
19 |
Correct |
11 ms |
14444 KB |
Output is correct |
20 |
Correct |
11 ms |
14444 KB |
Output is correct |
21 |
Correct |
11 ms |
14444 KB |
Output is correct |
22 |
Correct |
11 ms |
14444 KB |
Output is correct |
23 |
Correct |
13 ms |
14444 KB |
Output is correct |
24 |
Correct |
11 ms |
14444 KB |
Output is correct |
25 |
Correct |
11 ms |
14444 KB |
Output is correct |
26 |
Correct |
11 ms |
14444 KB |
Output is correct |
27 |
Correct |
10 ms |
14444 KB |
Output is correct |
28 |
Correct |
11 ms |
14444 KB |
Output is correct |
29 |
Correct |
12 ms |
14444 KB |
Output is correct |
30 |
Correct |
10 ms |
14444 KB |
Output is correct |
31 |
Correct |
11 ms |
14444 KB |
Output is correct |
32 |
Correct |
11 ms |
14444 KB |
Output is correct |
33 |
Correct |
11 ms |
14464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
14444 KB |
Output is correct |
2 |
Correct |
10 ms |
14444 KB |
Output is correct |
3 |
Correct |
10 ms |
14444 KB |
Output is correct |
4 |
Correct |
10 ms |
14444 KB |
Output is correct |
5 |
Correct |
10 ms |
14444 KB |
Output is correct |
6 |
Correct |
11 ms |
14444 KB |
Output is correct |
7 |
Correct |
13 ms |
14444 KB |
Output is correct |
8 |
Correct |
11 ms |
14444 KB |
Output is correct |
9 |
Correct |
11 ms |
14444 KB |
Output is correct |
10 |
Correct |
11 ms |
14444 KB |
Output is correct |
11 |
Correct |
11 ms |
14444 KB |
Output is correct |
12 |
Correct |
11 ms |
14444 KB |
Output is correct |
13 |
Correct |
11 ms |
14444 KB |
Output is correct |
14 |
Correct |
11 ms |
14592 KB |
Output is correct |
15 |
Correct |
11 ms |
14444 KB |
Output is correct |
16 |
Correct |
11 ms |
14572 KB |
Output is correct |
17 |
Correct |
11 ms |
14444 KB |
Output is correct |
18 |
Correct |
10 ms |
14444 KB |
Output is correct |
19 |
Correct |
11 ms |
14444 KB |
Output is correct |
20 |
Correct |
11 ms |
14444 KB |
Output is correct |
21 |
Correct |
11 ms |
14444 KB |
Output is correct |
22 |
Correct |
11 ms |
14444 KB |
Output is correct |
23 |
Correct |
13 ms |
14444 KB |
Output is correct |
24 |
Correct |
11 ms |
14444 KB |
Output is correct |
25 |
Correct |
11 ms |
14444 KB |
Output is correct |
26 |
Correct |
11 ms |
14444 KB |
Output is correct |
27 |
Correct |
10 ms |
14444 KB |
Output is correct |
28 |
Correct |
11 ms |
14444 KB |
Output is correct |
29 |
Correct |
12 ms |
14444 KB |
Output is correct |
30 |
Correct |
10 ms |
14444 KB |
Output is correct |
31 |
Correct |
11 ms |
14444 KB |
Output is correct |
32 |
Correct |
11 ms |
14444 KB |
Output is correct |
33 |
Correct |
11 ms |
14464 KB |
Output is correct |
34 |
Correct |
13 ms |
14572 KB |
Output is correct |
35 |
Correct |
96 ms |
19824 KB |
Output is correct |
36 |
Correct |
124 ms |
21612 KB |
Output is correct |
37 |
Correct |
135 ms |
21740 KB |
Output is correct |
38 |
Correct |
126 ms |
21356 KB |
Output is correct |
39 |
Correct |
13 ms |
14956 KB |
Output is correct |
40 |
Correct |
17 ms |
15212 KB |
Output is correct |
41 |
Correct |
15 ms |
15212 KB |
Output is correct |
42 |
Correct |
13 ms |
14956 KB |
Output is correct |
43 |
Correct |
30 ms |
14956 KB |
Output is correct |
44 |
Correct |
28 ms |
14956 KB |
Output is correct |
45 |
Correct |
13 ms |
14956 KB |
Output is correct |
46 |
Correct |
13 ms |
14956 KB |
Output is correct |
47 |
Correct |
18 ms |
15084 KB |
Output is correct |
48 |
Correct |
16 ms |
15084 KB |
Output is correct |
49 |
Correct |
22 ms |
15468 KB |
Output is correct |
50 |
Correct |
134 ms |
21740 KB |
Output is correct |
51 |
Correct |
32 ms |
15084 KB |
Output is correct |
52 |
Correct |
109 ms |
20588 KB |
Output is correct |
53 |
Correct |
24 ms |
15468 KB |
Output is correct |
54 |
Correct |
115 ms |
21228 KB |
Output is correct |
55 |
Correct |
21 ms |
15084 KB |
Output is correct |
56 |
Correct |
21 ms |
15212 KB |
Output is correct |
57 |
Correct |
23 ms |
15084 KB |
Output is correct |
58 |
Correct |
20 ms |
15084 KB |
Output is correct |
59 |
Correct |
15 ms |
15340 KB |
Output is correct |
60 |
Correct |
111 ms |
20076 KB |
Output is correct |
61 |
Correct |
18 ms |
15084 KB |
Output is correct |
62 |
Correct |
122 ms |
21356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
14444 KB |
Output is correct |
2 |
Correct |
10 ms |
14444 KB |
Output is correct |
3 |
Correct |
10 ms |
14444 KB |
Output is correct |
4 |
Correct |
10 ms |
14444 KB |
Output is correct |
5 |
Correct |
10 ms |
14444 KB |
Output is correct |
6 |
Correct |
11 ms |
14444 KB |
Output is correct |
7 |
Correct |
13 ms |
14444 KB |
Output is correct |
8 |
Correct |
11 ms |
14444 KB |
Output is correct |
9 |
Correct |
11 ms |
14444 KB |
Output is correct |
10 |
Correct |
11 ms |
14444 KB |
Output is correct |
11 |
Correct |
11 ms |
14444 KB |
Output is correct |
12 |
Correct |
11 ms |
14444 KB |
Output is correct |
13 |
Correct |
11 ms |
14444 KB |
Output is correct |
14 |
Correct |
11 ms |
14592 KB |
Output is correct |
15 |
Correct |
11 ms |
14444 KB |
Output is correct |
16 |
Correct |
11 ms |
14572 KB |
Output is correct |
17 |
Correct |
11 ms |
14444 KB |
Output is correct |
18 |
Correct |
10 ms |
14444 KB |
Output is correct |
19 |
Correct |
11 ms |
14444 KB |
Output is correct |
20 |
Correct |
11 ms |
14444 KB |
Output is correct |
21 |
Correct |
11 ms |
14444 KB |
Output is correct |
22 |
Correct |
11 ms |
14444 KB |
Output is correct |
23 |
Correct |
13 ms |
14444 KB |
Output is correct |
24 |
Correct |
11 ms |
14444 KB |
Output is correct |
25 |
Correct |
11 ms |
14444 KB |
Output is correct |
26 |
Correct |
11 ms |
14444 KB |
Output is correct |
27 |
Correct |
10 ms |
14444 KB |
Output is correct |
28 |
Correct |
11 ms |
14444 KB |
Output is correct |
29 |
Correct |
12 ms |
14444 KB |
Output is correct |
30 |
Correct |
10 ms |
14444 KB |
Output is correct |
31 |
Correct |
11 ms |
14444 KB |
Output is correct |
32 |
Correct |
11 ms |
14444 KB |
Output is correct |
33 |
Correct |
11 ms |
14464 KB |
Output is correct |
34 |
Correct |
13 ms |
14572 KB |
Output is correct |
35 |
Correct |
96 ms |
19824 KB |
Output is correct |
36 |
Correct |
124 ms |
21612 KB |
Output is correct |
37 |
Correct |
135 ms |
21740 KB |
Output is correct |
38 |
Correct |
126 ms |
21356 KB |
Output is correct |
39 |
Correct |
13 ms |
14956 KB |
Output is correct |
40 |
Correct |
17 ms |
15212 KB |
Output is correct |
41 |
Correct |
15 ms |
15212 KB |
Output is correct |
42 |
Correct |
13 ms |
14956 KB |
Output is correct |
43 |
Correct |
30 ms |
14956 KB |
Output is correct |
44 |
Correct |
28 ms |
14956 KB |
Output is correct |
45 |
Correct |
13 ms |
14956 KB |
Output is correct |
46 |
Correct |
13 ms |
14956 KB |
Output is correct |
47 |
Correct |
18 ms |
15084 KB |
Output is correct |
48 |
Correct |
16 ms |
15084 KB |
Output is correct |
49 |
Correct |
22 ms |
15468 KB |
Output is correct |
50 |
Correct |
134 ms |
21740 KB |
Output is correct |
51 |
Correct |
32 ms |
15084 KB |
Output is correct |
52 |
Correct |
109 ms |
20588 KB |
Output is correct |
53 |
Correct |
24 ms |
15468 KB |
Output is correct |
54 |
Correct |
115 ms |
21228 KB |
Output is correct |
55 |
Correct |
21 ms |
15084 KB |
Output is correct |
56 |
Correct |
21 ms |
15212 KB |
Output is correct |
57 |
Correct |
23 ms |
15084 KB |
Output is correct |
58 |
Correct |
20 ms |
15084 KB |
Output is correct |
59 |
Correct |
15 ms |
15340 KB |
Output is correct |
60 |
Correct |
111 ms |
20076 KB |
Output is correct |
61 |
Correct |
18 ms |
15084 KB |
Output is correct |
62 |
Correct |
122 ms |
21356 KB |
Output is correct |
63 |
Correct |
515 ms |
52460 KB |
Output is correct |
64 |
Correct |
516 ms |
52844 KB |
Output is correct |
65 |
Correct |
528 ms |
52504 KB |
Output is correct |
66 |
Correct |
299 ms |
37868 KB |
Output is correct |
67 |
Correct |
461 ms |
63724 KB |
Output is correct |
68 |
Correct |
284 ms |
37740 KB |
Output is correct |
69 |
Execution timed out |
5046 ms |
23944 KB |
Time limit exceeded |
70 |
Halted |
0 ms |
0 KB |
- |