#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=5e5+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||component of that node
queue<pii>todo;
int find(int u){return pa[u]==u?u:pa[u]=find(pa[u]);}
int 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];
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=5e5+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=5e5+5,lg=30,inf=1e18,minf=-1e18;
| ^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
74328 KB |
Output is correct |
2 |
Correct |
14 ms |
74432 KB |
Output is correct |
3 |
Correct |
17 ms |
74424 KB |
Output is correct |
4 |
Correct |
14 ms |
74332 KB |
Output is correct |
5 |
Correct |
14 ms |
74512 KB |
Output is correct |
6 |
Correct |
14 ms |
74332 KB |
Output is correct |
7 |
Correct |
15 ms |
74500 KB |
Output is correct |
8 |
Correct |
15 ms |
74584 KB |
Output is correct |
9 |
Correct |
16 ms |
74588 KB |
Output is correct |
10 |
Correct |
14 ms |
74564 KB |
Output is correct |
11 |
Correct |
16 ms |
74528 KB |
Output is correct |
12 |
Correct |
14 ms |
74332 KB |
Output is correct |
13 |
Correct |
16 ms |
74584 KB |
Output is correct |
14 |
Correct |
14 ms |
74584 KB |
Output is correct |
15 |
Correct |
15 ms |
74632 KB |
Output is correct |
16 |
Correct |
15 ms |
74328 KB |
Output is correct |
17 |
Correct |
14 ms |
74328 KB |
Output is correct |
18 |
Correct |
15 ms |
74340 KB |
Output is correct |
19 |
Correct |
15 ms |
74328 KB |
Output is correct |
20 |
Correct |
15 ms |
74512 KB |
Output is correct |
21 |
Correct |
16 ms |
74588 KB |
Output is correct |
22 |
Correct |
15 ms |
74588 KB |
Output is correct |
23 |
Correct |
15 ms |
74472 KB |
Output is correct |
24 |
Correct |
15 ms |
74548 KB |
Output is correct |
25 |
Correct |
15 ms |
74588 KB |
Output is correct |
26 |
Correct |
15 ms |
74452 KB |
Output is correct |
27 |
Correct |
15 ms |
74596 KB |
Output is correct |
28 |
Correct |
16 ms |
74588 KB |
Output is correct |
29 |
Correct |
17 ms |
74608 KB |
Output is correct |
30 |
Correct |
16 ms |
74332 KB |
Output is correct |
31 |
Correct |
15 ms |
74332 KB |
Output is correct |
32 |
Correct |
14 ms |
74328 KB |
Output is correct |
33 |
Correct |
15 ms |
74588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
74328 KB |
Output is correct |
2 |
Correct |
14 ms |
74432 KB |
Output is correct |
3 |
Correct |
17 ms |
74424 KB |
Output is correct |
4 |
Correct |
14 ms |
74332 KB |
Output is correct |
5 |
Correct |
14 ms |
74512 KB |
Output is correct |
6 |
Correct |
14 ms |
74332 KB |
Output is correct |
7 |
Correct |
15 ms |
74500 KB |
Output is correct |
8 |
Correct |
15 ms |
74584 KB |
Output is correct |
9 |
Correct |
16 ms |
74588 KB |
Output is correct |
10 |
Correct |
14 ms |
74564 KB |
Output is correct |
11 |
Correct |
16 ms |
74528 KB |
Output is correct |
12 |
Correct |
14 ms |
74332 KB |
Output is correct |
13 |
Correct |
16 ms |
74584 KB |
Output is correct |
14 |
Correct |
14 ms |
74584 KB |
Output is correct |
15 |
Correct |
15 ms |
74632 KB |
Output is correct |
16 |
Correct |
15 ms |
74328 KB |
Output is correct |
17 |
Correct |
14 ms |
74328 KB |
Output is correct |
18 |
Correct |
15 ms |
74340 KB |
Output is correct |
19 |
Correct |
15 ms |
74328 KB |
Output is correct |
20 |
Correct |
15 ms |
74512 KB |
Output is correct |
21 |
Correct |
16 ms |
74588 KB |
Output is correct |
22 |
Correct |
15 ms |
74588 KB |
Output is correct |
23 |
Correct |
15 ms |
74472 KB |
Output is correct |
24 |
Correct |
15 ms |
74548 KB |
Output is correct |
25 |
Correct |
15 ms |
74588 KB |
Output is correct |
26 |
Correct |
15 ms |
74452 KB |
Output is correct |
27 |
Correct |
15 ms |
74596 KB |
Output is correct |
28 |
Correct |
16 ms |
74588 KB |
Output is correct |
29 |
Correct |
17 ms |
74608 KB |
Output is correct |
30 |
Correct |
16 ms |
74332 KB |
Output is correct |
31 |
Correct |
15 ms |
74332 KB |
Output is correct |
32 |
Correct |
14 ms |
74328 KB |
Output is correct |
33 |
Correct |
15 ms |
74588 KB |
Output is correct |
34 |
Correct |
19 ms |
75096 KB |
Output is correct |
35 |
Correct |
398 ms |
154468 KB |
Output is correct |
36 |
Correct |
1458 ms |
392944 KB |
Output is correct |
37 |
Correct |
1527 ms |
398736 KB |
Output is correct |
38 |
Correct |
1495 ms |
397676 KB |
Output is correct |
39 |
Correct |
260 ms |
168532 KB |
Output is correct |
40 |
Correct |
20 ms |
75612 KB |
Output is correct |
41 |
Correct |
20 ms |
75776 KB |
Output is correct |
42 |
Correct |
266 ms |
168620 KB |
Output is correct |
43 |
Correct |
572 ms |
262736 KB |
Output is correct |
44 |
Correct |
572 ms |
260912 KB |
Output is correct |
45 |
Correct |
262 ms |
168876 KB |
Output is correct |
46 |
Correct |
257 ms |
168396 KB |
Output is correct |
47 |
Correct |
79 ms |
93268 KB |
Output is correct |
48 |
Correct |
74 ms |
94284 KB |
Output is correct |
49 |
Correct |
405 ms |
185124 KB |
Output is correct |
50 |
Correct |
1511 ms |
391588 KB |
Output is correct |
51 |
Correct |
619 ms |
262848 KB |
Output is correct |
52 |
Correct |
1246 ms |
367556 KB |
Output is correct |
53 |
Correct |
532 ms |
212700 KB |
Output is correct |
54 |
Correct |
1420 ms |
387640 KB |
Output is correct |
55 |
Correct |
680 ms |
277068 KB |
Output is correct |
56 |
Correct |
728 ms |
275620 KB |
Output is correct |
57 |
Correct |
527 ms |
237396 KB |
Output is correct |
58 |
Correct |
536 ms |
239608 KB |
Output is correct |
59 |
Correct |
18 ms |
75096 KB |
Output is correct |
60 |
Correct |
71 ms |
80052 KB |
Output is correct |
61 |
Correct |
196 ms |
130388 KB |
Output is correct |
62 |
Correct |
1438 ms |
391468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
74328 KB |
Output is correct |
2 |
Correct |
14 ms |
74432 KB |
Output is correct |
3 |
Correct |
17 ms |
74424 KB |
Output is correct |
4 |
Correct |
14 ms |
74332 KB |
Output is correct |
5 |
Correct |
14 ms |
74512 KB |
Output is correct |
6 |
Correct |
14 ms |
74332 KB |
Output is correct |
7 |
Correct |
15 ms |
74500 KB |
Output is correct |
8 |
Correct |
15 ms |
74584 KB |
Output is correct |
9 |
Correct |
16 ms |
74588 KB |
Output is correct |
10 |
Correct |
14 ms |
74564 KB |
Output is correct |
11 |
Correct |
16 ms |
74528 KB |
Output is correct |
12 |
Correct |
14 ms |
74332 KB |
Output is correct |
13 |
Correct |
16 ms |
74584 KB |
Output is correct |
14 |
Correct |
14 ms |
74584 KB |
Output is correct |
15 |
Correct |
15 ms |
74632 KB |
Output is correct |
16 |
Correct |
15 ms |
74328 KB |
Output is correct |
17 |
Correct |
14 ms |
74328 KB |
Output is correct |
18 |
Correct |
15 ms |
74340 KB |
Output is correct |
19 |
Correct |
15 ms |
74328 KB |
Output is correct |
20 |
Correct |
15 ms |
74512 KB |
Output is correct |
21 |
Correct |
16 ms |
74588 KB |
Output is correct |
22 |
Correct |
15 ms |
74588 KB |
Output is correct |
23 |
Correct |
15 ms |
74472 KB |
Output is correct |
24 |
Correct |
15 ms |
74548 KB |
Output is correct |
25 |
Correct |
15 ms |
74588 KB |
Output is correct |
26 |
Correct |
15 ms |
74452 KB |
Output is correct |
27 |
Correct |
15 ms |
74596 KB |
Output is correct |
28 |
Correct |
16 ms |
74588 KB |
Output is correct |
29 |
Correct |
17 ms |
74608 KB |
Output is correct |
30 |
Correct |
16 ms |
74332 KB |
Output is correct |
31 |
Correct |
15 ms |
74332 KB |
Output is correct |
32 |
Correct |
14 ms |
74328 KB |
Output is correct |
33 |
Correct |
15 ms |
74588 KB |
Output is correct |
34 |
Correct |
19 ms |
75096 KB |
Output is correct |
35 |
Correct |
398 ms |
154468 KB |
Output is correct |
36 |
Correct |
1458 ms |
392944 KB |
Output is correct |
37 |
Correct |
1527 ms |
398736 KB |
Output is correct |
38 |
Correct |
1495 ms |
397676 KB |
Output is correct |
39 |
Correct |
260 ms |
168532 KB |
Output is correct |
40 |
Correct |
20 ms |
75612 KB |
Output is correct |
41 |
Correct |
20 ms |
75776 KB |
Output is correct |
42 |
Correct |
266 ms |
168620 KB |
Output is correct |
43 |
Correct |
572 ms |
262736 KB |
Output is correct |
44 |
Correct |
572 ms |
260912 KB |
Output is correct |
45 |
Correct |
262 ms |
168876 KB |
Output is correct |
46 |
Correct |
257 ms |
168396 KB |
Output is correct |
47 |
Correct |
79 ms |
93268 KB |
Output is correct |
48 |
Correct |
74 ms |
94284 KB |
Output is correct |
49 |
Correct |
405 ms |
185124 KB |
Output is correct |
50 |
Correct |
1511 ms |
391588 KB |
Output is correct |
51 |
Correct |
619 ms |
262848 KB |
Output is correct |
52 |
Correct |
1246 ms |
367556 KB |
Output is correct |
53 |
Correct |
532 ms |
212700 KB |
Output is correct |
54 |
Correct |
1420 ms |
387640 KB |
Output is correct |
55 |
Correct |
680 ms |
277068 KB |
Output is correct |
56 |
Correct |
728 ms |
275620 KB |
Output is correct |
57 |
Correct |
527 ms |
237396 KB |
Output is correct |
58 |
Correct |
536 ms |
239608 KB |
Output is correct |
59 |
Correct |
18 ms |
75096 KB |
Output is correct |
60 |
Correct |
71 ms |
80052 KB |
Output is correct |
61 |
Correct |
196 ms |
130388 KB |
Output is correct |
62 |
Correct |
1438 ms |
391468 KB |
Output is correct |
63 |
Correct |
500 ms |
126900 KB |
Output is correct |
64 |
Correct |
506 ms |
126904 KB |
Output is correct |
65 |
Correct |
494 ms |
126920 KB |
Output is correct |
66 |
Runtime error |
2451 ms |
1048576 KB |
Execution killed with signal 9 |
67 |
Halted |
0 ms |
0 KB |
- |