/* In the name of God, aka Allah */
// let this be mytemp.cpp
#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define endl '\n'
#define mk make_pair
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int N = 1e5 + 50;
int n, m, id[N];
vector<int> vc[N];
set<int> adj[2][N], st[N];
queue<pii> q;
ll ans;
void uniset(int u, int v) {
u = id[u], v= id[v];
if (u == v) return;
if (vc[u].size() < vc[v].size()) swap(u, v);
ans -= 1ll*st[u].size()*vc[u].size();
ans -= 1ll*st[v].size()*vc[v].size();
ans += 2ll*vc[u].size()*vc[v].size();
for (int x:vc[v]) {
id[x] = u;
vc[u].push_back(x);
st[u].erase(x);
}
vc[v].clear();
for (int x:st[v]) {
if (id[x] != u) {
st[u].insert(x);
}
}
st[v].clear();
ans += 1ll*st[u].size()*vc[u].size();
adj[0][u].erase(v);
adj[1][u].erase(v);
for (int x:adj[0][v]) {
if (x == u) continue;
adj[1][x].erase(v);
adj[1][x].insert(u);
if (adj[1][u].find(x) != adj[1][u].end()) q.push({u, x});
adj[0][u].insert(x);
}
adj[0][v].clear();
for (int x:adj[1][v]) {
if (x == u) continue;
adj[0][x].erase(v);
adj[0][x].insert(u);
if (adj[0][u].find(x) != adj[0][u].end()) q.push({u, x});
adj[1][u].insert(x);
}
adj[1][v].clear();
}
void solve() {
cin >> n >> m;
for (int u = 1; u <= n; u++) {
id[u] = u;
vc[u].push_back(u);
}
while (m--) {
int u, v;
cin >> u >> v;
v = id[v];
if (id[u] != id[v] && st[v].find(u) == st[v].end()) {
ans += vc[v].size();
st[v].insert(u);
}
u = id[u];
adj[0][u].insert(v);
adj[1][v].insert(u);
if (adj[0][v].find(u) != adj[0][v].end()) q.push({u, v});
while (q.size()) {
int x = q.front().F, y = q.front().S;
q.pop();
uniset(x, y);
}
cout << ans << endl;
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
16724 KB |
Output is correct |
2 |
Correct |
8 ms |
16660 KB |
Output is correct |
3 |
Correct |
8 ms |
16728 KB |
Output is correct |
4 |
Correct |
9 ms |
16724 KB |
Output is correct |
5 |
Correct |
10 ms |
16724 KB |
Output is correct |
6 |
Correct |
8 ms |
16724 KB |
Output is correct |
7 |
Correct |
8 ms |
16780 KB |
Output is correct |
8 |
Correct |
9 ms |
16724 KB |
Output is correct |
9 |
Correct |
9 ms |
16812 KB |
Output is correct |
10 |
Correct |
8 ms |
16700 KB |
Output is correct |
11 |
Correct |
10 ms |
16724 KB |
Output is correct |
12 |
Correct |
8 ms |
16700 KB |
Output is correct |
13 |
Correct |
9 ms |
16676 KB |
Output is correct |
14 |
Correct |
8 ms |
16784 KB |
Output is correct |
15 |
Correct |
8 ms |
16724 KB |
Output is correct |
16 |
Correct |
8 ms |
16696 KB |
Output is correct |
17 |
Correct |
8 ms |
16736 KB |
Output is correct |
18 |
Correct |
8 ms |
16724 KB |
Output is correct |
19 |
Correct |
8 ms |
16724 KB |
Output is correct |
20 |
Correct |
8 ms |
16744 KB |
Output is correct |
21 |
Correct |
9 ms |
16724 KB |
Output is correct |
22 |
Correct |
8 ms |
16724 KB |
Output is correct |
23 |
Correct |
8 ms |
16724 KB |
Output is correct |
24 |
Correct |
8 ms |
16724 KB |
Output is correct |
25 |
Correct |
11 ms |
16724 KB |
Output is correct |
26 |
Correct |
8 ms |
16792 KB |
Output is correct |
27 |
Correct |
8 ms |
16720 KB |
Output is correct |
28 |
Correct |
8 ms |
16724 KB |
Output is correct |
29 |
Correct |
8 ms |
16724 KB |
Output is correct |
30 |
Correct |
8 ms |
16724 KB |
Output is correct |
31 |
Correct |
9 ms |
16724 KB |
Output is correct |
32 |
Correct |
8 ms |
16724 KB |
Output is correct |
33 |
Correct |
9 ms |
16724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
16724 KB |
Output is correct |
2 |
Correct |
8 ms |
16660 KB |
Output is correct |
3 |
Correct |
8 ms |
16728 KB |
Output is correct |
4 |
Correct |
9 ms |
16724 KB |
Output is correct |
5 |
Correct |
10 ms |
16724 KB |
Output is correct |
6 |
Correct |
8 ms |
16724 KB |
Output is correct |
7 |
Correct |
8 ms |
16780 KB |
Output is correct |
8 |
Correct |
9 ms |
16724 KB |
Output is correct |
9 |
Correct |
9 ms |
16812 KB |
Output is correct |
10 |
Correct |
8 ms |
16700 KB |
Output is correct |
11 |
Correct |
10 ms |
16724 KB |
Output is correct |
12 |
Correct |
8 ms |
16700 KB |
Output is correct |
13 |
Correct |
9 ms |
16676 KB |
Output is correct |
14 |
Correct |
8 ms |
16784 KB |
Output is correct |
15 |
Correct |
8 ms |
16724 KB |
Output is correct |
16 |
Correct |
8 ms |
16696 KB |
Output is correct |
17 |
Correct |
8 ms |
16736 KB |
Output is correct |
18 |
Correct |
8 ms |
16724 KB |
Output is correct |
19 |
Correct |
8 ms |
16724 KB |
Output is correct |
20 |
Correct |
8 ms |
16744 KB |
Output is correct |
21 |
Correct |
9 ms |
16724 KB |
Output is correct |
22 |
Correct |
8 ms |
16724 KB |
Output is correct |
23 |
Correct |
8 ms |
16724 KB |
Output is correct |
24 |
Correct |
8 ms |
16724 KB |
Output is correct |
25 |
Correct |
11 ms |
16724 KB |
Output is correct |
26 |
Correct |
8 ms |
16792 KB |
Output is correct |
27 |
Correct |
8 ms |
16720 KB |
Output is correct |
28 |
Correct |
8 ms |
16724 KB |
Output is correct |
29 |
Correct |
8 ms |
16724 KB |
Output is correct |
30 |
Correct |
8 ms |
16724 KB |
Output is correct |
31 |
Correct |
9 ms |
16724 KB |
Output is correct |
32 |
Correct |
8 ms |
16724 KB |
Output is correct |
33 |
Correct |
9 ms |
16724 KB |
Output is correct |
34 |
Correct |
10 ms |
16852 KB |
Output is correct |
35 |
Correct |
79 ms |
20172 KB |
Output is correct |
36 |
Correct |
100 ms |
22516 KB |
Output is correct |
37 |
Correct |
103 ms |
22640 KB |
Output is correct |
38 |
Correct |
98 ms |
22248 KB |
Output is correct |
39 |
Correct |
9 ms |
16900 KB |
Output is correct |
40 |
Correct |
11 ms |
16928 KB |
Output is correct |
41 |
Correct |
12 ms |
16984 KB |
Output is correct |
42 |
Correct |
10 ms |
16852 KB |
Output is correct |
43 |
Correct |
11 ms |
16980 KB |
Output is correct |
44 |
Correct |
11 ms |
16980 KB |
Output is correct |
45 |
Correct |
11 ms |
16852 KB |
Output is correct |
46 |
Correct |
12 ms |
16852 KB |
Output is correct |
47 |
Correct |
11 ms |
16956 KB |
Output is correct |
48 |
Correct |
11 ms |
16992 KB |
Output is correct |
49 |
Correct |
17 ms |
17620 KB |
Output is correct |
50 |
Correct |
102 ms |
22712 KB |
Output is correct |
51 |
Correct |
14 ms |
17224 KB |
Output is correct |
52 |
Correct |
88 ms |
21068 KB |
Output is correct |
53 |
Correct |
16 ms |
17532 KB |
Output is correct |
54 |
Correct |
93 ms |
21884 KB |
Output is correct |
55 |
Correct |
13 ms |
17364 KB |
Output is correct |
56 |
Correct |
12 ms |
17364 KB |
Output is correct |
57 |
Correct |
13 ms |
17364 KB |
Output is correct |
58 |
Correct |
13 ms |
17372 KB |
Output is correct |
59 |
Correct |
10 ms |
16852 KB |
Output is correct |
60 |
Correct |
76 ms |
19252 KB |
Output is correct |
61 |
Correct |
12 ms |
17040 KB |
Output is correct |
62 |
Correct |
100 ms |
22048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
16724 KB |
Output is correct |
2 |
Correct |
8 ms |
16660 KB |
Output is correct |
3 |
Correct |
8 ms |
16728 KB |
Output is correct |
4 |
Correct |
9 ms |
16724 KB |
Output is correct |
5 |
Correct |
10 ms |
16724 KB |
Output is correct |
6 |
Correct |
8 ms |
16724 KB |
Output is correct |
7 |
Correct |
8 ms |
16780 KB |
Output is correct |
8 |
Correct |
9 ms |
16724 KB |
Output is correct |
9 |
Correct |
9 ms |
16812 KB |
Output is correct |
10 |
Correct |
8 ms |
16700 KB |
Output is correct |
11 |
Correct |
10 ms |
16724 KB |
Output is correct |
12 |
Correct |
8 ms |
16700 KB |
Output is correct |
13 |
Correct |
9 ms |
16676 KB |
Output is correct |
14 |
Correct |
8 ms |
16784 KB |
Output is correct |
15 |
Correct |
8 ms |
16724 KB |
Output is correct |
16 |
Correct |
8 ms |
16696 KB |
Output is correct |
17 |
Correct |
8 ms |
16736 KB |
Output is correct |
18 |
Correct |
8 ms |
16724 KB |
Output is correct |
19 |
Correct |
8 ms |
16724 KB |
Output is correct |
20 |
Correct |
8 ms |
16744 KB |
Output is correct |
21 |
Correct |
9 ms |
16724 KB |
Output is correct |
22 |
Correct |
8 ms |
16724 KB |
Output is correct |
23 |
Correct |
8 ms |
16724 KB |
Output is correct |
24 |
Correct |
8 ms |
16724 KB |
Output is correct |
25 |
Correct |
11 ms |
16724 KB |
Output is correct |
26 |
Correct |
8 ms |
16792 KB |
Output is correct |
27 |
Correct |
8 ms |
16720 KB |
Output is correct |
28 |
Correct |
8 ms |
16724 KB |
Output is correct |
29 |
Correct |
8 ms |
16724 KB |
Output is correct |
30 |
Correct |
8 ms |
16724 KB |
Output is correct |
31 |
Correct |
9 ms |
16724 KB |
Output is correct |
32 |
Correct |
8 ms |
16724 KB |
Output is correct |
33 |
Correct |
9 ms |
16724 KB |
Output is correct |
34 |
Correct |
10 ms |
16852 KB |
Output is correct |
35 |
Correct |
79 ms |
20172 KB |
Output is correct |
36 |
Correct |
100 ms |
22516 KB |
Output is correct |
37 |
Correct |
103 ms |
22640 KB |
Output is correct |
38 |
Correct |
98 ms |
22248 KB |
Output is correct |
39 |
Correct |
9 ms |
16900 KB |
Output is correct |
40 |
Correct |
11 ms |
16928 KB |
Output is correct |
41 |
Correct |
12 ms |
16984 KB |
Output is correct |
42 |
Correct |
10 ms |
16852 KB |
Output is correct |
43 |
Correct |
11 ms |
16980 KB |
Output is correct |
44 |
Correct |
11 ms |
16980 KB |
Output is correct |
45 |
Correct |
11 ms |
16852 KB |
Output is correct |
46 |
Correct |
12 ms |
16852 KB |
Output is correct |
47 |
Correct |
11 ms |
16956 KB |
Output is correct |
48 |
Correct |
11 ms |
16992 KB |
Output is correct |
49 |
Correct |
17 ms |
17620 KB |
Output is correct |
50 |
Correct |
102 ms |
22712 KB |
Output is correct |
51 |
Correct |
14 ms |
17224 KB |
Output is correct |
52 |
Correct |
88 ms |
21068 KB |
Output is correct |
53 |
Correct |
16 ms |
17532 KB |
Output is correct |
54 |
Correct |
93 ms |
21884 KB |
Output is correct |
55 |
Correct |
13 ms |
17364 KB |
Output is correct |
56 |
Correct |
12 ms |
17364 KB |
Output is correct |
57 |
Correct |
13 ms |
17364 KB |
Output is correct |
58 |
Correct |
13 ms |
17372 KB |
Output is correct |
59 |
Correct |
10 ms |
16852 KB |
Output is correct |
60 |
Correct |
76 ms |
19252 KB |
Output is correct |
61 |
Correct |
12 ms |
17040 KB |
Output is correct |
62 |
Correct |
100 ms |
22048 KB |
Output is correct |
63 |
Correct |
466 ms |
64536 KB |
Output is correct |
64 |
Correct |
462 ms |
64716 KB |
Output is correct |
65 |
Correct |
464 ms |
64672 KB |
Output is correct |
66 |
Correct |
114 ms |
22852 KB |
Output is correct |
67 |
Correct |
255 ms |
30588 KB |
Output is correct |
68 |
Correct |
114 ms |
22852 KB |
Output is correct |
69 |
Correct |
345 ms |
29820 KB |
Output is correct |
70 |
Correct |
111 ms |
22832 KB |
Output is correct |
71 |
Correct |
109 ms |
22904 KB |
Output is correct |
72 |
Correct |
316 ms |
29796 KB |
Output is correct |
73 |
Correct |
315 ms |
29920 KB |
Output is correct |
74 |
Correct |
814 ms |
39676 KB |
Output is correct |
75 |
Correct |
470 ms |
34504 KB |
Output is correct |
76 |
Correct |
651 ms |
39040 KB |
Output is correct |
77 |
Correct |
671 ms |
39228 KB |
Output is correct |
78 |
Correct |
164 ms |
28920 KB |
Output is correct |
79 |
Correct |
270 ms |
30756 KB |
Output is correct |
80 |
Correct |
170 ms |
29004 KB |
Output is correct |
81 |
Correct |
279 ms |
30796 KB |
Output is correct |
82 |
Correct |
774 ms |
50488 KB |
Output is correct |
83 |
Correct |
780 ms |
50380 KB |
Output is correct |
84 |
Correct |
528 ms |
50484 KB |
Output is correct |
85 |
Correct |
528 ms |
50348 KB |
Output is correct |
86 |
Correct |
150 ms |
27336 KB |
Output is correct |
87 |
Correct |
192 ms |
28276 KB |
Output is correct |
88 |
Correct |
337 ms |
29780 KB |
Output is correct |
89 |
Correct |
609 ms |
38832 KB |
Output is correct |