#include "bits/stdc++.h"
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...)
#endif
using ll = long long;
using pii = pair<int, int>;
#define F first
#define S second
#define sz(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll get_rand(ll l, ll r) {
assert(l <= r);
return uniform_int_distribution<ll> (l, r)(rng);
}
const int N = 1e5 + 3;
int n, m;
int p[N];
ll sz[N];
set<int> fl[N], rfl[N], comp[N];
queue<pii> to_merge;
ll ans;
int find(int x) { return x == p[x] ? x : p[x] = find(p[x]); }
void insert_weak(int u, int v){
fl[u].insert(v);
rfl[v].insert(u);
if(fl[v].count(u))
to_merge.push({u, v});
}
int dsu_sz(int u) { return sz(comp[u]) + sz(fl[u]) + sz(rfl[u]); }
void merge_set(int u, int v){
u = find(u), v = find(v);
if(u == v) return;
if(dsu_sz(u) < dsu_sz(v)) swap(u, v);
ans += (sz[v] * sz(comp[u])) + (sz[u] * sz(comp[v]));
p[v] = u;
sz[u] += sz[v];
for(int i : comp[v]){
if(comp[u].count(i)) ans -= sz[u];
else comp[u].insert(i);
}
fl[u].erase(v); rfl[v].erase(u);
fl[v].erase(u); rfl[u].erase(v);
for(int i : fl[v]){
rfl[i].erase(v);
insert_weak(u, i);
}
for(int i : rfl[v]){
fl[i].erase(v);
insert_weak(i, u);
}
}
void solve(){
cin >> n >> m;
for(int i = 1; i <= n; ++i){
sz[i] = 1, p[i] = i;
comp[i].insert(i);
}
while(m--){
int u, v;
cin >> u >> v;
v = find(v);
if(find(u) != v and !comp[v].count(u)){
comp[v].insert(u);
ans += sz[v];
u = find(u);
insert_weak(u, v);
while(!to_merge.empty()){
merge_set(to_merge.front().F, to_merge.front().S);
to_merge.pop();
}
}
cout << ans << '\n';
}
}
int32_t main() {
cin.tie(nullptr)->sync_with_stdio(0);
#define task "troll"
if(fopen(task".inp", "r")){
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int test = 1;
// cin >> test;
for(int i = 1; i <= test; ++i){
// cout << "Case #" << i << ": ";
solve();
}
#ifdef LOCAL
cerr << "\n[Time]: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms.\n";
#endif
return 0;
}
Compilation message
joitter2.cpp: In function 'int32_t main()':
joitter2.cpp:106:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
106 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
joitter2.cpp:107:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
107 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
15000 KB |
Output is correct |
2 |
Correct |
3 ms |
14936 KB |
Output is correct |
3 |
Correct |
3 ms |
14940 KB |
Output is correct |
4 |
Correct |
4 ms |
14940 KB |
Output is correct |
5 |
Correct |
3 ms |
14936 KB |
Output is correct |
6 |
Correct |
4 ms |
14940 KB |
Output is correct |
7 |
Correct |
4 ms |
14940 KB |
Output is correct |
8 |
Correct |
4 ms |
14940 KB |
Output is correct |
9 |
Correct |
4 ms |
14940 KB |
Output is correct |
10 |
Correct |
3 ms |
15004 KB |
Output is correct |
11 |
Correct |
3 ms |
14940 KB |
Output is correct |
12 |
Correct |
4 ms |
14940 KB |
Output is correct |
13 |
Correct |
3 ms |
14936 KB |
Output is correct |
14 |
Correct |
3 ms |
14940 KB |
Output is correct |
15 |
Correct |
3 ms |
14936 KB |
Output is correct |
16 |
Correct |
3 ms |
14936 KB |
Output is correct |
17 |
Correct |
4 ms |
14940 KB |
Output is correct |
18 |
Correct |
3 ms |
14940 KB |
Output is correct |
19 |
Correct |
3 ms |
14936 KB |
Output is correct |
20 |
Correct |
3 ms |
14940 KB |
Output is correct |
21 |
Correct |
4 ms |
14940 KB |
Output is correct |
22 |
Correct |
3 ms |
14940 KB |
Output is correct |
23 |
Correct |
3 ms |
14940 KB |
Output is correct |
24 |
Correct |
4 ms |
14936 KB |
Output is correct |
25 |
Correct |
3 ms |
14936 KB |
Output is correct |
26 |
Correct |
3 ms |
14940 KB |
Output is correct |
27 |
Correct |
3 ms |
14940 KB |
Output is correct |
28 |
Correct |
3 ms |
14940 KB |
Output is correct |
29 |
Correct |
3 ms |
14940 KB |
Output is correct |
30 |
Correct |
4 ms |
14940 KB |
Output is correct |
31 |
Correct |
4 ms |
14940 KB |
Output is correct |
32 |
Correct |
3 ms |
14940 KB |
Output is correct |
33 |
Correct |
3 ms |
14936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
15000 KB |
Output is correct |
2 |
Correct |
3 ms |
14936 KB |
Output is correct |
3 |
Correct |
3 ms |
14940 KB |
Output is correct |
4 |
Correct |
4 ms |
14940 KB |
Output is correct |
5 |
Correct |
3 ms |
14936 KB |
Output is correct |
6 |
Correct |
4 ms |
14940 KB |
Output is correct |
7 |
Correct |
4 ms |
14940 KB |
Output is correct |
8 |
Correct |
4 ms |
14940 KB |
Output is correct |
9 |
Correct |
4 ms |
14940 KB |
Output is correct |
10 |
Correct |
3 ms |
15004 KB |
Output is correct |
11 |
Correct |
3 ms |
14940 KB |
Output is correct |
12 |
Correct |
4 ms |
14940 KB |
Output is correct |
13 |
Correct |
3 ms |
14936 KB |
Output is correct |
14 |
Correct |
3 ms |
14940 KB |
Output is correct |
15 |
Correct |
3 ms |
14936 KB |
Output is correct |
16 |
Correct |
3 ms |
14936 KB |
Output is correct |
17 |
Correct |
4 ms |
14940 KB |
Output is correct |
18 |
Correct |
3 ms |
14940 KB |
Output is correct |
19 |
Correct |
3 ms |
14936 KB |
Output is correct |
20 |
Correct |
3 ms |
14940 KB |
Output is correct |
21 |
Correct |
4 ms |
14940 KB |
Output is correct |
22 |
Correct |
3 ms |
14940 KB |
Output is correct |
23 |
Correct |
3 ms |
14940 KB |
Output is correct |
24 |
Correct |
4 ms |
14936 KB |
Output is correct |
25 |
Correct |
3 ms |
14936 KB |
Output is correct |
26 |
Correct |
3 ms |
14940 KB |
Output is correct |
27 |
Correct |
3 ms |
14940 KB |
Output is correct |
28 |
Correct |
3 ms |
14940 KB |
Output is correct |
29 |
Correct |
3 ms |
14940 KB |
Output is correct |
30 |
Correct |
4 ms |
14940 KB |
Output is correct |
31 |
Correct |
4 ms |
14940 KB |
Output is correct |
32 |
Correct |
3 ms |
14940 KB |
Output is correct |
33 |
Correct |
3 ms |
14936 KB |
Output is correct |
34 |
Correct |
5 ms |
14940 KB |
Output is correct |
35 |
Correct |
64 ms |
20804 KB |
Output is correct |
36 |
Correct |
78 ms |
23636 KB |
Output is correct |
37 |
Correct |
78 ms |
23632 KB |
Output is correct |
38 |
Correct |
74 ms |
23252 KB |
Output is correct |
39 |
Correct |
6 ms |
15192 KB |
Output is correct |
40 |
Correct |
6 ms |
15708 KB |
Output is correct |
41 |
Correct |
6 ms |
15708 KB |
Output is correct |
42 |
Correct |
5 ms |
15196 KB |
Output is correct |
43 |
Correct |
6 ms |
15196 KB |
Output is correct |
44 |
Correct |
6 ms |
15196 KB |
Output is correct |
45 |
Correct |
5 ms |
15288 KB |
Output is correct |
46 |
Correct |
5 ms |
15192 KB |
Output is correct |
47 |
Correct |
6 ms |
15452 KB |
Output is correct |
48 |
Correct |
6 ms |
15452 KB |
Output is correct |
49 |
Correct |
11 ms |
16220 KB |
Output is correct |
50 |
Correct |
77 ms |
23676 KB |
Output is correct |
51 |
Correct |
8 ms |
15688 KB |
Output is correct |
52 |
Correct |
72 ms |
21844 KB |
Output is correct |
53 |
Correct |
10 ms |
15960 KB |
Output is correct |
54 |
Correct |
91 ms |
22732 KB |
Output is correct |
55 |
Correct |
7 ms |
15704 KB |
Output is correct |
56 |
Correct |
7 ms |
15708 KB |
Output is correct |
57 |
Correct |
7 ms |
15524 KB |
Output is correct |
58 |
Correct |
7 ms |
15708 KB |
Output is correct |
59 |
Correct |
7 ms |
15708 KB |
Output is correct |
60 |
Correct |
56 ms |
20516 KB |
Output is correct |
61 |
Correct |
7 ms |
15452 KB |
Output is correct |
62 |
Correct |
73 ms |
23144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
15000 KB |
Output is correct |
2 |
Correct |
3 ms |
14936 KB |
Output is correct |
3 |
Correct |
3 ms |
14940 KB |
Output is correct |
4 |
Correct |
4 ms |
14940 KB |
Output is correct |
5 |
Correct |
3 ms |
14936 KB |
Output is correct |
6 |
Correct |
4 ms |
14940 KB |
Output is correct |
7 |
Correct |
4 ms |
14940 KB |
Output is correct |
8 |
Correct |
4 ms |
14940 KB |
Output is correct |
9 |
Correct |
4 ms |
14940 KB |
Output is correct |
10 |
Correct |
3 ms |
15004 KB |
Output is correct |
11 |
Correct |
3 ms |
14940 KB |
Output is correct |
12 |
Correct |
4 ms |
14940 KB |
Output is correct |
13 |
Correct |
3 ms |
14936 KB |
Output is correct |
14 |
Correct |
3 ms |
14940 KB |
Output is correct |
15 |
Correct |
3 ms |
14936 KB |
Output is correct |
16 |
Correct |
3 ms |
14936 KB |
Output is correct |
17 |
Correct |
4 ms |
14940 KB |
Output is correct |
18 |
Correct |
3 ms |
14940 KB |
Output is correct |
19 |
Correct |
3 ms |
14936 KB |
Output is correct |
20 |
Correct |
3 ms |
14940 KB |
Output is correct |
21 |
Correct |
4 ms |
14940 KB |
Output is correct |
22 |
Correct |
3 ms |
14940 KB |
Output is correct |
23 |
Correct |
3 ms |
14940 KB |
Output is correct |
24 |
Correct |
4 ms |
14936 KB |
Output is correct |
25 |
Correct |
3 ms |
14936 KB |
Output is correct |
26 |
Correct |
3 ms |
14940 KB |
Output is correct |
27 |
Correct |
3 ms |
14940 KB |
Output is correct |
28 |
Correct |
3 ms |
14940 KB |
Output is correct |
29 |
Correct |
3 ms |
14940 KB |
Output is correct |
30 |
Correct |
4 ms |
14940 KB |
Output is correct |
31 |
Correct |
4 ms |
14940 KB |
Output is correct |
32 |
Correct |
3 ms |
14940 KB |
Output is correct |
33 |
Correct |
3 ms |
14936 KB |
Output is correct |
34 |
Correct |
5 ms |
14940 KB |
Output is correct |
35 |
Correct |
64 ms |
20804 KB |
Output is correct |
36 |
Correct |
78 ms |
23636 KB |
Output is correct |
37 |
Correct |
78 ms |
23632 KB |
Output is correct |
38 |
Correct |
74 ms |
23252 KB |
Output is correct |
39 |
Correct |
6 ms |
15192 KB |
Output is correct |
40 |
Correct |
6 ms |
15708 KB |
Output is correct |
41 |
Correct |
6 ms |
15708 KB |
Output is correct |
42 |
Correct |
5 ms |
15196 KB |
Output is correct |
43 |
Correct |
6 ms |
15196 KB |
Output is correct |
44 |
Correct |
6 ms |
15196 KB |
Output is correct |
45 |
Correct |
5 ms |
15288 KB |
Output is correct |
46 |
Correct |
5 ms |
15192 KB |
Output is correct |
47 |
Correct |
6 ms |
15452 KB |
Output is correct |
48 |
Correct |
6 ms |
15452 KB |
Output is correct |
49 |
Correct |
11 ms |
16220 KB |
Output is correct |
50 |
Correct |
77 ms |
23676 KB |
Output is correct |
51 |
Correct |
8 ms |
15688 KB |
Output is correct |
52 |
Correct |
72 ms |
21844 KB |
Output is correct |
53 |
Correct |
10 ms |
15960 KB |
Output is correct |
54 |
Correct |
91 ms |
22732 KB |
Output is correct |
55 |
Correct |
7 ms |
15704 KB |
Output is correct |
56 |
Correct |
7 ms |
15708 KB |
Output is correct |
57 |
Correct |
7 ms |
15524 KB |
Output is correct |
58 |
Correct |
7 ms |
15708 KB |
Output is correct |
59 |
Correct |
7 ms |
15708 KB |
Output is correct |
60 |
Correct |
56 ms |
20516 KB |
Output is correct |
61 |
Correct |
7 ms |
15452 KB |
Output is correct |
62 |
Correct |
73 ms |
23144 KB |
Output is correct |
63 |
Correct |
546 ms |
68136 KB |
Output is correct |
64 |
Correct |
490 ms |
67920 KB |
Output is correct |
65 |
Correct |
490 ms |
67920 KB |
Output is correct |
66 |
Correct |
197 ms |
34140 KB |
Output is correct |
67 |
Correct |
353 ms |
61012 KB |
Output is correct |
68 |
Correct |
171 ms |
34132 KB |
Output is correct |
69 |
Correct |
346 ms |
35152 KB |
Output is correct |
70 |
Correct |
176 ms |
34200 KB |
Output is correct |
71 |
Correct |
187 ms |
34128 KB |
Output is correct |
72 |
Correct |
350 ms |
45144 KB |
Output is correct |
73 |
Correct |
402 ms |
46912 KB |
Output is correct |
74 |
Correct |
902 ms |
68672 KB |
Output is correct |
75 |
Correct |
511 ms |
42108 KB |
Output is correct |
76 |
Correct |
640 ms |
54200 KB |
Output is correct |
77 |
Correct |
642 ms |
54612 KB |
Output is correct |
78 |
Correct |
204 ms |
37604 KB |
Output is correct |
79 |
Correct |
336 ms |
42380 KB |
Output is correct |
80 |
Correct |
169 ms |
36436 KB |
Output is correct |
81 |
Correct |
307 ms |
40492 KB |
Output is correct |
82 |
Correct |
666 ms |
54612 KB |
Output is correct |
83 |
Correct |
677 ms |
54608 KB |
Output is correct |
84 |
Correct |
511 ms |
53360 KB |
Output is correct |
85 |
Correct |
521 ms |
53344 KB |
Output is correct |
86 |
Correct |
264 ms |
66896 KB |
Output is correct |
87 |
Correct |
307 ms |
69116 KB |
Output is correct |
88 |
Correct |
356 ms |
46144 KB |
Output is correct |
89 |
Correct |
581 ms |
51960 KB |
Output is correct |