#include<bits/stdc++.h>
#define ll long long
#define task ""
using namespace std;
const ll maxn = 1e6 + 2, LG = 20, blocksize = 550, inf = 1e18;
int n, m, a, b, par[maxn];
ll res = 0;
set<int> f[maxn], adj[maxn], radj[maxn];
vector<pair<int, int>> cur;
int find_set (int u)
{
if (par[u] < 0) return u;
return (par[u] = find_set(par[u]));
}
void union_set (int u, int v)
{
u = find_set(u); v = find_set(v);
if (u == v) return;
if (f[u].size() + adj[u].size() + radj[u].size() < f[v].size() + adj[v].size() + radj[v].size()) swap(u, v);
res -= 1LL*par[u]*f[v].size() + 1LL*par[v]*f[u].size();
par[u] += par[v];
par[v] = u;
for (int i : f[v])
{
if (f[u].find(i) != f[u].end()) res += par[u];
else f[u].insert(i);
}
f[v].clear();
adj[u].erase(v); radj[u].erase(v);
adj[v].erase(u); radj[v].erase(u);
for (int i : adj[v])
{
adj[u].insert(i);
radj[i].insert(u);
if (radj[u].find(i) != radj[u].end()) cur.push_back({u, i});
}
for (int i : radj[v])
{
radj[u].insert(i);
adj[i].insert(u);
if (adj[u].find(i) != adj[u].end()) cur.push_back({u, i});
}
}
int main()
{
//freopen(task".INP", "r", stdin);
//freopen(task".OUT", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
par[i] = -1;
f[i].insert(i);
}
while (m--)
{
cin >> a >> b;
b = find_set(b);
if (a == b || f[b].find(a) != f[b].end())
{
cout << res << '\n';
continue;
}
res -= par[b];
f[b].insert(a);
a = find_set(a);
adj[a].insert(b);
radj[b].insert(a);
if (radj[a].find(b) != radj[a].end()) cur.push_back({a, b});
while (!cur.empty())
{
union_set(cur[0].first, cur[0].second);
swap(cur[0], cur.back());
cur.pop_back();
}
cout << res << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
142160 KB |
Output is correct |
2 |
Correct |
23 ms |
142216 KB |
Output is correct |
3 |
Correct |
22 ms |
142328 KB |
Output is correct |
4 |
Correct |
23 ms |
142172 KB |
Output is correct |
5 |
Correct |
28 ms |
142160 KB |
Output is correct |
6 |
Correct |
23 ms |
142160 KB |
Output is correct |
7 |
Correct |
22 ms |
142160 KB |
Output is correct |
8 |
Correct |
24 ms |
142160 KB |
Output is correct |
9 |
Correct |
24 ms |
142172 KB |
Output is correct |
10 |
Correct |
22 ms |
142228 KB |
Output is correct |
11 |
Correct |
24 ms |
142160 KB |
Output is correct |
12 |
Correct |
24 ms |
142160 KB |
Output is correct |
13 |
Correct |
23 ms |
142160 KB |
Output is correct |
14 |
Correct |
24 ms |
142160 KB |
Output is correct |
15 |
Correct |
25 ms |
142160 KB |
Output is correct |
16 |
Correct |
25 ms |
142172 KB |
Output is correct |
17 |
Correct |
23 ms |
142160 KB |
Output is correct |
18 |
Correct |
25 ms |
142160 KB |
Output is correct |
19 |
Correct |
24 ms |
142160 KB |
Output is correct |
20 |
Correct |
24 ms |
142160 KB |
Output is correct |
21 |
Correct |
25 ms |
142160 KB |
Output is correct |
22 |
Correct |
24 ms |
142160 KB |
Output is correct |
23 |
Correct |
24 ms |
142112 KB |
Output is correct |
24 |
Correct |
26 ms |
142160 KB |
Output is correct |
25 |
Correct |
25 ms |
142236 KB |
Output is correct |
26 |
Correct |
25 ms |
142160 KB |
Output is correct |
27 |
Correct |
25 ms |
142160 KB |
Output is correct |
28 |
Correct |
24 ms |
142160 KB |
Output is correct |
29 |
Correct |
22 ms |
142160 KB |
Output is correct |
30 |
Correct |
22 ms |
142172 KB |
Output is correct |
31 |
Correct |
22 ms |
142160 KB |
Output is correct |
32 |
Correct |
22 ms |
142164 KB |
Output is correct |
33 |
Correct |
24 ms |
142160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
142160 KB |
Output is correct |
2 |
Correct |
23 ms |
142216 KB |
Output is correct |
3 |
Correct |
22 ms |
142328 KB |
Output is correct |
4 |
Correct |
23 ms |
142172 KB |
Output is correct |
5 |
Correct |
28 ms |
142160 KB |
Output is correct |
6 |
Correct |
23 ms |
142160 KB |
Output is correct |
7 |
Correct |
22 ms |
142160 KB |
Output is correct |
8 |
Correct |
24 ms |
142160 KB |
Output is correct |
9 |
Correct |
24 ms |
142172 KB |
Output is correct |
10 |
Correct |
22 ms |
142228 KB |
Output is correct |
11 |
Correct |
24 ms |
142160 KB |
Output is correct |
12 |
Correct |
24 ms |
142160 KB |
Output is correct |
13 |
Correct |
23 ms |
142160 KB |
Output is correct |
14 |
Correct |
24 ms |
142160 KB |
Output is correct |
15 |
Correct |
25 ms |
142160 KB |
Output is correct |
16 |
Correct |
25 ms |
142172 KB |
Output is correct |
17 |
Correct |
23 ms |
142160 KB |
Output is correct |
18 |
Correct |
25 ms |
142160 KB |
Output is correct |
19 |
Correct |
24 ms |
142160 KB |
Output is correct |
20 |
Correct |
24 ms |
142160 KB |
Output is correct |
21 |
Correct |
25 ms |
142160 KB |
Output is correct |
22 |
Correct |
24 ms |
142160 KB |
Output is correct |
23 |
Correct |
24 ms |
142112 KB |
Output is correct |
24 |
Correct |
26 ms |
142160 KB |
Output is correct |
25 |
Correct |
25 ms |
142236 KB |
Output is correct |
26 |
Correct |
25 ms |
142160 KB |
Output is correct |
27 |
Correct |
25 ms |
142160 KB |
Output is correct |
28 |
Correct |
24 ms |
142160 KB |
Output is correct |
29 |
Correct |
22 ms |
142160 KB |
Output is correct |
30 |
Correct |
22 ms |
142172 KB |
Output is correct |
31 |
Correct |
22 ms |
142160 KB |
Output is correct |
32 |
Correct |
22 ms |
142164 KB |
Output is correct |
33 |
Correct |
24 ms |
142160 KB |
Output is correct |
34 |
Correct |
23 ms |
142180 KB |
Output is correct |
35 |
Correct |
90 ms |
148212 KB |
Output is correct |
36 |
Correct |
117 ms |
151060 KB |
Output is correct |
37 |
Correct |
117 ms |
151236 KB |
Output is correct |
38 |
Correct |
122 ms |
150724 KB |
Output is correct |
39 |
Correct |
24 ms |
142160 KB |
Output is correct |
40 |
Correct |
27 ms |
142928 KB |
Output is correct |
41 |
Correct |
26 ms |
142672 KB |
Output is correct |
42 |
Correct |
23 ms |
142160 KB |
Output is correct |
43 |
Correct |
24 ms |
142408 KB |
Output is correct |
44 |
Correct |
25 ms |
142532 KB |
Output is correct |
45 |
Correct |
25 ms |
142160 KB |
Output is correct |
46 |
Correct |
25 ms |
142328 KB |
Output is correct |
47 |
Correct |
25 ms |
142604 KB |
Output is correct |
48 |
Correct |
29 ms |
142416 KB |
Output is correct |
49 |
Correct |
36 ms |
143688 KB |
Output is correct |
50 |
Correct |
130 ms |
151364 KB |
Output is correct |
51 |
Correct |
36 ms |
142672 KB |
Output is correct |
52 |
Correct |
151 ms |
149556 KB |
Output is correct |
53 |
Correct |
34 ms |
143440 KB |
Output is correct |
54 |
Correct |
115 ms |
150212 KB |
Output is correct |
55 |
Correct |
32 ms |
142928 KB |
Output is correct |
56 |
Correct |
30 ms |
142920 KB |
Output is correct |
57 |
Correct |
29 ms |
142928 KB |
Output is correct |
58 |
Correct |
29 ms |
142920 KB |
Output is correct |
59 |
Correct |
26 ms |
142424 KB |
Output is correct |
60 |
Correct |
93 ms |
147272 KB |
Output is correct |
61 |
Correct |
28 ms |
142552 KB |
Output is correct |
62 |
Correct |
121 ms |
150724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
142160 KB |
Output is correct |
2 |
Correct |
23 ms |
142216 KB |
Output is correct |
3 |
Correct |
22 ms |
142328 KB |
Output is correct |
4 |
Correct |
23 ms |
142172 KB |
Output is correct |
5 |
Correct |
28 ms |
142160 KB |
Output is correct |
6 |
Correct |
23 ms |
142160 KB |
Output is correct |
7 |
Correct |
22 ms |
142160 KB |
Output is correct |
8 |
Correct |
24 ms |
142160 KB |
Output is correct |
9 |
Correct |
24 ms |
142172 KB |
Output is correct |
10 |
Correct |
22 ms |
142228 KB |
Output is correct |
11 |
Correct |
24 ms |
142160 KB |
Output is correct |
12 |
Correct |
24 ms |
142160 KB |
Output is correct |
13 |
Correct |
23 ms |
142160 KB |
Output is correct |
14 |
Correct |
24 ms |
142160 KB |
Output is correct |
15 |
Correct |
25 ms |
142160 KB |
Output is correct |
16 |
Correct |
25 ms |
142172 KB |
Output is correct |
17 |
Correct |
23 ms |
142160 KB |
Output is correct |
18 |
Correct |
25 ms |
142160 KB |
Output is correct |
19 |
Correct |
24 ms |
142160 KB |
Output is correct |
20 |
Correct |
24 ms |
142160 KB |
Output is correct |
21 |
Correct |
25 ms |
142160 KB |
Output is correct |
22 |
Correct |
24 ms |
142160 KB |
Output is correct |
23 |
Correct |
24 ms |
142112 KB |
Output is correct |
24 |
Correct |
26 ms |
142160 KB |
Output is correct |
25 |
Correct |
25 ms |
142236 KB |
Output is correct |
26 |
Correct |
25 ms |
142160 KB |
Output is correct |
27 |
Correct |
25 ms |
142160 KB |
Output is correct |
28 |
Correct |
24 ms |
142160 KB |
Output is correct |
29 |
Correct |
22 ms |
142160 KB |
Output is correct |
30 |
Correct |
22 ms |
142172 KB |
Output is correct |
31 |
Correct |
22 ms |
142160 KB |
Output is correct |
32 |
Correct |
22 ms |
142164 KB |
Output is correct |
33 |
Correct |
24 ms |
142160 KB |
Output is correct |
34 |
Correct |
23 ms |
142180 KB |
Output is correct |
35 |
Correct |
90 ms |
148212 KB |
Output is correct |
36 |
Correct |
117 ms |
151060 KB |
Output is correct |
37 |
Correct |
117 ms |
151236 KB |
Output is correct |
38 |
Correct |
122 ms |
150724 KB |
Output is correct |
39 |
Correct |
24 ms |
142160 KB |
Output is correct |
40 |
Correct |
27 ms |
142928 KB |
Output is correct |
41 |
Correct |
26 ms |
142672 KB |
Output is correct |
42 |
Correct |
23 ms |
142160 KB |
Output is correct |
43 |
Correct |
24 ms |
142408 KB |
Output is correct |
44 |
Correct |
25 ms |
142532 KB |
Output is correct |
45 |
Correct |
25 ms |
142160 KB |
Output is correct |
46 |
Correct |
25 ms |
142328 KB |
Output is correct |
47 |
Correct |
25 ms |
142604 KB |
Output is correct |
48 |
Correct |
29 ms |
142416 KB |
Output is correct |
49 |
Correct |
36 ms |
143688 KB |
Output is correct |
50 |
Correct |
130 ms |
151364 KB |
Output is correct |
51 |
Correct |
36 ms |
142672 KB |
Output is correct |
52 |
Correct |
151 ms |
149556 KB |
Output is correct |
53 |
Correct |
34 ms |
143440 KB |
Output is correct |
54 |
Correct |
115 ms |
150212 KB |
Output is correct |
55 |
Correct |
32 ms |
142928 KB |
Output is correct |
56 |
Correct |
30 ms |
142920 KB |
Output is correct |
57 |
Correct |
29 ms |
142928 KB |
Output is correct |
58 |
Correct |
29 ms |
142920 KB |
Output is correct |
59 |
Correct |
26 ms |
142424 KB |
Output is correct |
60 |
Correct |
93 ms |
147272 KB |
Output is correct |
61 |
Correct |
28 ms |
142552 KB |
Output is correct |
62 |
Correct |
121 ms |
150724 KB |
Output is correct |
63 |
Correct |
457 ms |
194632 KB |
Output is correct |
64 |
Correct |
422 ms |
194592 KB |
Output is correct |
65 |
Correct |
398 ms |
194632 KB |
Output is correct |
66 |
Correct |
175 ms |
151148 KB |
Output is correct |
67 |
Correct |
539 ms |
189264 KB |
Output is correct |
68 |
Correct |
160 ms |
151128 KB |
Output is correct |
69 |
Correct |
298 ms |
158232 KB |
Output is correct |
70 |
Correct |
188 ms |
151112 KB |
Output is correct |
71 |
Correct |
185 ms |
151112 KB |
Output is correct |
72 |
Correct |
368 ms |
162808 KB |
Output is correct |
73 |
Correct |
359 ms |
164004 KB |
Output is correct |
74 |
Correct |
1157 ms |
211196 KB |
Output is correct |
75 |
Correct |
525 ms |
166216 KB |
Output is correct |
76 |
Correct |
798 ms |
181676 KB |
Output is correct |
77 |
Correct |
919 ms |
181860 KB |
Output is correct |
78 |
Correct |
239 ms |
161732 KB |
Output is correct |
79 |
Correct |
395 ms |
165020 KB |
Output is correct |
80 |
Correct |
241 ms |
161620 KB |
Output is correct |
81 |
Correct |
465 ms |
164932 KB |
Output is correct |
82 |
Correct |
801 ms |
182604 KB |
Output is correct |
83 |
Correct |
768 ms |
182704 KB |
Output is correct |
84 |
Correct |
865 ms |
184004 KB |
Output is correct |
85 |
Correct |
783 ms |
183916 KB |
Output is correct |
86 |
Correct |
268 ms |
152136 KB |
Output is correct |
87 |
Correct |
308 ms |
154176 KB |
Output is correct |
88 |
Correct |
464 ms |
163400 KB |
Output is correct |
89 |
Correct |
788 ms |
179516 KB |
Output is correct |