//#pragma GCC optimize("Ofast,O3")
//#pragma GCC target("avx,avx2")
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debugger.cpp"
#else
#define debug(...)
#endif
template<class T>
int len(T &a){
return a.size();
}
using ll = long long;
using pii = pair<int,int>;
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
string fileio = "";
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const int MOD = 1e9 + 7;
const int inf = 1e9;
const ll infl = 1e18;
const int maxn = 1e5 + 1;
struct DSU{
vector<int> par, sz; int n;
DSU (int _n){
n = _n;
par.resize(n);
iota(all(par), 0);
sz.assign(n, 1);
}
DSU (){}
int get(int a){
return (par[a] == a ? a : par[a] = get(par[a]));
}
void init(int _n){
n = _n;
par.resize(n);
iota(all(par), 0);
sz.assign(n, 1);
}
void unite(int a, int b){
a = get(a);
b = get(b);
if(a == b) return;
if(sz[a] < sz[b]) swap(a,b);
sz[a] += sz[b];
par[b] = a;
}
void link(int a, int b){
a = get(a); b = get(b);
if(a == b) return;
sz[b] += sz[a];
par[a] = b;
}
};
void solve(){
int n, m; cin >> n >> m;
vector<set<int>> half(n), rhalf(n);
vector<set<int>> lead(n);
for(int i = 0; i < n; i ++) lead[i].insert(i);
DSU t(n);
ll ans = 0;
queue<pair<int,int>> q;
auto half_edge = [&](int a, int b){
//debug(a + 1, b + 1);
half[a].insert(b);
rhalf[b].insert(a);
if(half[b].count(a)){
q.push({a, b});
}
};
while(m --){
int a, b; cin >> a >> b; a --; b --;
int la = t.get(a), lb = t.get(b);
if(la == lb || lead[lb].count(a)){
cout << ans << '\n';
continue;
}
lead[lb].insert(a);
half_edge(la, lb);
ans += t.sz[lb];
while(!q.empty()){
tie(a, b) = q.front(); q.pop();
a = t.get(a), b = t.get(b);
if(a == b) continue;
ans += 1ll * t.sz[a] * len(lead[b]) + 1ll * t.sz[b] * len(lead[a]);
if(len(lead[a]) > len(lead[b])) swap(a, b);
t.link(a, b);
for(auto u : lead[a]){
if(lead[b].count(u)) ans -= t.sz[b];
else lead[b].insert(u);
}
lead[a].clear();
half[a].erase(b); half[b].erase(a);
rhalf[b].erase(a); rhalf[a].erase(b);
for(auto i : half[a]){
rhalf[i].erase(b);
half_edge(b, i);
}
for(auto i : rhalf[a]){
half[i].erase(a);
half_edge(i, b);
}
}
cout << ans << '\n';
}
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
auto start = chrono::high_resolution_clock::now();
#ifndef LOCAL
if(fileio.size()){
freopen((fileio + ".in").c_str(), "r", stdin);
freopen((fileio + ".out").c_str(), "w", stdout);
}
#endif
int testcases = 1;
#ifdef Tests
cin >> testcases;
#endif
while(testcases --){
solve();
if(testcases) cout << '\n';
#ifdef LOCAL
else cout << '\n';
cout << "_________________________" << endl;
#endif
}
#ifdef LOCAL
auto duration = chrono::duration_cast<chrono::microseconds>(chrono::high_resolution_clock::now() - start);
cout << "time: " << (double)duration.count()/1000.0 << " milliseconds" << endl;
#endif
return 0;
}
Compilation message
joitter2.cpp: In function 'int main()':
joitter2.cpp:138:10: warning: variable 'start' set but not used [-Wunused-but-set-variable]
138 | auto start = chrono::high_resolution_clock::now();
| ^~~~~
joitter2.cpp:141:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
141 | freopen((fileio + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
joitter2.cpp:142:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
142 | freopen((fileio + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
456 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
424 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
0 ms |
600 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
456 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
424 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
0 ms |
600 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
2 ms |
604 KB |
Output is correct |
35 |
Correct |
53 ms |
6272 KB |
Output is correct |
36 |
Correct |
80 ms |
9780 KB |
Output is correct |
37 |
Correct |
76 ms |
9300 KB |
Output is correct |
38 |
Correct |
80 ms |
8952 KB |
Output is correct |
39 |
Correct |
2 ms |
856 KB |
Output is correct |
40 |
Correct |
4 ms |
1116 KB |
Output is correct |
41 |
Correct |
4 ms |
1116 KB |
Output is correct |
42 |
Correct |
2 ms |
860 KB |
Output is correct |
43 |
Correct |
3 ms |
860 KB |
Output is correct |
44 |
Correct |
3 ms |
860 KB |
Output is correct |
45 |
Correct |
3 ms |
856 KB |
Output is correct |
46 |
Correct |
2 ms |
1112 KB |
Output is correct |
47 |
Correct |
4 ms |
860 KB |
Output is correct |
48 |
Correct |
4 ms |
860 KB |
Output is correct |
49 |
Correct |
10 ms |
1876 KB |
Output is correct |
50 |
Correct |
78 ms |
9380 KB |
Output is correct |
51 |
Correct |
6 ms |
1368 KB |
Output is correct |
52 |
Correct |
79 ms |
7684 KB |
Output is correct |
53 |
Correct |
9 ms |
1880 KB |
Output is correct |
54 |
Correct |
70 ms |
8532 KB |
Output is correct |
55 |
Correct |
5 ms |
1372 KB |
Output is correct |
56 |
Correct |
5 ms |
1372 KB |
Output is correct |
57 |
Correct |
5 ms |
1372 KB |
Output is correct |
58 |
Correct |
6 ms |
1372 KB |
Output is correct |
59 |
Correct |
3 ms |
860 KB |
Output is correct |
60 |
Correct |
50 ms |
5892 KB |
Output is correct |
61 |
Correct |
4 ms |
1112 KB |
Output is correct |
62 |
Correct |
73 ms |
8788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
456 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
424 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
0 ms |
600 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
2 ms |
604 KB |
Output is correct |
35 |
Correct |
53 ms |
6272 KB |
Output is correct |
36 |
Correct |
80 ms |
9780 KB |
Output is correct |
37 |
Correct |
76 ms |
9300 KB |
Output is correct |
38 |
Correct |
80 ms |
8952 KB |
Output is correct |
39 |
Correct |
2 ms |
856 KB |
Output is correct |
40 |
Correct |
4 ms |
1116 KB |
Output is correct |
41 |
Correct |
4 ms |
1116 KB |
Output is correct |
42 |
Correct |
2 ms |
860 KB |
Output is correct |
43 |
Correct |
3 ms |
860 KB |
Output is correct |
44 |
Correct |
3 ms |
860 KB |
Output is correct |
45 |
Correct |
3 ms |
856 KB |
Output is correct |
46 |
Correct |
2 ms |
1112 KB |
Output is correct |
47 |
Correct |
4 ms |
860 KB |
Output is correct |
48 |
Correct |
4 ms |
860 KB |
Output is correct |
49 |
Correct |
10 ms |
1876 KB |
Output is correct |
50 |
Correct |
78 ms |
9380 KB |
Output is correct |
51 |
Correct |
6 ms |
1368 KB |
Output is correct |
52 |
Correct |
79 ms |
7684 KB |
Output is correct |
53 |
Correct |
9 ms |
1880 KB |
Output is correct |
54 |
Correct |
70 ms |
8532 KB |
Output is correct |
55 |
Correct |
5 ms |
1372 KB |
Output is correct |
56 |
Correct |
5 ms |
1372 KB |
Output is correct |
57 |
Correct |
5 ms |
1372 KB |
Output is correct |
58 |
Correct |
6 ms |
1372 KB |
Output is correct |
59 |
Correct |
3 ms |
860 KB |
Output is correct |
60 |
Correct |
50 ms |
5892 KB |
Output is correct |
61 |
Correct |
4 ms |
1112 KB |
Output is correct |
62 |
Correct |
73 ms |
8788 KB |
Output is correct |
63 |
Correct |
507 ms |
67616 KB |
Output is correct |
64 |
Correct |
479 ms |
67668 KB |
Output is correct |
65 |
Correct |
511 ms |
67744 KB |
Output is correct |
66 |
Correct |
183 ms |
24320 KB |
Output is correct |
67 |
Correct |
512 ms |
41340 KB |
Output is correct |
68 |
Correct |
153 ms |
24400 KB |
Output is correct |
69 |
Correct |
290 ms |
31268 KB |
Output is correct |
70 |
Correct |
165 ms |
24400 KB |
Output is correct |
71 |
Correct |
164 ms |
24400 KB |
Output is correct |
72 |
Correct |
449 ms |
32608 KB |
Output is correct |
73 |
Correct |
405 ms |
32376 KB |
Output is correct |
74 |
Correct |
1268 ms |
62988 KB |
Output is correct |
75 |
Correct |
552 ms |
38488 KB |
Output is correct |
76 |
Correct |
840 ms |
48976 KB |
Output is correct |
77 |
Correct |
824 ms |
49504 KB |
Output is correct |
78 |
Correct |
232 ms |
35976 KB |
Output is correct |
79 |
Correct |
409 ms |
39276 KB |
Output is correct |
80 |
Correct |
191 ms |
33756 KB |
Output is correct |
81 |
Correct |
336 ms |
36840 KB |
Output is correct |
82 |
Correct |
886 ms |
53256 KB |
Output is correct |
83 |
Correct |
854 ms |
53012 KB |
Output is correct |
84 |
Correct |
639 ms |
52308 KB |
Output is correct |
85 |
Correct |
662 ms |
52052 KB |
Output is correct |
86 |
Correct |
270 ms |
25192 KB |
Output is correct |
87 |
Correct |
267 ms |
27348 KB |
Output is correct |
88 |
Correct |
448 ms |
32756 KB |
Output is correct |
89 |
Correct |
851 ms |
47076 KB |
Output is correct |