#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")
#define int long long
using namespace std;
const int mod=1e9+7,mxn=1e6+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]=1ll,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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
144212 KB |
Output is correct |
2 |
Correct |
28 ms |
144216 KB |
Output is correct |
3 |
Correct |
28 ms |
144212 KB |
Output is correct |
4 |
Correct |
28 ms |
144276 KB |
Output is correct |
5 |
Correct |
27 ms |
144220 KB |
Output is correct |
6 |
Correct |
28 ms |
144272 KB |
Output is correct |
7 |
Correct |
29 ms |
144412 KB |
Output is correct |
8 |
Correct |
30 ms |
144416 KB |
Output is correct |
9 |
Correct |
29 ms |
144220 KB |
Output is correct |
10 |
Correct |
28 ms |
144208 KB |
Output is correct |
11 |
Correct |
29 ms |
144240 KB |
Output is correct |
12 |
Correct |
28 ms |
144216 KB |
Output is correct |
13 |
Correct |
29 ms |
144208 KB |
Output is correct |
14 |
Correct |
29 ms |
144212 KB |
Output is correct |
15 |
Correct |
30 ms |
144476 KB |
Output is correct |
16 |
Correct |
28 ms |
144220 KB |
Output is correct |
17 |
Correct |
28 ms |
144120 KB |
Output is correct |
18 |
Correct |
28 ms |
144220 KB |
Output is correct |
19 |
Correct |
28 ms |
144196 KB |
Output is correct |
20 |
Correct |
28 ms |
144256 KB |
Output is correct |
21 |
Correct |
29 ms |
144216 KB |
Output is correct |
22 |
Correct |
29 ms |
144472 KB |
Output is correct |
23 |
Correct |
29 ms |
144220 KB |
Output is correct |
24 |
Correct |
31 ms |
144208 KB |
Output is correct |
25 |
Correct |
30 ms |
144216 KB |
Output is correct |
26 |
Correct |
28 ms |
144400 KB |
Output is correct |
27 |
Correct |
28 ms |
144220 KB |
Output is correct |
28 |
Correct |
28 ms |
144212 KB |
Output is correct |
29 |
Correct |
29 ms |
144220 KB |
Output is correct |
30 |
Correct |
28 ms |
144220 KB |
Output is correct |
31 |
Correct |
31 ms |
144300 KB |
Output is correct |
32 |
Correct |
28 ms |
144220 KB |
Output is correct |
33 |
Correct |
28 ms |
144204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
144212 KB |
Output is correct |
2 |
Correct |
28 ms |
144216 KB |
Output is correct |
3 |
Correct |
28 ms |
144212 KB |
Output is correct |
4 |
Correct |
28 ms |
144276 KB |
Output is correct |
5 |
Correct |
27 ms |
144220 KB |
Output is correct |
6 |
Correct |
28 ms |
144272 KB |
Output is correct |
7 |
Correct |
29 ms |
144412 KB |
Output is correct |
8 |
Correct |
30 ms |
144416 KB |
Output is correct |
9 |
Correct |
29 ms |
144220 KB |
Output is correct |
10 |
Correct |
28 ms |
144208 KB |
Output is correct |
11 |
Correct |
29 ms |
144240 KB |
Output is correct |
12 |
Correct |
28 ms |
144216 KB |
Output is correct |
13 |
Correct |
29 ms |
144208 KB |
Output is correct |
14 |
Correct |
29 ms |
144212 KB |
Output is correct |
15 |
Correct |
30 ms |
144476 KB |
Output is correct |
16 |
Correct |
28 ms |
144220 KB |
Output is correct |
17 |
Correct |
28 ms |
144120 KB |
Output is correct |
18 |
Correct |
28 ms |
144220 KB |
Output is correct |
19 |
Correct |
28 ms |
144196 KB |
Output is correct |
20 |
Correct |
28 ms |
144256 KB |
Output is correct |
21 |
Correct |
29 ms |
144216 KB |
Output is correct |
22 |
Correct |
29 ms |
144472 KB |
Output is correct |
23 |
Correct |
29 ms |
144220 KB |
Output is correct |
24 |
Correct |
31 ms |
144208 KB |
Output is correct |
25 |
Correct |
30 ms |
144216 KB |
Output is correct |
26 |
Correct |
28 ms |
144400 KB |
Output is correct |
27 |
Correct |
28 ms |
144220 KB |
Output is correct |
28 |
Correct |
28 ms |
144212 KB |
Output is correct |
29 |
Correct |
29 ms |
144220 KB |
Output is correct |
30 |
Correct |
28 ms |
144220 KB |
Output is correct |
31 |
Correct |
31 ms |
144300 KB |
Output is correct |
32 |
Correct |
28 ms |
144220 KB |
Output is correct |
33 |
Correct |
28 ms |
144204 KB |
Output is correct |
34 |
Correct |
33 ms |
144720 KB |
Output is correct |
35 |
Correct |
408 ms |
225104 KB |
Output is correct |
36 |
Correct |
1533 ms |
468092 KB |
Output is correct |
37 |
Correct |
1537 ms |
474208 KB |
Output is correct |
38 |
Correct |
1486 ms |
472296 KB |
Output is correct |
39 |
Correct |
284 ms |
238324 KB |
Output is correct |
40 |
Correct |
33 ms |
145460 KB |
Output is correct |
41 |
Correct |
34 ms |
145488 KB |
Output is correct |
42 |
Correct |
278 ms |
238172 KB |
Output is correct |
43 |
Correct |
612 ms |
332628 KB |
Output is correct |
44 |
Correct |
575 ms |
330596 KB |
Output is correct |
45 |
Correct |
288 ms |
238400 KB |
Output is correct |
46 |
Correct |
281 ms |
238276 KB |
Output is correct |
47 |
Correct |
91 ms |
163156 KB |
Output is correct |
48 |
Correct |
102 ms |
164036 KB |
Output is correct |
49 |
Correct |
427 ms |
254884 KB |
Output is correct |
50 |
Correct |
1506 ms |
466792 KB |
Output is correct |
51 |
Correct |
659 ms |
332724 KB |
Output is correct |
52 |
Correct |
1323 ms |
437572 KB |
Output is correct |
53 |
Correct |
535 ms |
282488 KB |
Output is correct |
54 |
Correct |
1515 ms |
460932 KB |
Output is correct |
55 |
Correct |
729 ms |
346668 KB |
Output is correct |
56 |
Correct |
725 ms |
345444 KB |
Output is correct |
57 |
Correct |
569 ms |
307136 KB |
Output is correct |
58 |
Correct |
580 ms |
309556 KB |
Output is correct |
59 |
Correct |
33 ms |
144988 KB |
Output is correct |
60 |
Correct |
101 ms |
149760 KB |
Output is correct |
61 |
Correct |
204 ms |
200168 KB |
Output is correct |
62 |
Correct |
1523 ms |
465204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
144212 KB |
Output is correct |
2 |
Correct |
28 ms |
144216 KB |
Output is correct |
3 |
Correct |
28 ms |
144212 KB |
Output is correct |
4 |
Correct |
28 ms |
144276 KB |
Output is correct |
5 |
Correct |
27 ms |
144220 KB |
Output is correct |
6 |
Correct |
28 ms |
144272 KB |
Output is correct |
7 |
Correct |
29 ms |
144412 KB |
Output is correct |
8 |
Correct |
30 ms |
144416 KB |
Output is correct |
9 |
Correct |
29 ms |
144220 KB |
Output is correct |
10 |
Correct |
28 ms |
144208 KB |
Output is correct |
11 |
Correct |
29 ms |
144240 KB |
Output is correct |
12 |
Correct |
28 ms |
144216 KB |
Output is correct |
13 |
Correct |
29 ms |
144208 KB |
Output is correct |
14 |
Correct |
29 ms |
144212 KB |
Output is correct |
15 |
Correct |
30 ms |
144476 KB |
Output is correct |
16 |
Correct |
28 ms |
144220 KB |
Output is correct |
17 |
Correct |
28 ms |
144120 KB |
Output is correct |
18 |
Correct |
28 ms |
144220 KB |
Output is correct |
19 |
Correct |
28 ms |
144196 KB |
Output is correct |
20 |
Correct |
28 ms |
144256 KB |
Output is correct |
21 |
Correct |
29 ms |
144216 KB |
Output is correct |
22 |
Correct |
29 ms |
144472 KB |
Output is correct |
23 |
Correct |
29 ms |
144220 KB |
Output is correct |
24 |
Correct |
31 ms |
144208 KB |
Output is correct |
25 |
Correct |
30 ms |
144216 KB |
Output is correct |
26 |
Correct |
28 ms |
144400 KB |
Output is correct |
27 |
Correct |
28 ms |
144220 KB |
Output is correct |
28 |
Correct |
28 ms |
144212 KB |
Output is correct |
29 |
Correct |
29 ms |
144220 KB |
Output is correct |
30 |
Correct |
28 ms |
144220 KB |
Output is correct |
31 |
Correct |
31 ms |
144300 KB |
Output is correct |
32 |
Correct |
28 ms |
144220 KB |
Output is correct |
33 |
Correct |
28 ms |
144204 KB |
Output is correct |
34 |
Correct |
33 ms |
144720 KB |
Output is correct |
35 |
Correct |
408 ms |
225104 KB |
Output is correct |
36 |
Correct |
1533 ms |
468092 KB |
Output is correct |
37 |
Correct |
1537 ms |
474208 KB |
Output is correct |
38 |
Correct |
1486 ms |
472296 KB |
Output is correct |
39 |
Correct |
284 ms |
238324 KB |
Output is correct |
40 |
Correct |
33 ms |
145460 KB |
Output is correct |
41 |
Correct |
34 ms |
145488 KB |
Output is correct |
42 |
Correct |
278 ms |
238172 KB |
Output is correct |
43 |
Correct |
612 ms |
332628 KB |
Output is correct |
44 |
Correct |
575 ms |
330596 KB |
Output is correct |
45 |
Correct |
288 ms |
238400 KB |
Output is correct |
46 |
Correct |
281 ms |
238276 KB |
Output is correct |
47 |
Correct |
91 ms |
163156 KB |
Output is correct |
48 |
Correct |
102 ms |
164036 KB |
Output is correct |
49 |
Correct |
427 ms |
254884 KB |
Output is correct |
50 |
Correct |
1506 ms |
466792 KB |
Output is correct |
51 |
Correct |
659 ms |
332724 KB |
Output is correct |
52 |
Correct |
1323 ms |
437572 KB |
Output is correct |
53 |
Correct |
535 ms |
282488 KB |
Output is correct |
54 |
Correct |
1515 ms |
460932 KB |
Output is correct |
55 |
Correct |
729 ms |
346668 KB |
Output is correct |
56 |
Correct |
725 ms |
345444 KB |
Output is correct |
57 |
Correct |
569 ms |
307136 KB |
Output is correct |
58 |
Correct |
580 ms |
309556 KB |
Output is correct |
59 |
Correct |
33 ms |
144988 KB |
Output is correct |
60 |
Correct |
101 ms |
149760 KB |
Output is correct |
61 |
Correct |
204 ms |
200168 KB |
Output is correct |
62 |
Correct |
1523 ms |
465204 KB |
Output is correct |
63 |
Correct |
570 ms |
196784 KB |
Output is correct |
64 |
Correct |
566 ms |
196664 KB |
Output is correct |
65 |
Correct |
555 ms |
196676 KB |
Output is correct |
66 |
Runtime error |
2592 ms |
1048576 KB |
Execution killed with signal 9 |
67 |
Halted |
0 ms |
0 KB |
- |