#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define pii pair <ll, ll>
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define F first
#define S second
const ll maxn = 3e5 + 10;
ll par[maxn], sz[maxn];
set <ll> in[maxn], inc[maxn];
set <pii> out[maxn];
queue <pii> q;
ll ans = 0;
ll find_par(ll x){
if(x == par[x])
return x;
return par[x] = find_par(par[x]);
}
inline bool add_edge(ll a, ll b){
b = find_par(b);
ll p = find_par(a);
if(p == b || out[p].find(mp(a, b)) != out[p].end()){
return false;
}
out[p].insert({a, b});
in[b].insert(a);
inc[b].insert(p);
if(inc[p].find(b) != inc[p].end()){
q.push({p, b});
}
return true;
}
void update_edges(ll x, ll y){
vector <pii> add, del;
for(ll u : in[x]){
del.pb({u, x});
if(find_par(u) != y){
add.pb({u, y});
}
}
for(auto u : out[x]){
del.pb(u);
if(find_par(u.S) != y)
add.pb(u);
}
for(auto u : del){
out[find_par(u.F)].erase(u);
in[find_par(u.S)].erase(u.F);
}
ll px = find_par(x);
ll py = find_par(y);
par[px] = py;
sz[py] += sz[px];
for(auto u : add){
add_edge(u.F, u.S);
}
}
ll siz(ll y){
// cout << "innn " << in[y].size() << endl;
return 1LL * sz[y] * (sz[y] - (ll)1) + sz[y] * (ll)in[y].size();
}
void merge(ll x, ll y){
if(in[x].size() + out[x].size() > in[y].size() + out[y].size())
swap(x, y);
ans -= (siz(y) + siz(x));
// cout << "annnnn " << ans << endl;
update_edges(x, y);
ans += siz(y);
}
int main(){
fast_io;
ll n, m;
cin >> n >> m;
for(ll i = 0; i < n; i ++){
sz[i] = 1;
par[i] = i;
}
while(m --){
ll a, b;
cin >> a >> b;
a --; b --;
if(add_edge(a, b)){
ans += sz[find_par(b)];
}
while(q.size()){
ll x = find_par(q.front().F);
ll y = find_par(q.front().S);
// cout << "!: " << x << " " << y << endl;
q.pop();
if(x != y){
merge(x, y);
}
}
cout << ans << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
42604 KB |
Output is correct |
2 |
Correct |
28 ms |
42604 KB |
Output is correct |
3 |
Correct |
33 ms |
42752 KB |
Output is correct |
4 |
Correct |
29 ms |
42604 KB |
Output is correct |
5 |
Correct |
29 ms |
42604 KB |
Output is correct |
6 |
Correct |
33 ms |
42604 KB |
Output is correct |
7 |
Correct |
33 ms |
42732 KB |
Output is correct |
8 |
Correct |
34 ms |
42732 KB |
Output is correct |
9 |
Correct |
29 ms |
42732 KB |
Output is correct |
10 |
Correct |
29 ms |
42604 KB |
Output is correct |
11 |
Correct |
28 ms |
42604 KB |
Output is correct |
12 |
Correct |
28 ms |
42604 KB |
Output is correct |
13 |
Correct |
30 ms |
42604 KB |
Output is correct |
14 |
Correct |
33 ms |
42604 KB |
Output is correct |
15 |
Correct |
28 ms |
42604 KB |
Output is correct |
16 |
Correct |
29 ms |
42604 KB |
Output is correct |
17 |
Correct |
29 ms |
42604 KB |
Output is correct |
18 |
Correct |
29 ms |
42604 KB |
Output is correct |
19 |
Correct |
28 ms |
42604 KB |
Output is correct |
20 |
Correct |
29 ms |
42604 KB |
Output is correct |
21 |
Correct |
30 ms |
42732 KB |
Output is correct |
22 |
Correct |
29 ms |
42604 KB |
Output is correct |
23 |
Correct |
29 ms |
42732 KB |
Output is correct |
24 |
Correct |
29 ms |
42604 KB |
Output is correct |
25 |
Correct |
29 ms |
42732 KB |
Output is correct |
26 |
Correct |
33 ms |
42604 KB |
Output is correct |
27 |
Correct |
30 ms |
42604 KB |
Output is correct |
28 |
Correct |
33 ms |
42604 KB |
Output is correct |
29 |
Correct |
28 ms |
42604 KB |
Output is correct |
30 |
Correct |
32 ms |
42604 KB |
Output is correct |
31 |
Correct |
29 ms |
42604 KB |
Output is correct |
32 |
Correct |
29 ms |
42732 KB |
Output is correct |
33 |
Correct |
30 ms |
42732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
42604 KB |
Output is correct |
2 |
Correct |
28 ms |
42604 KB |
Output is correct |
3 |
Correct |
33 ms |
42752 KB |
Output is correct |
4 |
Correct |
29 ms |
42604 KB |
Output is correct |
5 |
Correct |
29 ms |
42604 KB |
Output is correct |
6 |
Correct |
33 ms |
42604 KB |
Output is correct |
7 |
Correct |
33 ms |
42732 KB |
Output is correct |
8 |
Correct |
34 ms |
42732 KB |
Output is correct |
9 |
Correct |
29 ms |
42732 KB |
Output is correct |
10 |
Correct |
29 ms |
42604 KB |
Output is correct |
11 |
Correct |
28 ms |
42604 KB |
Output is correct |
12 |
Correct |
28 ms |
42604 KB |
Output is correct |
13 |
Correct |
30 ms |
42604 KB |
Output is correct |
14 |
Correct |
33 ms |
42604 KB |
Output is correct |
15 |
Correct |
28 ms |
42604 KB |
Output is correct |
16 |
Correct |
29 ms |
42604 KB |
Output is correct |
17 |
Correct |
29 ms |
42604 KB |
Output is correct |
18 |
Correct |
29 ms |
42604 KB |
Output is correct |
19 |
Correct |
28 ms |
42604 KB |
Output is correct |
20 |
Correct |
29 ms |
42604 KB |
Output is correct |
21 |
Correct |
30 ms |
42732 KB |
Output is correct |
22 |
Correct |
29 ms |
42604 KB |
Output is correct |
23 |
Correct |
29 ms |
42732 KB |
Output is correct |
24 |
Correct |
29 ms |
42604 KB |
Output is correct |
25 |
Correct |
29 ms |
42732 KB |
Output is correct |
26 |
Correct |
33 ms |
42604 KB |
Output is correct |
27 |
Correct |
30 ms |
42604 KB |
Output is correct |
28 |
Correct |
33 ms |
42604 KB |
Output is correct |
29 |
Correct |
28 ms |
42604 KB |
Output is correct |
30 |
Correct |
32 ms |
42604 KB |
Output is correct |
31 |
Correct |
29 ms |
42604 KB |
Output is correct |
32 |
Correct |
29 ms |
42732 KB |
Output is correct |
33 |
Correct |
30 ms |
42732 KB |
Output is correct |
34 |
Correct |
31 ms |
42860 KB |
Output is correct |
35 |
Correct |
126 ms |
48620 KB |
Output is correct |
36 |
Correct |
174 ms |
51564 KB |
Output is correct |
37 |
Correct |
164 ms |
51692 KB |
Output is correct |
38 |
Correct |
150 ms |
51308 KB |
Output is correct |
39 |
Correct |
32 ms |
42988 KB |
Output is correct |
40 |
Correct |
32 ms |
42988 KB |
Output is correct |
41 |
Correct |
33 ms |
42988 KB |
Output is correct |
42 |
Correct |
31 ms |
42988 KB |
Output is correct |
43 |
Correct |
33 ms |
42988 KB |
Output is correct |
44 |
Correct |
33 ms |
42988 KB |
Output is correct |
45 |
Correct |
32 ms |
43136 KB |
Output is correct |
46 |
Correct |
32 ms |
42988 KB |
Output is correct |
47 |
Correct |
36 ms |
43020 KB |
Output is correct |
48 |
Correct |
34 ms |
42988 KB |
Output is correct |
49 |
Correct |
43 ms |
43884 KB |
Output is correct |
50 |
Correct |
162 ms |
51832 KB |
Output is correct |
51 |
Correct |
37 ms |
43244 KB |
Output is correct |
52 |
Correct |
135 ms |
50028 KB |
Output is correct |
53 |
Correct |
41 ms |
43756 KB |
Output is correct |
54 |
Correct |
148 ms |
50924 KB |
Output is correct |
55 |
Correct |
36 ms |
43372 KB |
Output is correct |
56 |
Correct |
35 ms |
43372 KB |
Output is correct |
57 |
Correct |
37 ms |
43452 KB |
Output is correct |
58 |
Correct |
35 ms |
43500 KB |
Output is correct |
59 |
Correct |
31 ms |
42860 KB |
Output is correct |
60 |
Correct |
116 ms |
47860 KB |
Output is correct |
61 |
Correct |
33 ms |
43116 KB |
Output is correct |
62 |
Correct |
150 ms |
51180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
42604 KB |
Output is correct |
2 |
Correct |
28 ms |
42604 KB |
Output is correct |
3 |
Correct |
33 ms |
42752 KB |
Output is correct |
4 |
Correct |
29 ms |
42604 KB |
Output is correct |
5 |
Correct |
29 ms |
42604 KB |
Output is correct |
6 |
Correct |
33 ms |
42604 KB |
Output is correct |
7 |
Correct |
33 ms |
42732 KB |
Output is correct |
8 |
Correct |
34 ms |
42732 KB |
Output is correct |
9 |
Correct |
29 ms |
42732 KB |
Output is correct |
10 |
Correct |
29 ms |
42604 KB |
Output is correct |
11 |
Correct |
28 ms |
42604 KB |
Output is correct |
12 |
Correct |
28 ms |
42604 KB |
Output is correct |
13 |
Correct |
30 ms |
42604 KB |
Output is correct |
14 |
Correct |
33 ms |
42604 KB |
Output is correct |
15 |
Correct |
28 ms |
42604 KB |
Output is correct |
16 |
Correct |
29 ms |
42604 KB |
Output is correct |
17 |
Correct |
29 ms |
42604 KB |
Output is correct |
18 |
Correct |
29 ms |
42604 KB |
Output is correct |
19 |
Correct |
28 ms |
42604 KB |
Output is correct |
20 |
Correct |
29 ms |
42604 KB |
Output is correct |
21 |
Correct |
30 ms |
42732 KB |
Output is correct |
22 |
Correct |
29 ms |
42604 KB |
Output is correct |
23 |
Correct |
29 ms |
42732 KB |
Output is correct |
24 |
Correct |
29 ms |
42604 KB |
Output is correct |
25 |
Correct |
29 ms |
42732 KB |
Output is correct |
26 |
Correct |
33 ms |
42604 KB |
Output is correct |
27 |
Correct |
30 ms |
42604 KB |
Output is correct |
28 |
Correct |
33 ms |
42604 KB |
Output is correct |
29 |
Correct |
28 ms |
42604 KB |
Output is correct |
30 |
Correct |
32 ms |
42604 KB |
Output is correct |
31 |
Correct |
29 ms |
42604 KB |
Output is correct |
32 |
Correct |
29 ms |
42732 KB |
Output is correct |
33 |
Correct |
30 ms |
42732 KB |
Output is correct |
34 |
Correct |
31 ms |
42860 KB |
Output is correct |
35 |
Correct |
126 ms |
48620 KB |
Output is correct |
36 |
Correct |
174 ms |
51564 KB |
Output is correct |
37 |
Correct |
164 ms |
51692 KB |
Output is correct |
38 |
Correct |
150 ms |
51308 KB |
Output is correct |
39 |
Correct |
32 ms |
42988 KB |
Output is correct |
40 |
Correct |
32 ms |
42988 KB |
Output is correct |
41 |
Correct |
33 ms |
42988 KB |
Output is correct |
42 |
Correct |
31 ms |
42988 KB |
Output is correct |
43 |
Correct |
33 ms |
42988 KB |
Output is correct |
44 |
Correct |
33 ms |
42988 KB |
Output is correct |
45 |
Correct |
32 ms |
43136 KB |
Output is correct |
46 |
Correct |
32 ms |
42988 KB |
Output is correct |
47 |
Correct |
36 ms |
43020 KB |
Output is correct |
48 |
Correct |
34 ms |
42988 KB |
Output is correct |
49 |
Correct |
43 ms |
43884 KB |
Output is correct |
50 |
Correct |
162 ms |
51832 KB |
Output is correct |
51 |
Correct |
37 ms |
43244 KB |
Output is correct |
52 |
Correct |
135 ms |
50028 KB |
Output is correct |
53 |
Correct |
41 ms |
43756 KB |
Output is correct |
54 |
Correct |
148 ms |
50924 KB |
Output is correct |
55 |
Correct |
36 ms |
43372 KB |
Output is correct |
56 |
Correct |
35 ms |
43372 KB |
Output is correct |
57 |
Correct |
37 ms |
43452 KB |
Output is correct |
58 |
Correct |
35 ms |
43500 KB |
Output is correct |
59 |
Correct |
31 ms |
42860 KB |
Output is correct |
60 |
Correct |
116 ms |
47860 KB |
Output is correct |
61 |
Correct |
33 ms |
43116 KB |
Output is correct |
62 |
Correct |
150 ms |
51180 KB |
Output is correct |
63 |
Correct |
595 ms |
96748 KB |
Output is correct |
64 |
Correct |
653 ms |
96680 KB |
Output is correct |
65 |
Correct |
623 ms |
96748 KB |
Output is correct |
66 |
Correct |
293 ms |
58220 KB |
Output is correct |
67 |
Correct |
445 ms |
61036 KB |
Output is correct |
68 |
Correct |
283 ms |
58092 KB |
Output is correct |
69 |
Correct |
579 ms |
60140 KB |
Output is correct |
70 |
Correct |
286 ms |
57964 KB |
Output is correct |
71 |
Correct |
287 ms |
57964 KB |
Output is correct |
72 |
Correct |
498 ms |
61548 KB |
Output is correct |
73 |
Correct |
589 ms |
61420 KB |
Output is correct |
74 |
Correct |
1467 ms |
86252 KB |
Output is correct |
75 |
Correct |
852 ms |
66668 KB |
Output is correct |
76 |
Correct |
1014 ms |
75992 KB |
Output is correct |
77 |
Correct |
1048 ms |
76504 KB |
Output is correct |
78 |
Correct |
332 ms |
61304 KB |
Output is correct |
79 |
Correct |
605 ms |
66156 KB |
Output is correct |
80 |
Correct |
288 ms |
61292 KB |
Output is correct |
81 |
Correct |
502 ms |
65004 KB |
Output is correct |
82 |
Correct |
1109 ms |
80748 KB |
Output is correct |
83 |
Correct |
1136 ms |
80492 KB |
Output is correct |
84 |
Correct |
930 ms |
79340 KB |
Output is correct |
85 |
Correct |
919 ms |
79340 KB |
Output is correct |
86 |
Correct |
244 ms |
57388 KB |
Output is correct |
87 |
Correct |
312 ms |
59504 KB |
Output is correct |
88 |
Correct |
528 ms |
61804 KB |
Output is correct |
89 |
Correct |
1008 ms |
74092 KB |
Output is correct |