#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<limits.h>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric>
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-loops")
using namespace std;
const int mod=1e9+7,mxn=1e5+5,lg=30,inf=1e18,minf=-1e18;
int pa[mxn+10],sz[mxn+10];
set<int>con[mxn+10],rcon[mxn+10],fol[mxn+10];
// ->, <-, all node that follow in component||node of that component
queue<pii>todo;
int find(int u){return pa[u]==u?u:pa[u]=find(pa[u]);}
ll ans;
void merg(int a,int b){
a=find(a),b=find(b);
if(a==b)return;
if(sz[a]<sz[b])swap(a,b);
ans+=(fol[b].size()*sz[a])+(sz[b]*fol[a].size());
pa[b]=a;
sz[a]+=sz[b];
for(auto i:fol[b]){
if(fol[a].count(i))ans-=sz[a];
else fol[a].insert(i);
}
con[b].erase(a),con[a].erase(b),rcon[a].erase(b),rcon[b].erase(a);
for(auto i:con[b]){
rcon[i].erase(b);
rcon[i].insert(a);
con[a].insert(i);
if(rcon[a].count(i))todo.push({i,a});
}
for(auto i:rcon[b]){
con[i].erase(b);
con[i].insert(a);
rcon[a].insert(i);
if(con[a].count(i))todo.push({i,a});
}
}
int32_t main(){
fastio
int n,m;cin>>n>>m;
for(int i=1;i<=n;i++)pa[i]=i,sz[i]=1,fol[i].insert(i);
while(m--){
int a,b;cin>>a>>b;
b=find(b);
int g=find(a);
if(g==b){
cout<<ans<<'\n';
continue;
}
if(!fol[b].count(a)){
fol[b].insert(a);
ans+=sz[b];
if(!con[g].count(b))con[g].insert(b),rcon[b].insert(g);
if(con[b].count(g))todo.push({g,b});
}
while(!todo.empty())merg(todo.front().f,todo.front().s),todo.pop();
cout<<ans<<'\n';
}
return 0;
}
Compilation message
joitter2.cpp:33:41: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+18' to '2147483647' [-Woverflow]
33 | const int mod=1e9+7,mxn=1e5+5,lg=30,inf=1e18,minf=-1e18;
| ^~~~
joitter2.cpp:33:51: warning: overflow in conversion from 'double' to 'int' changes value from '-1.0e+18' to '-2147483648' [-Woverflow]
33 | const int mod=1e9+7,mxn=1e5+5,lg=30,inf=1e18,minf=-1e18;
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
14940 KB |
Output is correct |
2 |
Correct |
3 ms |
14936 KB |
Output is correct |
3 |
Correct |
3 ms |
15000 KB |
Output is correct |
4 |
Correct |
4 ms |
14940 KB |
Output is correct |
5 |
Correct |
3 ms |
14940 KB |
Output is correct |
6 |
Correct |
3 ms |
14940 KB |
Output is correct |
7 |
Correct |
3 ms |
14940 KB |
Output is correct |
8 |
Correct |
4 ms |
14940 KB |
Output is correct |
9 |
Correct |
3 ms |
14960 KB |
Output is correct |
10 |
Correct |
3 ms |
14940 KB |
Output is correct |
11 |
Correct |
3 ms |
14940 KB |
Output is correct |
12 |
Correct |
3 ms |
14784 KB |
Output is correct |
13 |
Correct |
3 ms |
14940 KB |
Output is correct |
14 |
Correct |
3 ms |
14936 KB |
Output is correct |
15 |
Correct |
3 ms |
14940 KB |
Output is correct |
16 |
Correct |
3 ms |
14940 KB |
Output is correct |
17 |
Correct |
3 ms |
14940 KB |
Output is correct |
18 |
Correct |
3 ms |
14940 KB |
Output is correct |
19 |
Correct |
3 ms |
14940 KB |
Output is correct |
20 |
Correct |
3 ms |
14940 KB |
Output is correct |
21 |
Correct |
3 ms |
14940 KB |
Output is correct |
22 |
Correct |
3 ms |
14960 KB |
Output is correct |
23 |
Correct |
3 ms |
14940 KB |
Output is correct |
24 |
Correct |
4 ms |
15036 KB |
Output is correct |
25 |
Correct |
3 ms |
14940 KB |
Output is correct |
26 |
Correct |
3 ms |
14940 KB |
Output is correct |
27 |
Correct |
3 ms |
14936 KB |
Output is correct |
28 |
Correct |
3 ms |
14940 KB |
Output is correct |
29 |
Correct |
3 ms |
14940 KB |
Output is correct |
30 |
Correct |
3 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 |
15028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
14940 KB |
Output is correct |
2 |
Correct |
3 ms |
14936 KB |
Output is correct |
3 |
Correct |
3 ms |
15000 KB |
Output is correct |
4 |
Correct |
4 ms |
14940 KB |
Output is correct |
5 |
Correct |
3 ms |
14940 KB |
Output is correct |
6 |
Correct |
3 ms |
14940 KB |
Output is correct |
7 |
Correct |
3 ms |
14940 KB |
Output is correct |
8 |
Correct |
4 ms |
14940 KB |
Output is correct |
9 |
Correct |
3 ms |
14960 KB |
Output is correct |
10 |
Correct |
3 ms |
14940 KB |
Output is correct |
11 |
Correct |
3 ms |
14940 KB |
Output is correct |
12 |
Correct |
3 ms |
14784 KB |
Output is correct |
13 |
Correct |
3 ms |
14940 KB |
Output is correct |
14 |
Correct |
3 ms |
14936 KB |
Output is correct |
15 |
Correct |
3 ms |
14940 KB |
Output is correct |
16 |
Correct |
3 ms |
14940 KB |
Output is correct |
17 |
Correct |
3 ms |
14940 KB |
Output is correct |
18 |
Correct |
3 ms |
14940 KB |
Output is correct |
19 |
Correct |
3 ms |
14940 KB |
Output is correct |
20 |
Correct |
3 ms |
14940 KB |
Output is correct |
21 |
Correct |
3 ms |
14940 KB |
Output is correct |
22 |
Correct |
3 ms |
14960 KB |
Output is correct |
23 |
Correct |
3 ms |
14940 KB |
Output is correct |
24 |
Correct |
4 ms |
15036 KB |
Output is correct |
25 |
Correct |
3 ms |
14940 KB |
Output is correct |
26 |
Correct |
3 ms |
14940 KB |
Output is correct |
27 |
Correct |
3 ms |
14936 KB |
Output is correct |
28 |
Correct |
3 ms |
14940 KB |
Output is correct |
29 |
Correct |
3 ms |
14940 KB |
Output is correct |
30 |
Correct |
3 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 |
15028 KB |
Output is correct |
34 |
Correct |
5 ms |
14940 KB |
Output is correct |
35 |
Correct |
62 ms |
18356 KB |
Output is correct |
36 |
Correct |
81 ms |
20792 KB |
Output is correct |
37 |
Correct |
85 ms |
21060 KB |
Output is correct |
38 |
Correct |
83 ms |
20612 KB |
Output is correct |
39 |
Correct |
5 ms |
15196 KB |
Output is correct |
40 |
Correct |
8 ms |
15452 KB |
Output is correct |
41 |
Correct |
6 ms |
15452 KB |
Output is correct |
42 |
Correct |
4 ms |
15196 KB |
Output is correct |
43 |
Correct |
5 ms |
15196 KB |
Output is correct |
44 |
Correct |
6 ms |
15196 KB |
Output is correct |
45 |
Correct |
6 ms |
15196 KB |
Output is correct |
46 |
Correct |
5 ms |
15196 KB |
Output is correct |
47 |
Correct |
6 ms |
15336 KB |
Output is correct |
48 |
Correct |
6 ms |
15452 KB |
Output is correct |
49 |
Correct |
11 ms |
16052 KB |
Output is correct |
50 |
Correct |
100 ms |
21228 KB |
Output is correct |
51 |
Correct |
8 ms |
15452 KB |
Output is correct |
52 |
Correct |
73 ms |
19284 KB |
Output is correct |
53 |
Correct |
11 ms |
15964 KB |
Output is correct |
54 |
Correct |
77 ms |
20156 KB |
Output is correct |
55 |
Correct |
7 ms |
15704 KB |
Output is correct |
56 |
Correct |
7 ms |
15704 KB |
Output is correct |
57 |
Correct |
7 ms |
15452 KB |
Output is correct |
58 |
Correct |
6 ms |
15708 KB |
Output is correct |
59 |
Correct |
6 ms |
15708 KB |
Output is correct |
60 |
Correct |
61 ms |
17876 KB |
Output is correct |
61 |
Correct |
8 ms |
15324 KB |
Output is correct |
62 |
Correct |
78 ms |
20560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
14940 KB |
Output is correct |
2 |
Correct |
3 ms |
14936 KB |
Output is correct |
3 |
Correct |
3 ms |
15000 KB |
Output is correct |
4 |
Correct |
4 ms |
14940 KB |
Output is correct |
5 |
Correct |
3 ms |
14940 KB |
Output is correct |
6 |
Correct |
3 ms |
14940 KB |
Output is correct |
7 |
Correct |
3 ms |
14940 KB |
Output is correct |
8 |
Correct |
4 ms |
14940 KB |
Output is correct |
9 |
Correct |
3 ms |
14960 KB |
Output is correct |
10 |
Correct |
3 ms |
14940 KB |
Output is correct |
11 |
Correct |
3 ms |
14940 KB |
Output is correct |
12 |
Correct |
3 ms |
14784 KB |
Output is correct |
13 |
Correct |
3 ms |
14940 KB |
Output is correct |
14 |
Correct |
3 ms |
14936 KB |
Output is correct |
15 |
Correct |
3 ms |
14940 KB |
Output is correct |
16 |
Correct |
3 ms |
14940 KB |
Output is correct |
17 |
Correct |
3 ms |
14940 KB |
Output is correct |
18 |
Correct |
3 ms |
14940 KB |
Output is correct |
19 |
Correct |
3 ms |
14940 KB |
Output is correct |
20 |
Correct |
3 ms |
14940 KB |
Output is correct |
21 |
Correct |
3 ms |
14940 KB |
Output is correct |
22 |
Correct |
3 ms |
14960 KB |
Output is correct |
23 |
Correct |
3 ms |
14940 KB |
Output is correct |
24 |
Correct |
4 ms |
15036 KB |
Output is correct |
25 |
Correct |
3 ms |
14940 KB |
Output is correct |
26 |
Correct |
3 ms |
14940 KB |
Output is correct |
27 |
Correct |
3 ms |
14936 KB |
Output is correct |
28 |
Correct |
3 ms |
14940 KB |
Output is correct |
29 |
Correct |
3 ms |
14940 KB |
Output is correct |
30 |
Correct |
3 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 |
15028 KB |
Output is correct |
34 |
Correct |
5 ms |
14940 KB |
Output is correct |
35 |
Correct |
62 ms |
18356 KB |
Output is correct |
36 |
Correct |
81 ms |
20792 KB |
Output is correct |
37 |
Correct |
85 ms |
21060 KB |
Output is correct |
38 |
Correct |
83 ms |
20612 KB |
Output is correct |
39 |
Correct |
5 ms |
15196 KB |
Output is correct |
40 |
Correct |
8 ms |
15452 KB |
Output is correct |
41 |
Correct |
6 ms |
15452 KB |
Output is correct |
42 |
Correct |
4 ms |
15196 KB |
Output is correct |
43 |
Correct |
5 ms |
15196 KB |
Output is correct |
44 |
Correct |
6 ms |
15196 KB |
Output is correct |
45 |
Correct |
6 ms |
15196 KB |
Output is correct |
46 |
Correct |
5 ms |
15196 KB |
Output is correct |
47 |
Correct |
6 ms |
15336 KB |
Output is correct |
48 |
Correct |
6 ms |
15452 KB |
Output is correct |
49 |
Correct |
11 ms |
16052 KB |
Output is correct |
50 |
Correct |
100 ms |
21228 KB |
Output is correct |
51 |
Correct |
8 ms |
15452 KB |
Output is correct |
52 |
Correct |
73 ms |
19284 KB |
Output is correct |
53 |
Correct |
11 ms |
15964 KB |
Output is correct |
54 |
Correct |
77 ms |
20156 KB |
Output is correct |
55 |
Correct |
7 ms |
15704 KB |
Output is correct |
56 |
Correct |
7 ms |
15704 KB |
Output is correct |
57 |
Correct |
7 ms |
15452 KB |
Output is correct |
58 |
Correct |
6 ms |
15708 KB |
Output is correct |
59 |
Correct |
6 ms |
15708 KB |
Output is correct |
60 |
Correct |
61 ms |
17876 KB |
Output is correct |
61 |
Correct |
8 ms |
15324 KB |
Output is correct |
62 |
Correct |
78 ms |
20560 KB |
Output is correct |
63 |
Correct |
488 ms |
64288 KB |
Output is correct |
64 |
Correct |
479 ms |
64284 KB |
Output is correct |
65 |
Correct |
470 ms |
64084 KB |
Output is correct |
66 |
Correct |
174 ms |
31680 KB |
Output is correct |
67 |
Correct |
378 ms |
59100 KB |
Output is correct |
68 |
Correct |
173 ms |
31312 KB |
Output is correct |
69 |
Correct |
327 ms |
32632 KB |
Output is correct |
70 |
Correct |
177 ms |
31396 KB |
Output is correct |
71 |
Correct |
234 ms |
31432 KB |
Output is correct |
72 |
Correct |
391 ms |
43720 KB |
Output is correct |
73 |
Correct |
372 ms |
45400 KB |
Output is correct |
74 |
Correct |
962 ms |
67156 KB |
Output is correct |
75 |
Correct |
508 ms |
38088 KB |
Output is correct |
76 |
Correct |
700 ms |
51800 KB |
Output is correct |
77 |
Correct |
687 ms |
51944 KB |
Output is correct |
78 |
Correct |
200 ms |
35548 KB |
Output is correct |
79 |
Correct |
366 ms |
38332 KB |
Output is correct |
80 |
Correct |
178 ms |
34452 KB |
Output is correct |
81 |
Correct |
283 ms |
36200 KB |
Output is correct |
82 |
Correct |
690 ms |
52000 KB |
Output is correct |
83 |
Correct |
701 ms |
51980 KB |
Output is correct |
84 |
Correct |
600 ms |
50768 KB |
Output is correct |
85 |
Correct |
543 ms |
50708 KB |
Output is correct |
86 |
Correct |
271 ms |
64084 KB |
Output is correct |
87 |
Correct |
314 ms |
65404 KB |
Output is correct |
88 |
Correct |
402 ms |
44856 KB |
Output is correct |
89 |
Correct |
670 ms |
49668 KB |
Output is correct |