// O O O O O O O O O O O O O O O OO O OO O OO O O O TTCH O TTTCH O TTTCH O O O O
#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx")
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <stdio.h>
#include <cstdio>
#include <math.h>
#include <cmath>
#include <string>
#include <cstring>
#include <queue>
#include <deque>
// #include <random>
#include <iomanip>
#include <bitset>
#include <cassert>
using namespace std;
#define int long long
#define y1 y11
#define double long double
#define less less228
#define left left228
#define right right228
#define list list228
#define all(v) v.begin(), v.end()
template<typename T> void uin(T &a, T b) {
if (b < a) a = b;
}
template<typename T> void uax(T &a, T b) {
if (b > a) a = b;
}
const int N = 100 * 1000 + 228;
int p[N], sz[N];
vector<int> in[N];
void init(int n) {
for (int i = 1; i <= n; ++i) {
p[i] = i;
sz[i] = 1;
in[i].emplace_back(i);
}
}
int getp(int v){
if(v==p[v])return v;
p[v]=getp(p[v]);
return p[v];
}
int n,m;
int ans = 0;
map< pair<int,int>, bool> is;
map< pair<int,int>,int> between;
set<int> vic[N], vouc[N];
void squeeze(int c1, int c2) {
ans -= between[{c1, c2}] * sz[c2];
ans -= between[{c2, c1}] * sz[c1];
ans += 2LL * sz[c1] * sz[c2];
if (sz[c1] > sz[c2]) swap(c1, c2);
for (int x : in[c1]) {
if (vic[c2].count(x)) {
vic[c2].erase(x);
vouc[x].erase(c2);
}
}
int izn = (int)vic[c2].size();
vector<int> suspicion;
int cnt = 0;
for (int x : vic[c1]) {
if (getp(x) == c2) continue;
if (!vic[c2].count(x)) {
vic[c2].insert(x);
vouc[x].insert(c2);
ans += sz[c2];
++between[{getp(x), c2}];
suspicion.emplace_back(getp(x));
} else ++cnt;
is[{getp(x), c2}] = 1;
}
ans += (izn - cnt) * sz[c1];
for (int x : in[c1]) {
for (auto y : vouc[x]) {
if (y == c2 || y == c1) continue;
++between[{c2, y}];
is[{c2, y}] = 1;
suspicion.emplace_back(y);
}
}
p[c1] = c2;
sz[c2] += sz[c1];
for (int x : in[c1]) {
in[c2].emplace_back(x);
}
for (int c : suspicion) {
if (c != c2 && c != c1 && is[{getp(c2), getp(c)}] && is[{getp(c), getp(c2)}]) {
squeeze(getp(c), getp(c2));
}
}
}
void add(int a, int b) {
int c1 = getp(a);
int c2 = getp(b);
if (c1 == c2)return;
// c1 --> c2
if (!is[{c2, c1}]) {
is[{c1, c2}] = 1;
if (!vic[c2].count(a)) {
++between[{c1, c2}];
ans += sz[c2];
vic[c2].insert(a);
vouc[a].insert(c2);
}
} else {
squeeze(c1, c2);
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
init(n);
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
add(u, v);
cout << ans << '\n';
}
return 0;
}
// RU_023
/*
4 6
1 2
2 3
3 2
1 3
3 4
4 3
4 6
3 1
4 1
2 4
2 1
1 4
1 2
(9)
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
12160 KB |
Output is correct |
2 |
Correct |
11 ms |
12160 KB |
Output is correct |
3 |
Correct |
11 ms |
12160 KB |
Output is correct |
4 |
Correct |
11 ms |
12160 KB |
Output is correct |
5 |
Correct |
11 ms |
12160 KB |
Output is correct |
6 |
Correct |
12 ms |
12160 KB |
Output is correct |
7 |
Correct |
12 ms |
12160 KB |
Output is correct |
8 |
Correct |
12 ms |
12160 KB |
Output is correct |
9 |
Correct |
12 ms |
12160 KB |
Output is correct |
10 |
Correct |
11 ms |
12160 KB |
Output is correct |
11 |
Correct |
12 ms |
12160 KB |
Output is correct |
12 |
Correct |
11 ms |
12160 KB |
Output is correct |
13 |
Correct |
11 ms |
12160 KB |
Output is correct |
14 |
Correct |
11 ms |
12160 KB |
Output is correct |
15 |
Correct |
11 ms |
12160 KB |
Output is correct |
16 |
Correct |
11 ms |
12160 KB |
Output is correct |
17 |
Correct |
11 ms |
12160 KB |
Output is correct |
18 |
Correct |
11 ms |
12160 KB |
Output is correct |
19 |
Correct |
13 ms |
12160 KB |
Output is correct |
20 |
Correct |
11 ms |
12160 KB |
Output is correct |
21 |
Correct |
12 ms |
12160 KB |
Output is correct |
22 |
Correct |
11 ms |
12160 KB |
Output is correct |
23 |
Correct |
12 ms |
12160 KB |
Output is correct |
24 |
Correct |
12 ms |
12160 KB |
Output is correct |
25 |
Correct |
12 ms |
12160 KB |
Output is correct |
26 |
Correct |
11 ms |
12160 KB |
Output is correct |
27 |
Correct |
11 ms |
12160 KB |
Output is correct |
28 |
Correct |
13 ms |
12160 KB |
Output is correct |
29 |
Correct |
11 ms |
12160 KB |
Output is correct |
30 |
Correct |
11 ms |
12160 KB |
Output is correct |
31 |
Correct |
12 ms |
12160 KB |
Output is correct |
32 |
Correct |
11 ms |
12160 KB |
Output is correct |
33 |
Correct |
12 ms |
12160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
12160 KB |
Output is correct |
2 |
Correct |
11 ms |
12160 KB |
Output is correct |
3 |
Correct |
11 ms |
12160 KB |
Output is correct |
4 |
Correct |
11 ms |
12160 KB |
Output is correct |
5 |
Correct |
11 ms |
12160 KB |
Output is correct |
6 |
Correct |
12 ms |
12160 KB |
Output is correct |
7 |
Correct |
12 ms |
12160 KB |
Output is correct |
8 |
Correct |
12 ms |
12160 KB |
Output is correct |
9 |
Correct |
12 ms |
12160 KB |
Output is correct |
10 |
Correct |
11 ms |
12160 KB |
Output is correct |
11 |
Correct |
12 ms |
12160 KB |
Output is correct |
12 |
Correct |
11 ms |
12160 KB |
Output is correct |
13 |
Correct |
11 ms |
12160 KB |
Output is correct |
14 |
Correct |
11 ms |
12160 KB |
Output is correct |
15 |
Correct |
11 ms |
12160 KB |
Output is correct |
16 |
Correct |
11 ms |
12160 KB |
Output is correct |
17 |
Correct |
11 ms |
12160 KB |
Output is correct |
18 |
Correct |
11 ms |
12160 KB |
Output is correct |
19 |
Correct |
13 ms |
12160 KB |
Output is correct |
20 |
Correct |
11 ms |
12160 KB |
Output is correct |
21 |
Correct |
12 ms |
12160 KB |
Output is correct |
22 |
Correct |
11 ms |
12160 KB |
Output is correct |
23 |
Correct |
12 ms |
12160 KB |
Output is correct |
24 |
Correct |
12 ms |
12160 KB |
Output is correct |
25 |
Correct |
12 ms |
12160 KB |
Output is correct |
26 |
Correct |
11 ms |
12160 KB |
Output is correct |
27 |
Correct |
11 ms |
12160 KB |
Output is correct |
28 |
Correct |
13 ms |
12160 KB |
Output is correct |
29 |
Correct |
11 ms |
12160 KB |
Output is correct |
30 |
Correct |
11 ms |
12160 KB |
Output is correct |
31 |
Correct |
12 ms |
12160 KB |
Output is correct |
32 |
Correct |
11 ms |
12160 KB |
Output is correct |
33 |
Correct |
12 ms |
12160 KB |
Output is correct |
34 |
Correct |
14 ms |
12288 KB |
Output is correct |
35 |
Correct |
102 ms |
17656 KB |
Output is correct |
36 |
Correct |
179 ms |
23032 KB |
Output is correct |
37 |
Correct |
172 ms |
22904 KB |
Output is correct |
38 |
Correct |
160 ms |
22136 KB |
Output is correct |
39 |
Correct |
17 ms |
12928 KB |
Output is correct |
40 |
Correct |
23 ms |
14080 KB |
Output is correct |
41 |
Correct |
25 ms |
13952 KB |
Output is correct |
42 |
Correct |
16 ms |
12928 KB |
Output is correct |
43 |
Correct |
17 ms |
12928 KB |
Output is correct |
44 |
Correct |
17 ms |
12928 KB |
Output is correct |
45 |
Correct |
15 ms |
12928 KB |
Output is correct |
46 |
Correct |
15 ms |
12928 KB |
Output is correct |
47 |
Correct |
24 ms |
13568 KB |
Output is correct |
48 |
Correct |
22 ms |
13568 KB |
Output is correct |
49 |
Correct |
36 ms |
15480 KB |
Output is correct |
50 |
Correct |
172 ms |
23160 KB |
Output is correct |
51 |
Correct |
25 ms |
13568 KB |
Output is correct |
52 |
Correct |
126 ms |
19448 KB |
Output is correct |
53 |
Correct |
34 ms |
15096 KB |
Output is correct |
54 |
Correct |
143 ms |
21232 KB |
Output is correct |
55 |
Correct |
25 ms |
13952 KB |
Output is correct |
56 |
Correct |
24 ms |
13952 KB |
Output is correct |
57 |
Correct |
25 ms |
14336 KB |
Output is correct |
58 |
Correct |
25 ms |
14336 KB |
Output is correct |
59 |
Correct |
24 ms |
14208 KB |
Output is correct |
60 |
Correct |
98 ms |
16504 KB |
Output is correct |
61 |
Correct |
23 ms |
13696 KB |
Output is correct |
62 |
Correct |
151 ms |
21752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
12160 KB |
Output is correct |
2 |
Correct |
11 ms |
12160 KB |
Output is correct |
3 |
Correct |
11 ms |
12160 KB |
Output is correct |
4 |
Correct |
11 ms |
12160 KB |
Output is correct |
5 |
Correct |
11 ms |
12160 KB |
Output is correct |
6 |
Correct |
12 ms |
12160 KB |
Output is correct |
7 |
Correct |
12 ms |
12160 KB |
Output is correct |
8 |
Correct |
12 ms |
12160 KB |
Output is correct |
9 |
Correct |
12 ms |
12160 KB |
Output is correct |
10 |
Correct |
11 ms |
12160 KB |
Output is correct |
11 |
Correct |
12 ms |
12160 KB |
Output is correct |
12 |
Correct |
11 ms |
12160 KB |
Output is correct |
13 |
Correct |
11 ms |
12160 KB |
Output is correct |
14 |
Correct |
11 ms |
12160 KB |
Output is correct |
15 |
Correct |
11 ms |
12160 KB |
Output is correct |
16 |
Correct |
11 ms |
12160 KB |
Output is correct |
17 |
Correct |
11 ms |
12160 KB |
Output is correct |
18 |
Correct |
11 ms |
12160 KB |
Output is correct |
19 |
Correct |
13 ms |
12160 KB |
Output is correct |
20 |
Correct |
11 ms |
12160 KB |
Output is correct |
21 |
Correct |
12 ms |
12160 KB |
Output is correct |
22 |
Correct |
11 ms |
12160 KB |
Output is correct |
23 |
Correct |
12 ms |
12160 KB |
Output is correct |
24 |
Correct |
12 ms |
12160 KB |
Output is correct |
25 |
Correct |
12 ms |
12160 KB |
Output is correct |
26 |
Correct |
11 ms |
12160 KB |
Output is correct |
27 |
Correct |
11 ms |
12160 KB |
Output is correct |
28 |
Correct |
13 ms |
12160 KB |
Output is correct |
29 |
Correct |
11 ms |
12160 KB |
Output is correct |
30 |
Correct |
11 ms |
12160 KB |
Output is correct |
31 |
Correct |
12 ms |
12160 KB |
Output is correct |
32 |
Correct |
11 ms |
12160 KB |
Output is correct |
33 |
Correct |
12 ms |
12160 KB |
Output is correct |
34 |
Correct |
14 ms |
12288 KB |
Output is correct |
35 |
Correct |
102 ms |
17656 KB |
Output is correct |
36 |
Correct |
179 ms |
23032 KB |
Output is correct |
37 |
Correct |
172 ms |
22904 KB |
Output is correct |
38 |
Correct |
160 ms |
22136 KB |
Output is correct |
39 |
Correct |
17 ms |
12928 KB |
Output is correct |
40 |
Correct |
23 ms |
14080 KB |
Output is correct |
41 |
Correct |
25 ms |
13952 KB |
Output is correct |
42 |
Correct |
16 ms |
12928 KB |
Output is correct |
43 |
Correct |
17 ms |
12928 KB |
Output is correct |
44 |
Correct |
17 ms |
12928 KB |
Output is correct |
45 |
Correct |
15 ms |
12928 KB |
Output is correct |
46 |
Correct |
15 ms |
12928 KB |
Output is correct |
47 |
Correct |
24 ms |
13568 KB |
Output is correct |
48 |
Correct |
22 ms |
13568 KB |
Output is correct |
49 |
Correct |
36 ms |
15480 KB |
Output is correct |
50 |
Correct |
172 ms |
23160 KB |
Output is correct |
51 |
Correct |
25 ms |
13568 KB |
Output is correct |
52 |
Correct |
126 ms |
19448 KB |
Output is correct |
53 |
Correct |
34 ms |
15096 KB |
Output is correct |
54 |
Correct |
143 ms |
21232 KB |
Output is correct |
55 |
Correct |
25 ms |
13952 KB |
Output is correct |
56 |
Correct |
24 ms |
13952 KB |
Output is correct |
57 |
Correct |
25 ms |
14336 KB |
Output is correct |
58 |
Correct |
25 ms |
14336 KB |
Output is correct |
59 |
Correct |
24 ms |
14208 KB |
Output is correct |
60 |
Correct |
98 ms |
16504 KB |
Output is correct |
61 |
Correct |
23 ms |
13696 KB |
Output is correct |
62 |
Correct |
151 ms |
21752 KB |
Output is correct |
63 |
Correct |
1382 ms |
103800 KB |
Output is correct |
64 |
Correct |
1384 ms |
103488 KB |
Output is correct |
65 |
Correct |
1379 ms |
103416 KB |
Output is correct |
66 |
Correct |
450 ms |
54376 KB |
Output is correct |
67 |
Correct |
1818 ms |
126764 KB |
Output is correct |
68 |
Correct |
461 ms |
54216 KB |
Output is correct |
69 |
Correct |
755 ms |
50156 KB |
Output is correct |
70 |
Correct |
450 ms |
54124 KB |
Output is correct |
71 |
Correct |
472 ms |
54248 KB |
Output is correct |
72 |
Correct |
1533 ms |
87016 KB |
Output is correct |
73 |
Correct |
1557 ms |
90472 KB |
Output is correct |
74 |
Correct |
3068 ms |
172040 KB |
Output is correct |
75 |
Correct |
1245 ms |
64488 KB |
Output is correct |
76 |
Correct |
2162 ms |
112624 KB |
Output is correct |
77 |
Correct |
2181 ms |
113264 KB |
Output is correct |
78 |
Correct |
714 ms |
64600 KB |
Output is correct |
79 |
Correct |
1034 ms |
75456 KB |
Output is correct |
80 |
Correct |
722 ms |
66904 KB |
Output is correct |
81 |
Correct |
1022 ms |
72284 KB |
Output is correct |
82 |
Correct |
2025 ms |
101748 KB |
Output is correct |
83 |
Correct |
2001 ms |
101748 KB |
Output is correct |
84 |
Correct |
1789 ms |
125416 KB |
Output is correct |
85 |
Correct |
1790 ms |
125380 KB |
Output is correct |
86 |
Correct |
1465 ms |
151368 KB |
Output is correct |
87 |
Correct |
1492 ms |
151968 KB |
Output is correct |
88 |
Correct |
1566 ms |
89580 KB |
Output is correct |
89 |
Correct |
2102 ms |
107088 KB |
Output is correct |