// In the name of Allah
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define all(x) (x).begin(),(x).end()
#define len(x) ((ll) (x).size())
#define F first
#define S second
#define pb push_back
#define sep ' '
#define endl '\n'
#define Mp make_pair
#define kill(x) cout << x << '\n', exit(0)
#define set_dec(x) cout << fixed << setprecision(x);
#define file_io(x,y) freopen(x, "r", stdin); freopen(y, "w", stdout);
int n, q;
const int maxn = 4e5 + 4;
bool mark[maxn]; queue<int> qu;
int p[maxn]; ll val[maxn]; vector<pii> E;
vector<int> ls[maxn]; set<int> A[maxn];
set<int> adj[maxn], adjr[maxn]; ll res = 0;
ll cal(ll sz, ll t) {
return (sz * t) + sz * (sz - 1);
}
void upd(int a) {
res -= val[a];
val[a] = cal(len(ls[a]), len(A[a]));
res += val[a];
}
int get(int a) {
return p[a];
}
bool ok(int a, int b) {
a = get(a); b = get(b);
if (a != b && adj[a].find(b) != adj[a].end() && adj[b].find(a) != adj[b].end()) return 1;
return 0;
}
void merge(int a1, int b1) {
qu.push(b1); mark[b1] = 1;
while (!qu.empty()) {
int b1 = qu.front(); mark[b1] = 0;
qu.pop();
int a = get(a1), b = get(b1);
if (a == b) continue;
if (len(ls[a]) > len(ls[b])) swap(a, b);
for (int v : A[a]) {
if (p[v] == a || p[v] == b) continue;
A[b].insert(v);
}
A[a].clear();
for (int v : ls[a]) {
p[v] = b; ls[b].pb(v);
A[b].erase(v);
}
ls[a].clear();
for (int v : adj[a]) {
if (v == a || v == b) continue;
adj[b].insert(v);
adjr[v].erase(a); adjr[v].insert(b);
}
for (int v : adjr[a]) {
if (v == a || v == b) continue;
adjr[b].insert(v);
adj[v].erase(a); adj[v].insert(b);
}
adj[b].erase(a); adjr[b].erase(a);
for (int v : adj[a]) {
if (ok(b, v) && !mark[v]) {
qu.push(v); mark[v] = 1;
}
}
for (int v : adjr[a]) {
if (ok(b, v) && !mark[v]) {
qu.push(v); mark[v] = 1;
}
}
adj[a].clear(); adjr[a].clear();
upd(a), upd(b);
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> q;
for (int i = 0; i < n; i++) {
p[i] = i; ls[i].pb(i);
}
for (int i = 0; i < q; i++) {
int u1, v1;
cin >> u1 >> v1; u1--; v1--;
E.pb(Mp(u1, v1));
int u = get(u1), v = get(v1);
if (u == v) {
cout << res << endl;
continue;
}
A[v].insert(u1);
upd(v);
adj[u].insert(v); adjr[v].insert(u);
if (ok(u, v)) merge(u, v);
cout << res << endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
68188 KB |
Output is correct |
2 |
Correct |
13 ms |
68352 KB |
Output is correct |
3 |
Correct |
14 ms |
68352 KB |
Output is correct |
4 |
Correct |
13 ms |
68604 KB |
Output is correct |
5 |
Correct |
13 ms |
68348 KB |
Output is correct |
6 |
Correct |
13 ms |
68352 KB |
Output is correct |
7 |
Correct |
13 ms |
68188 KB |
Output is correct |
8 |
Correct |
14 ms |
68288 KB |
Output is correct |
9 |
Correct |
15 ms |
68188 KB |
Output is correct |
10 |
Correct |
14 ms |
68188 KB |
Output is correct |
11 |
Correct |
16 ms |
68444 KB |
Output is correct |
12 |
Correct |
13 ms |
68188 KB |
Output is correct |
13 |
Correct |
14 ms |
68188 KB |
Output is correct |
14 |
Correct |
13 ms |
68184 KB |
Output is correct |
15 |
Correct |
13 ms |
68284 KB |
Output is correct |
16 |
Correct |
13 ms |
68184 KB |
Output is correct |
17 |
Correct |
14 ms |
68312 KB |
Output is correct |
18 |
Correct |
13 ms |
68184 KB |
Output is correct |
19 |
Correct |
14 ms |
68188 KB |
Output is correct |
20 |
Correct |
13 ms |
68188 KB |
Output is correct |
21 |
Correct |
13 ms |
68300 KB |
Output is correct |
22 |
Correct |
14 ms |
68188 KB |
Output is correct |
23 |
Correct |
14 ms |
68408 KB |
Output is correct |
24 |
Correct |
15 ms |
68380 KB |
Output is correct |
25 |
Correct |
15 ms |
68408 KB |
Output is correct |
26 |
Correct |
14 ms |
68188 KB |
Output is correct |
27 |
Correct |
15 ms |
68188 KB |
Output is correct |
28 |
Correct |
14 ms |
68188 KB |
Output is correct |
29 |
Correct |
13 ms |
68440 KB |
Output is correct |
30 |
Correct |
14 ms |
68188 KB |
Output is correct |
31 |
Correct |
15 ms |
68188 KB |
Output is correct |
32 |
Correct |
14 ms |
68352 KB |
Output is correct |
33 |
Correct |
13 ms |
68184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
68188 KB |
Output is correct |
2 |
Correct |
13 ms |
68352 KB |
Output is correct |
3 |
Correct |
14 ms |
68352 KB |
Output is correct |
4 |
Correct |
13 ms |
68604 KB |
Output is correct |
5 |
Correct |
13 ms |
68348 KB |
Output is correct |
6 |
Correct |
13 ms |
68352 KB |
Output is correct |
7 |
Correct |
13 ms |
68188 KB |
Output is correct |
8 |
Correct |
14 ms |
68288 KB |
Output is correct |
9 |
Correct |
15 ms |
68188 KB |
Output is correct |
10 |
Correct |
14 ms |
68188 KB |
Output is correct |
11 |
Correct |
16 ms |
68444 KB |
Output is correct |
12 |
Correct |
13 ms |
68188 KB |
Output is correct |
13 |
Correct |
14 ms |
68188 KB |
Output is correct |
14 |
Correct |
13 ms |
68184 KB |
Output is correct |
15 |
Correct |
13 ms |
68284 KB |
Output is correct |
16 |
Correct |
13 ms |
68184 KB |
Output is correct |
17 |
Correct |
14 ms |
68312 KB |
Output is correct |
18 |
Correct |
13 ms |
68184 KB |
Output is correct |
19 |
Correct |
14 ms |
68188 KB |
Output is correct |
20 |
Correct |
13 ms |
68188 KB |
Output is correct |
21 |
Correct |
13 ms |
68300 KB |
Output is correct |
22 |
Correct |
14 ms |
68188 KB |
Output is correct |
23 |
Correct |
14 ms |
68408 KB |
Output is correct |
24 |
Correct |
15 ms |
68380 KB |
Output is correct |
25 |
Correct |
15 ms |
68408 KB |
Output is correct |
26 |
Correct |
14 ms |
68188 KB |
Output is correct |
27 |
Correct |
15 ms |
68188 KB |
Output is correct |
28 |
Correct |
14 ms |
68188 KB |
Output is correct |
29 |
Correct |
13 ms |
68440 KB |
Output is correct |
30 |
Correct |
14 ms |
68188 KB |
Output is correct |
31 |
Correct |
15 ms |
68188 KB |
Output is correct |
32 |
Correct |
14 ms |
68352 KB |
Output is correct |
33 |
Correct |
13 ms |
68184 KB |
Output is correct |
34 |
Correct |
15 ms |
68696 KB |
Output is correct |
35 |
Correct |
76 ms |
78380 KB |
Output is correct |
36 |
Correct |
99 ms |
81340 KB |
Output is correct |
37 |
Correct |
89 ms |
80832 KB |
Output is correct |
38 |
Correct |
86 ms |
80732 KB |
Output is correct |
39 |
Correct |
14 ms |
68444 KB |
Output is correct |
40 |
Correct |
15 ms |
68444 KB |
Output is correct |
41 |
Correct |
17 ms |
68444 KB |
Output is correct |
42 |
Correct |
14 ms |
68440 KB |
Output is correct |
43 |
Correct |
16 ms |
68440 KB |
Output is correct |
44 |
Correct |
16 ms |
68444 KB |
Output is correct |
45 |
Correct |
15 ms |
68444 KB |
Output is correct |
46 |
Correct |
15 ms |
68444 KB |
Output is correct |
47 |
Correct |
16 ms |
68444 KB |
Output is correct |
48 |
Correct |
16 ms |
68444 KB |
Output is correct |
49 |
Correct |
21 ms |
69212 KB |
Output is correct |
50 |
Correct |
89 ms |
82356 KB |
Output is correct |
51 |
Correct |
19 ms |
68956 KB |
Output is correct |
52 |
Correct |
77 ms |
79656 KB |
Output is correct |
53 |
Correct |
21 ms |
69212 KB |
Output is correct |
54 |
Correct |
83 ms |
80680 KB |
Output is correct |
55 |
Correct |
18 ms |
68956 KB |
Output is correct |
56 |
Correct |
18 ms |
69084 KB |
Output is correct |
57 |
Correct |
19 ms |
68952 KB |
Output is correct |
58 |
Correct |
17 ms |
68956 KB |
Output is correct |
59 |
Correct |
16 ms |
68444 KB |
Output is correct |
60 |
Correct |
70 ms |
77760 KB |
Output is correct |
61 |
Correct |
19 ms |
68584 KB |
Output is correct |
62 |
Correct |
83 ms |
79816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
68188 KB |
Output is correct |
2 |
Correct |
13 ms |
68352 KB |
Output is correct |
3 |
Correct |
14 ms |
68352 KB |
Output is correct |
4 |
Correct |
13 ms |
68604 KB |
Output is correct |
5 |
Correct |
13 ms |
68348 KB |
Output is correct |
6 |
Correct |
13 ms |
68352 KB |
Output is correct |
7 |
Correct |
13 ms |
68188 KB |
Output is correct |
8 |
Correct |
14 ms |
68288 KB |
Output is correct |
9 |
Correct |
15 ms |
68188 KB |
Output is correct |
10 |
Correct |
14 ms |
68188 KB |
Output is correct |
11 |
Correct |
16 ms |
68444 KB |
Output is correct |
12 |
Correct |
13 ms |
68188 KB |
Output is correct |
13 |
Correct |
14 ms |
68188 KB |
Output is correct |
14 |
Correct |
13 ms |
68184 KB |
Output is correct |
15 |
Correct |
13 ms |
68284 KB |
Output is correct |
16 |
Correct |
13 ms |
68184 KB |
Output is correct |
17 |
Correct |
14 ms |
68312 KB |
Output is correct |
18 |
Correct |
13 ms |
68184 KB |
Output is correct |
19 |
Correct |
14 ms |
68188 KB |
Output is correct |
20 |
Correct |
13 ms |
68188 KB |
Output is correct |
21 |
Correct |
13 ms |
68300 KB |
Output is correct |
22 |
Correct |
14 ms |
68188 KB |
Output is correct |
23 |
Correct |
14 ms |
68408 KB |
Output is correct |
24 |
Correct |
15 ms |
68380 KB |
Output is correct |
25 |
Correct |
15 ms |
68408 KB |
Output is correct |
26 |
Correct |
14 ms |
68188 KB |
Output is correct |
27 |
Correct |
15 ms |
68188 KB |
Output is correct |
28 |
Correct |
14 ms |
68188 KB |
Output is correct |
29 |
Correct |
13 ms |
68440 KB |
Output is correct |
30 |
Correct |
14 ms |
68188 KB |
Output is correct |
31 |
Correct |
15 ms |
68188 KB |
Output is correct |
32 |
Correct |
14 ms |
68352 KB |
Output is correct |
33 |
Correct |
13 ms |
68184 KB |
Output is correct |
34 |
Correct |
15 ms |
68696 KB |
Output is correct |
35 |
Correct |
76 ms |
78380 KB |
Output is correct |
36 |
Correct |
99 ms |
81340 KB |
Output is correct |
37 |
Correct |
89 ms |
80832 KB |
Output is correct |
38 |
Correct |
86 ms |
80732 KB |
Output is correct |
39 |
Correct |
14 ms |
68444 KB |
Output is correct |
40 |
Correct |
15 ms |
68444 KB |
Output is correct |
41 |
Correct |
17 ms |
68444 KB |
Output is correct |
42 |
Correct |
14 ms |
68440 KB |
Output is correct |
43 |
Correct |
16 ms |
68440 KB |
Output is correct |
44 |
Correct |
16 ms |
68444 KB |
Output is correct |
45 |
Correct |
15 ms |
68444 KB |
Output is correct |
46 |
Correct |
15 ms |
68444 KB |
Output is correct |
47 |
Correct |
16 ms |
68444 KB |
Output is correct |
48 |
Correct |
16 ms |
68444 KB |
Output is correct |
49 |
Correct |
21 ms |
69212 KB |
Output is correct |
50 |
Correct |
89 ms |
82356 KB |
Output is correct |
51 |
Correct |
19 ms |
68956 KB |
Output is correct |
52 |
Correct |
77 ms |
79656 KB |
Output is correct |
53 |
Correct |
21 ms |
69212 KB |
Output is correct |
54 |
Correct |
83 ms |
80680 KB |
Output is correct |
55 |
Correct |
18 ms |
68956 KB |
Output is correct |
56 |
Correct |
18 ms |
69084 KB |
Output is correct |
57 |
Correct |
19 ms |
68952 KB |
Output is correct |
58 |
Correct |
17 ms |
68956 KB |
Output is correct |
59 |
Correct |
16 ms |
68444 KB |
Output is correct |
60 |
Correct |
70 ms |
77760 KB |
Output is correct |
61 |
Correct |
19 ms |
68584 KB |
Output is correct |
62 |
Correct |
83 ms |
79816 KB |
Output is correct |
63 |
Correct |
479 ms |
123860 KB |
Output is correct |
64 |
Correct |
463 ms |
123424 KB |
Output is correct |
65 |
Correct |
437 ms |
123928 KB |
Output is correct |
66 |
Correct |
113 ms |
79032 KB |
Output is correct |
67 |
Correct |
210 ms |
86940 KB |
Output is correct |
68 |
Correct |
103 ms |
78976 KB |
Output is correct |
69 |
Correct |
292 ms |
86024 KB |
Output is correct |
70 |
Correct |
109 ms |
78908 KB |
Output is correct |
71 |
Correct |
102 ms |
78956 KB |
Output is correct |
72 |
Correct |
264 ms |
85688 KB |
Output is correct |
73 |
Correct |
272 ms |
85960 KB |
Output is correct |
74 |
Correct |
701 ms |
98512 KB |
Output is correct |
75 |
Correct |
372 ms |
93664 KB |
Output is correct |
76 |
Correct |
579 ms |
99412 KB |
Output is correct |
77 |
Correct |
534 ms |
99860 KB |
Output is correct |
78 |
Correct |
173 ms |
86720 KB |
Output is correct |
79 |
Correct |
255 ms |
93100 KB |
Output is correct |
80 |
Correct |
174 ms |
86796 KB |
Output is correct |
81 |
Correct |
271 ms |
94140 KB |
Output is correct |
82 |
Correct |
573 ms |
106640 KB |
Output is correct |
83 |
Correct |
605 ms |
106516 KB |
Output is correct |
84 |
Correct |
483 ms |
106512 KB |
Output is correct |
85 |
Correct |
510 ms |
106432 KB |
Output is correct |
86 |
Correct |
138 ms |
83644 KB |
Output is correct |
87 |
Correct |
162 ms |
88712 KB |
Output is correct |
88 |
Correct |
323 ms |
85692 KB |
Output is correct |
89 |
Correct |
539 ms |
99004 KB |
Output is correct |