#include "bits/stdc++.h"
using namespace std;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define ph push
#define f first
#define s second
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) { return (rng() % (y+1-x)) + x; } //inclusivesss
string inline to_string(char c) {string s(1,c);return s;} template<typename T> inline T gcd(T a,T b){ return a==0?llabs(b):gcd(b%a,a); }
typedef long long ll;
typedef long double ld;
#define FOR(i,s,e) for(ll i=s;i<=ll(e);++i)
#define DEC(i,s,e) for(ll i=s;i>=ll(e);--i)
typedef pair<ll,ll>pi; typedef pair<ll,pi>spi; typedef pair<pi,pi>dpi;
#define LLINF ((long long)1e18)
#define INF int(1e9+1e6)
#define MAXN (300006)
ll n,m,ans,fake[MAXN],deg[MAXN]; // rmbr to pass down everything including fake and deg!!!!
struct ufds_ {
int p[MAXN],sz[MAXN];
ufds_() {
FOR(i,0,MAXN-1) p[i]=i, sz[i]=1;
}
void merge(int x,int y){
x=find(x),y=find(y);
assert(x^y);
p[x]=y, sz[y]+=sz[x];
}
int find(int x) { return (p[x]==x)?x:p[x]=find(p[x]); }
ll get_sz(ll x) { return sz[find(x)]; }
} ufds;
ll nc2(ll n){ return n*(n-1); }
set<ll> v[MAXN],bck[MAXN];
map<ll,ll>howmany[MAXN];
queue<pi> q;
int main(){
FAST
cin>>n>>m;
auto solve=[&](ll a,ll b){
if(a==b)return;
assert(v[a].count(b)), assert(v[b].count(a));
v[a].erase(b), v[b].erase(a);
ans-=nc2(ufds.get_sz(b)), ans-=nc2(ufds.get_sz(a)), ans+=nc2(ufds.get_sz(a)+ufds.get_sz(b));
ans-=ufds.get_sz(a)*howmany[a][b], ans-=ufds.get_sz(b)*howmany[b][a];
// assert(howmany[b][a]==0);
// check if need swap;
if(siz(v[a])+siz(bck[a])>siz(v[b])+siz(bck[b]))swap(a,b);
// swap(a,b);
fake[b]+=howmany[b][a], howmany[b][a]=0;
for(auto i:v[a]) {
assert(i==ufds.find(i));
howmany[i][b]+=howmany[i][a];
howmany[i].erase(a);
v[b].ins(i);
if(v[i].count(b)) {
q.emplace(b, i);
}
}
v[a].clear();
ans+=ufds.get_sz(a)*(siz(bck[b])-fake[b]);
for(auto i:bck[a]){
if(ufds.find(i)==b||ufds.find(i)==a) continue;
if(v[b].count(ufds.find(i))){
q.emplace(b, i);
}
v[ufds.find(i)].erase(a), v[ufds.find(i)].ins(b);
if(bck[b].find(i)!=bck[b].end()) ans-=ufds.get_sz(a);
else bck[b].ins(i), howmany[b][ufds.find(i)]++, ans+=ufds.get_sz(b);
}
howmany[a].clear();
bck[a].clear();
deg[b]+=deg[a];
ufds.merge(a,b);
};
FOR(i,1,m){
ll a,b;cin>>a>>b;
ll oa=a;
a=ufds.find(a), b=ufds.find(b);
if(a==b){
cout<<ans<<'\n';
continue;
}
if(v[b].find(a)!=v[b].end()){
assert(bck[b].find(oa)==bck[b].end());
v[a].ins(b);
bck[b].ins(oa);
++ howmany[b][a];
ans+=ufds.get_sz(b);
q.emplace(a, b);
while(q.size()){
solve(ufds.find(q.front().f),ufds.find(q.front().s));
q.pop();
}
}else{
if(bck[b].find(oa)==bck[b].end()){
v[a].ins(b), bck[b].ins(oa);
++ deg[a], ++deg[b];
++ howmany[b][a]; // [receiving end][giving end]
ans+=ufds.get_sz(b);
}
}
cout<<ans<<'\n';
}
}
/*
3 4
3 2
1 3
2 1
1 2
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
44928 KB |
Output is correct |
2 |
Correct |
27 ms |
44928 KB |
Output is correct |
3 |
Correct |
28 ms |
45056 KB |
Output is correct |
4 |
Correct |
30 ms |
44928 KB |
Output is correct |
5 |
Correct |
27 ms |
45056 KB |
Output is correct |
6 |
Correct |
28 ms |
44928 KB |
Output is correct |
7 |
Correct |
29 ms |
45056 KB |
Output is correct |
8 |
Correct |
27 ms |
45056 KB |
Output is correct |
9 |
Correct |
29 ms |
45056 KB |
Output is correct |
10 |
Correct |
28 ms |
44928 KB |
Output is correct |
11 |
Correct |
28 ms |
45056 KB |
Output is correct |
12 |
Correct |
28 ms |
44928 KB |
Output is correct |
13 |
Correct |
27 ms |
45056 KB |
Output is correct |
14 |
Correct |
29 ms |
44928 KB |
Output is correct |
15 |
Correct |
27 ms |
45056 KB |
Output is correct |
16 |
Correct |
27 ms |
44928 KB |
Output is correct |
17 |
Correct |
27 ms |
44928 KB |
Output is correct |
18 |
Correct |
28 ms |
44960 KB |
Output is correct |
19 |
Correct |
27 ms |
44928 KB |
Output is correct |
20 |
Correct |
28 ms |
44928 KB |
Output is correct |
21 |
Correct |
28 ms |
45056 KB |
Output is correct |
22 |
Correct |
28 ms |
44928 KB |
Output is correct |
23 |
Correct |
28 ms |
45056 KB |
Output is correct |
24 |
Correct |
27 ms |
45048 KB |
Output is correct |
25 |
Correct |
28 ms |
45056 KB |
Output is correct |
26 |
Correct |
27 ms |
44928 KB |
Output is correct |
27 |
Correct |
29 ms |
44928 KB |
Output is correct |
28 |
Correct |
27 ms |
44928 KB |
Output is correct |
29 |
Correct |
31 ms |
45184 KB |
Output is correct |
30 |
Correct |
27 ms |
45056 KB |
Output is correct |
31 |
Correct |
28 ms |
44928 KB |
Output is correct |
32 |
Correct |
27 ms |
44928 KB |
Output is correct |
33 |
Correct |
28 ms |
45056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
44928 KB |
Output is correct |
2 |
Correct |
27 ms |
44928 KB |
Output is correct |
3 |
Correct |
28 ms |
45056 KB |
Output is correct |
4 |
Correct |
30 ms |
44928 KB |
Output is correct |
5 |
Correct |
27 ms |
45056 KB |
Output is correct |
6 |
Correct |
28 ms |
44928 KB |
Output is correct |
7 |
Correct |
29 ms |
45056 KB |
Output is correct |
8 |
Correct |
27 ms |
45056 KB |
Output is correct |
9 |
Correct |
29 ms |
45056 KB |
Output is correct |
10 |
Correct |
28 ms |
44928 KB |
Output is correct |
11 |
Correct |
28 ms |
45056 KB |
Output is correct |
12 |
Correct |
28 ms |
44928 KB |
Output is correct |
13 |
Correct |
27 ms |
45056 KB |
Output is correct |
14 |
Correct |
29 ms |
44928 KB |
Output is correct |
15 |
Correct |
27 ms |
45056 KB |
Output is correct |
16 |
Correct |
27 ms |
44928 KB |
Output is correct |
17 |
Correct |
27 ms |
44928 KB |
Output is correct |
18 |
Correct |
28 ms |
44960 KB |
Output is correct |
19 |
Correct |
27 ms |
44928 KB |
Output is correct |
20 |
Correct |
28 ms |
44928 KB |
Output is correct |
21 |
Correct |
28 ms |
45056 KB |
Output is correct |
22 |
Correct |
28 ms |
44928 KB |
Output is correct |
23 |
Correct |
28 ms |
45056 KB |
Output is correct |
24 |
Correct |
27 ms |
45048 KB |
Output is correct |
25 |
Correct |
28 ms |
45056 KB |
Output is correct |
26 |
Correct |
27 ms |
44928 KB |
Output is correct |
27 |
Correct |
29 ms |
44928 KB |
Output is correct |
28 |
Correct |
27 ms |
44928 KB |
Output is correct |
29 |
Correct |
31 ms |
45184 KB |
Output is correct |
30 |
Correct |
27 ms |
45056 KB |
Output is correct |
31 |
Correct |
28 ms |
44928 KB |
Output is correct |
32 |
Correct |
27 ms |
44928 KB |
Output is correct |
33 |
Correct |
28 ms |
45056 KB |
Output is correct |
34 |
Correct |
30 ms |
45048 KB |
Output is correct |
35 |
Correct |
117 ms |
48708 KB |
Output is correct |
36 |
Correct |
148 ms |
51196 KB |
Output is correct |
37 |
Correct |
138 ms |
51320 KB |
Output is correct |
38 |
Correct |
136 ms |
50912 KB |
Output is correct |
39 |
Correct |
31 ms |
45304 KB |
Output is correct |
40 |
Correct |
31 ms |
45312 KB |
Output is correct |
41 |
Correct |
31 ms |
45312 KB |
Output is correct |
42 |
Correct |
31 ms |
45184 KB |
Output is correct |
43 |
Correct |
32 ms |
45304 KB |
Output is correct |
44 |
Correct |
33 ms |
45304 KB |
Output is correct |
45 |
Correct |
30 ms |
45312 KB |
Output is correct |
46 |
Correct |
33 ms |
45304 KB |
Output is correct |
47 |
Correct |
31 ms |
45304 KB |
Output is correct |
48 |
Correct |
31 ms |
45312 KB |
Output is correct |
49 |
Correct |
38 ms |
45952 KB |
Output is correct |
50 |
Correct |
148 ms |
51512 KB |
Output is correct |
51 |
Correct |
35 ms |
45440 KB |
Output is correct |
52 |
Correct |
120 ms |
49528 KB |
Output is correct |
53 |
Correct |
38 ms |
45816 KB |
Output is correct |
54 |
Correct |
134 ms |
50552 KB |
Output is correct |
55 |
Correct |
33 ms |
45688 KB |
Output is correct |
56 |
Correct |
34 ms |
45696 KB |
Output is correct |
57 |
Correct |
34 ms |
45696 KB |
Output is correct |
58 |
Correct |
35 ms |
45568 KB |
Output is correct |
59 |
Correct |
32 ms |
45184 KB |
Output is correct |
60 |
Correct |
104 ms |
47608 KB |
Output is correct |
61 |
Correct |
32 ms |
45304 KB |
Output is correct |
62 |
Correct |
142 ms |
50844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
44928 KB |
Output is correct |
2 |
Correct |
27 ms |
44928 KB |
Output is correct |
3 |
Correct |
28 ms |
45056 KB |
Output is correct |
4 |
Correct |
30 ms |
44928 KB |
Output is correct |
5 |
Correct |
27 ms |
45056 KB |
Output is correct |
6 |
Correct |
28 ms |
44928 KB |
Output is correct |
7 |
Correct |
29 ms |
45056 KB |
Output is correct |
8 |
Correct |
27 ms |
45056 KB |
Output is correct |
9 |
Correct |
29 ms |
45056 KB |
Output is correct |
10 |
Correct |
28 ms |
44928 KB |
Output is correct |
11 |
Correct |
28 ms |
45056 KB |
Output is correct |
12 |
Correct |
28 ms |
44928 KB |
Output is correct |
13 |
Correct |
27 ms |
45056 KB |
Output is correct |
14 |
Correct |
29 ms |
44928 KB |
Output is correct |
15 |
Correct |
27 ms |
45056 KB |
Output is correct |
16 |
Correct |
27 ms |
44928 KB |
Output is correct |
17 |
Correct |
27 ms |
44928 KB |
Output is correct |
18 |
Correct |
28 ms |
44960 KB |
Output is correct |
19 |
Correct |
27 ms |
44928 KB |
Output is correct |
20 |
Correct |
28 ms |
44928 KB |
Output is correct |
21 |
Correct |
28 ms |
45056 KB |
Output is correct |
22 |
Correct |
28 ms |
44928 KB |
Output is correct |
23 |
Correct |
28 ms |
45056 KB |
Output is correct |
24 |
Correct |
27 ms |
45048 KB |
Output is correct |
25 |
Correct |
28 ms |
45056 KB |
Output is correct |
26 |
Correct |
27 ms |
44928 KB |
Output is correct |
27 |
Correct |
29 ms |
44928 KB |
Output is correct |
28 |
Correct |
27 ms |
44928 KB |
Output is correct |
29 |
Correct |
31 ms |
45184 KB |
Output is correct |
30 |
Correct |
27 ms |
45056 KB |
Output is correct |
31 |
Correct |
28 ms |
44928 KB |
Output is correct |
32 |
Correct |
27 ms |
44928 KB |
Output is correct |
33 |
Correct |
28 ms |
45056 KB |
Output is correct |
34 |
Correct |
30 ms |
45048 KB |
Output is correct |
35 |
Correct |
117 ms |
48708 KB |
Output is correct |
36 |
Correct |
148 ms |
51196 KB |
Output is correct |
37 |
Correct |
138 ms |
51320 KB |
Output is correct |
38 |
Correct |
136 ms |
50912 KB |
Output is correct |
39 |
Correct |
31 ms |
45304 KB |
Output is correct |
40 |
Correct |
31 ms |
45312 KB |
Output is correct |
41 |
Correct |
31 ms |
45312 KB |
Output is correct |
42 |
Correct |
31 ms |
45184 KB |
Output is correct |
43 |
Correct |
32 ms |
45304 KB |
Output is correct |
44 |
Correct |
33 ms |
45304 KB |
Output is correct |
45 |
Correct |
30 ms |
45312 KB |
Output is correct |
46 |
Correct |
33 ms |
45304 KB |
Output is correct |
47 |
Correct |
31 ms |
45304 KB |
Output is correct |
48 |
Correct |
31 ms |
45312 KB |
Output is correct |
49 |
Correct |
38 ms |
45952 KB |
Output is correct |
50 |
Correct |
148 ms |
51512 KB |
Output is correct |
51 |
Correct |
35 ms |
45440 KB |
Output is correct |
52 |
Correct |
120 ms |
49528 KB |
Output is correct |
53 |
Correct |
38 ms |
45816 KB |
Output is correct |
54 |
Correct |
134 ms |
50552 KB |
Output is correct |
55 |
Correct |
33 ms |
45688 KB |
Output is correct |
56 |
Correct |
34 ms |
45696 KB |
Output is correct |
57 |
Correct |
34 ms |
45696 KB |
Output is correct |
58 |
Correct |
35 ms |
45568 KB |
Output is correct |
59 |
Correct |
32 ms |
45184 KB |
Output is correct |
60 |
Correct |
104 ms |
47608 KB |
Output is correct |
61 |
Correct |
32 ms |
45304 KB |
Output is correct |
62 |
Correct |
142 ms |
50844 KB |
Output is correct |
63 |
Correct |
501 ms |
94764 KB |
Output is correct |
64 |
Correct |
479 ms |
94968 KB |
Output is correct |
65 |
Correct |
484 ms |
94712 KB |
Output is correct |
66 |
Correct |
302 ms |
58872 KB |
Output is correct |
67 |
Correct |
339 ms |
59388 KB |
Output is correct |
68 |
Correct |
330 ms |
58872 KB |
Output is correct |
69 |
Correct |
488 ms |
60280 KB |
Output is correct |
70 |
Correct |
304 ms |
58872 KB |
Output is correct |
71 |
Correct |
310 ms |
59128 KB |
Output is correct |
72 |
Correct |
384 ms |
59000 KB |
Output is correct |
73 |
Correct |
399 ms |
59128 KB |
Output is correct |
74 |
Correct |
1012 ms |
72440 KB |
Output is correct |
75 |
Correct |
653 ms |
63868 KB |
Output is correct |
76 |
Correct |
764 ms |
70528 KB |
Output is correct |
77 |
Correct |
783 ms |
70648 KB |
Output is correct |
78 |
Correct |
310 ms |
60664 KB |
Output is correct |
79 |
Correct |
530 ms |
63096 KB |
Output is correct |
80 |
Correct |
270 ms |
60792 KB |
Output is correct |
81 |
Correct |
402 ms |
62712 KB |
Output is correct |
82 |
Correct |
906 ms |
78840 KB |
Output is correct |
83 |
Correct |
902 ms |
78600 KB |
Output is correct |
84 |
Correct |
688 ms |
78584 KB |
Output is correct |
85 |
Correct |
684 ms |
78328 KB |
Output is correct |
86 |
Correct |
198 ms |
53368 KB |
Output is correct |
87 |
Correct |
238 ms |
54532 KB |
Output is correct |
88 |
Correct |
392 ms |
58872 KB |
Output is correct |
89 |
Correct |
739 ms |
69624 KB |
Output is correct |