#include <bits/stdc++.h>
#define ll long long
#define ldb long double
#define endl '\n'
#define For(i,l,r) for(int i=l;i<=r;i++)
#define ForD(i,r,l) for(int i=r;i>=l;i--)
#define REP(i,l,r) For(i,l,r-1)
#define PER(i,r,l) ForD(i,r-1,l)
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
#define All(x,n) x+1,x+1+n
#define Alll(x,n) x,x+n
#define sz(x) (signed)x.size()
#define unq(x) x.resize(unique(all(x))-x.begin())
#define mpa make_pair
#ifdef NCGM
#include"debug.h"
#else
#define debug(...) "fr";
#endif
using namespace std;
const int N=3e5+3;
int n,m,f[N],f1[N],mmb[N];
ll ans=0;
map<int,int> mp[N],mp1[N];
set<int> st[N];
vector<int> ahu;
queue<pair<int,int>> q;
int find_set(int u) {
return (f[u]<0?u:f[u]=find_set(f[u]));
}
int find_set1(int u) {
return (f1[u]<0?u:f1[u]=find_set1(f1[u]));
}
void unite(int u,int v) {
int a=find_set(u),b=find_set(v);
if (a==b) return;
int a1=find_set1(u),b1=find_set1(v);
ans+=1LL*f[a]*(sz(st[a1])-1);
ans+=1LL*f[b]*(sz(st[b1])-1);
if (sz(st[a1])<sz(st[b1])) swap(a1,b1);
if (sz(mp[a])+sz(mp1[a])<sz(mp[b])+sz(mp1[b])) swap(a,b);
ahu.clear();
for(auto el: mp[b]) {
ahu.pb(el.ff);
if (mp[find_set(el.ff)].count(a) || mp[find_set(el.ff)].count(b)) q.push({el.ff,a});
}
for(auto el: mp1[b]) {
if (mp1[el.ff].count(a) || mp1[el.ff].count(b)) q.push({el.ff,a});
}
for(auto el: mp1[b]) mp[el.ff][a]=mp[el.ff][b];
for(auto el: ahu) mp1[el][a]=mp1[el][b];
for(auto el: st[b1]) st[a1].insert(el);
st[b1].clear();
for(auto el: mp[b]) mp[a][el.ff]+=el.ss;
mp[b].clear();
for(auto el: mp1[b]) mp1[a][el.ff]+=el.ss;
mp1[b].clear();
f[a]+=f[b];
f1[a1]+=f1[b1];
f[b]=a;
f1[b1]=a1;
ans-=1LL*f[a]*(sz(st[find_set1(a)])-1);
}
void add_edge(int u,int v) {
if (find_set(u)==find_set(v) || st[find_set1(v)].find(u)!=st[find_set1(v)].end()) return;
mp[find_set(v)][find_set(u)]++;
mp1[find_set(u)][find_set(v)]++;
if (st[find_set1(v)].find(u)==st[find_set1(v)].end()) ans-=f[find_set(v)];
st[find_set1(v)].insert(u);
// if (u==4 && v==2) debug(find_set(2));
if (mp[find_set(u)].count(find_set(v))) unite(u,v);
while(sz(q)) {
unite(q.front().ff,q.front().ss);
q.pop();
}
}
int main() {
cin.tie(0)->sync_with_stdio(0);
// freopen("B.INP","r",stdin);
// freopen("B.OUT","w",stdout);
cin >> n >> m;
For(i,1,n) f[i]=f1[i]=-1;
For(i,1,n) st[i].insert(i);
For(i,1,m) {
int u,v;
cin >> u >> v;
add_edge(u,v);
cout << ans << endl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
45648 KB |
Output is correct |
2 |
Correct |
7 ms |
45764 KB |
Output is correct |
3 |
Correct |
7 ms |
45648 KB |
Output is correct |
4 |
Correct |
7 ms |
45704 KB |
Output is correct |
5 |
Correct |
8 ms |
45648 KB |
Output is correct |
6 |
Correct |
8 ms |
45648 KB |
Output is correct |
7 |
Correct |
7 ms |
45788 KB |
Output is correct |
8 |
Correct |
8 ms |
45816 KB |
Output is correct |
9 |
Correct |
8 ms |
45648 KB |
Output is correct |
10 |
Correct |
7 ms |
45648 KB |
Output is correct |
11 |
Correct |
9 ms |
45648 KB |
Output is correct |
12 |
Correct |
7 ms |
45772 KB |
Output is correct |
13 |
Correct |
7 ms |
45648 KB |
Output is correct |
14 |
Correct |
9 ms |
45772 KB |
Output is correct |
15 |
Correct |
8 ms |
45648 KB |
Output is correct |
16 |
Correct |
8 ms |
45648 KB |
Output is correct |
17 |
Correct |
8 ms |
45648 KB |
Output is correct |
18 |
Correct |
8 ms |
45648 KB |
Output is correct |
19 |
Correct |
8 ms |
45648 KB |
Output is correct |
20 |
Correct |
8 ms |
45648 KB |
Output is correct |
21 |
Correct |
10 ms |
45648 KB |
Output is correct |
22 |
Correct |
8 ms |
45648 KB |
Output is correct |
23 |
Correct |
10 ms |
45648 KB |
Output is correct |
24 |
Correct |
9 ms |
45760 KB |
Output is correct |
25 |
Correct |
9 ms |
45648 KB |
Output is correct |
26 |
Correct |
8 ms |
45792 KB |
Output is correct |
27 |
Correct |
8 ms |
45648 KB |
Output is correct |
28 |
Correct |
7 ms |
45648 KB |
Output is correct |
29 |
Correct |
7 ms |
45648 KB |
Output is correct |
30 |
Correct |
7 ms |
45648 KB |
Output is correct |
31 |
Correct |
8 ms |
45824 KB |
Output is correct |
32 |
Correct |
8 ms |
45648 KB |
Output is correct |
33 |
Correct |
8 ms |
45840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
45648 KB |
Output is correct |
2 |
Correct |
7 ms |
45764 KB |
Output is correct |
3 |
Correct |
7 ms |
45648 KB |
Output is correct |
4 |
Correct |
7 ms |
45704 KB |
Output is correct |
5 |
Correct |
8 ms |
45648 KB |
Output is correct |
6 |
Correct |
8 ms |
45648 KB |
Output is correct |
7 |
Correct |
7 ms |
45788 KB |
Output is correct |
8 |
Correct |
8 ms |
45816 KB |
Output is correct |
9 |
Correct |
8 ms |
45648 KB |
Output is correct |
10 |
Correct |
7 ms |
45648 KB |
Output is correct |
11 |
Correct |
9 ms |
45648 KB |
Output is correct |
12 |
Correct |
7 ms |
45772 KB |
Output is correct |
13 |
Correct |
7 ms |
45648 KB |
Output is correct |
14 |
Correct |
9 ms |
45772 KB |
Output is correct |
15 |
Correct |
8 ms |
45648 KB |
Output is correct |
16 |
Correct |
8 ms |
45648 KB |
Output is correct |
17 |
Correct |
8 ms |
45648 KB |
Output is correct |
18 |
Correct |
8 ms |
45648 KB |
Output is correct |
19 |
Correct |
8 ms |
45648 KB |
Output is correct |
20 |
Correct |
8 ms |
45648 KB |
Output is correct |
21 |
Correct |
10 ms |
45648 KB |
Output is correct |
22 |
Correct |
8 ms |
45648 KB |
Output is correct |
23 |
Correct |
10 ms |
45648 KB |
Output is correct |
24 |
Correct |
9 ms |
45760 KB |
Output is correct |
25 |
Correct |
9 ms |
45648 KB |
Output is correct |
26 |
Correct |
8 ms |
45792 KB |
Output is correct |
27 |
Correct |
8 ms |
45648 KB |
Output is correct |
28 |
Correct |
7 ms |
45648 KB |
Output is correct |
29 |
Correct |
7 ms |
45648 KB |
Output is correct |
30 |
Correct |
7 ms |
45648 KB |
Output is correct |
31 |
Correct |
8 ms |
45824 KB |
Output is correct |
32 |
Correct |
8 ms |
45648 KB |
Output is correct |
33 |
Correct |
8 ms |
45840 KB |
Output is correct |
34 |
Correct |
9 ms |
45904 KB |
Output is correct |
35 |
Correct |
63 ms |
50540 KB |
Output is correct |
36 |
Correct |
100 ms |
54336 KB |
Output is correct |
37 |
Correct |
117 ms |
54220 KB |
Output is correct |
38 |
Correct |
90 ms |
53832 KB |
Output is correct |
39 |
Correct |
12 ms |
45904 KB |
Output is correct |
40 |
Correct |
16 ms |
46832 KB |
Output is correct |
41 |
Correct |
17 ms |
46928 KB |
Output is correct |
42 |
Correct |
11 ms |
45904 KB |
Output is correct |
43 |
Correct |
13 ms |
45904 KB |
Output is correct |
44 |
Correct |
15 ms |
46124 KB |
Output is correct |
45 |
Correct |
12 ms |
45904 KB |
Output is correct |
46 |
Correct |
12 ms |
45904 KB |
Output is correct |
47 |
Correct |
18 ms |
46416 KB |
Output is correct |
48 |
Correct |
15 ms |
46616 KB |
Output is correct |
49 |
Correct |
22 ms |
47132 KB |
Output is correct |
50 |
Correct |
90 ms |
54344 KB |
Output is correct |
51 |
Correct |
15 ms |
46476 KB |
Output is correct |
52 |
Correct |
72 ms |
52376 KB |
Output is correct |
53 |
Correct |
20 ms |
46928 KB |
Output is correct |
54 |
Correct |
86 ms |
52552 KB |
Output is correct |
55 |
Correct |
15 ms |
46416 KB |
Output is correct |
56 |
Correct |
13 ms |
46416 KB |
Output is correct |
57 |
Correct |
14 ms |
46416 KB |
Output is correct |
58 |
Correct |
13 ms |
46416 KB |
Output is correct |
59 |
Correct |
19 ms |
47320 KB |
Output is correct |
60 |
Correct |
65 ms |
51740 KB |
Output is correct |
61 |
Correct |
17 ms |
46416 KB |
Output is correct |
62 |
Correct |
88 ms |
53732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
45648 KB |
Output is correct |
2 |
Correct |
7 ms |
45764 KB |
Output is correct |
3 |
Correct |
7 ms |
45648 KB |
Output is correct |
4 |
Correct |
7 ms |
45704 KB |
Output is correct |
5 |
Correct |
8 ms |
45648 KB |
Output is correct |
6 |
Correct |
8 ms |
45648 KB |
Output is correct |
7 |
Correct |
7 ms |
45788 KB |
Output is correct |
8 |
Correct |
8 ms |
45816 KB |
Output is correct |
9 |
Correct |
8 ms |
45648 KB |
Output is correct |
10 |
Correct |
7 ms |
45648 KB |
Output is correct |
11 |
Correct |
9 ms |
45648 KB |
Output is correct |
12 |
Correct |
7 ms |
45772 KB |
Output is correct |
13 |
Correct |
7 ms |
45648 KB |
Output is correct |
14 |
Correct |
9 ms |
45772 KB |
Output is correct |
15 |
Correct |
8 ms |
45648 KB |
Output is correct |
16 |
Correct |
8 ms |
45648 KB |
Output is correct |
17 |
Correct |
8 ms |
45648 KB |
Output is correct |
18 |
Correct |
8 ms |
45648 KB |
Output is correct |
19 |
Correct |
8 ms |
45648 KB |
Output is correct |
20 |
Correct |
8 ms |
45648 KB |
Output is correct |
21 |
Correct |
10 ms |
45648 KB |
Output is correct |
22 |
Correct |
8 ms |
45648 KB |
Output is correct |
23 |
Correct |
10 ms |
45648 KB |
Output is correct |
24 |
Correct |
9 ms |
45760 KB |
Output is correct |
25 |
Correct |
9 ms |
45648 KB |
Output is correct |
26 |
Correct |
8 ms |
45792 KB |
Output is correct |
27 |
Correct |
8 ms |
45648 KB |
Output is correct |
28 |
Correct |
7 ms |
45648 KB |
Output is correct |
29 |
Correct |
7 ms |
45648 KB |
Output is correct |
30 |
Correct |
7 ms |
45648 KB |
Output is correct |
31 |
Correct |
8 ms |
45824 KB |
Output is correct |
32 |
Correct |
8 ms |
45648 KB |
Output is correct |
33 |
Correct |
8 ms |
45840 KB |
Output is correct |
34 |
Correct |
9 ms |
45904 KB |
Output is correct |
35 |
Correct |
63 ms |
50540 KB |
Output is correct |
36 |
Correct |
100 ms |
54336 KB |
Output is correct |
37 |
Correct |
117 ms |
54220 KB |
Output is correct |
38 |
Correct |
90 ms |
53832 KB |
Output is correct |
39 |
Correct |
12 ms |
45904 KB |
Output is correct |
40 |
Correct |
16 ms |
46832 KB |
Output is correct |
41 |
Correct |
17 ms |
46928 KB |
Output is correct |
42 |
Correct |
11 ms |
45904 KB |
Output is correct |
43 |
Correct |
13 ms |
45904 KB |
Output is correct |
44 |
Correct |
15 ms |
46124 KB |
Output is correct |
45 |
Correct |
12 ms |
45904 KB |
Output is correct |
46 |
Correct |
12 ms |
45904 KB |
Output is correct |
47 |
Correct |
18 ms |
46416 KB |
Output is correct |
48 |
Correct |
15 ms |
46616 KB |
Output is correct |
49 |
Correct |
22 ms |
47132 KB |
Output is correct |
50 |
Correct |
90 ms |
54344 KB |
Output is correct |
51 |
Correct |
15 ms |
46476 KB |
Output is correct |
52 |
Correct |
72 ms |
52376 KB |
Output is correct |
53 |
Correct |
20 ms |
46928 KB |
Output is correct |
54 |
Correct |
86 ms |
52552 KB |
Output is correct |
55 |
Correct |
15 ms |
46416 KB |
Output is correct |
56 |
Correct |
13 ms |
46416 KB |
Output is correct |
57 |
Correct |
14 ms |
46416 KB |
Output is correct |
58 |
Correct |
13 ms |
46416 KB |
Output is correct |
59 |
Correct |
19 ms |
47320 KB |
Output is correct |
60 |
Correct |
65 ms |
51740 KB |
Output is correct |
61 |
Correct |
17 ms |
46416 KB |
Output is correct |
62 |
Correct |
88 ms |
53732 KB |
Output is correct |
63 |
Correct |
369 ms |
97616 KB |
Output is correct |
64 |
Correct |
354 ms |
97676 KB |
Output is correct |
65 |
Correct |
399 ms |
97608 KB |
Output is correct |
66 |
Correct |
252 ms |
63904 KB |
Output is correct |
67 |
Correct |
1074 ms |
123588 KB |
Output is correct |
68 |
Correct |
274 ms |
63688 KB |
Output is correct |
69 |
Correct |
403 ms |
64584 KB |
Output is correct |
70 |
Correct |
306 ms |
63816 KB |
Output is correct |
71 |
Correct |
285 ms |
63816 KB |
Output is correct |
72 |
Correct |
746 ms |
88648 KB |
Output is correct |
73 |
Correct |
697 ms |
92232 KB |
Output is correct |
74 |
Correct |
1542 ms |
111784 KB |
Output is correct |
75 |
Correct |
607 ms |
71620 KB |
Output is correct |
76 |
Correct |
936 ms |
86344 KB |
Output is correct |
77 |
Correct |
984 ms |
87696 KB |
Output is correct |
78 |
Correct |
211 ms |
67728 KB |
Output is correct |
79 |
Correct |
361 ms |
71508 KB |
Output is correct |
80 |
Correct |
215 ms |
67316 KB |
Output is correct |
81 |
Correct |
410 ms |
70992 KB |
Output is correct |
82 |
Correct |
690 ms |
83528 KB |
Output is correct |
83 |
Correct |
743 ms |
82764 KB |
Output is correct |
84 |
Correct |
656 ms |
82064 KB |
Output is correct |
85 |
Correct |
614 ms |
81992 KB |
Output is correct |
86 |
Correct |
1022 ms |
141252 KB |
Output is correct |
87 |
Correct |
1064 ms |
142984 KB |
Output is correct |
88 |
Correct |
691 ms |
90884 KB |
Output is correct |
89 |
Correct |
837 ms |
85304 KB |
Output is correct |