#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast,unroll-loops")
#define ll long long
#define int ll
#define ull unsigned ll
#define ld long double
#define rep(a) rep1(i,a)
#define rep1(i,a) rep2(i,0,a)
#define rep2(i,b,a) for(int i=(b); i<((int)(a)); i++)
#define rep3(i,b,a) for(int i=(b); i>=((int)(a)); i--)
#define chkmin(a,b) (a=min(a,b))
#define chkmax(a,b) (a=max(a,b))
#define all(a) a.begin(),a.end()
#define pii pair<int,int>
#define pb push_back
//#define inf 1010000000
#define inf 4000000000000000000
#define eps 1e-9
#define sz(a) ((int)a.size())
#define pow2(x) (1ll<<(x))
#define ceiling(a,b) (((a)+(b)-1)/(b))
#define print0(a) cout << (a) << ' '
#define ykh mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#ifdef i_am_noob
#define bug(...) cerr << "#" << __LINE__ << ' ' << #__VA_ARGS__ << "- ", _do(__VA_ARGS__)
template<typename T> void _do(T && x) {cerr << x << endl;}
template<typename T, typename ...S> void _do(T && x, S&&...y) {cerr << x << ", "; _do(y...);}
#else
#define bug(...) 777771449
#endif
const int mod=1000000007;
const int maxn=100005,maxm=26,maxk=7777714;
//i_am_noob
int n,m,res=0;
int par[maxn];
vector<vector<int>> has(maxn);
unordered_set<int> from[maxn],to[maxn],from2[maxn];
int Find(int x){return x==par[x]?x:par[x]=Find(par[x]);}
void orzck(){
cin >> n >> m;
rep(n) par[i]=i,has[i].pb(i);
rep1(_,m){
int u,v;
cin >> u >> v;
u--,v--;
if(Find(u)==Find(v)){
cout << res << "\n";
continue;
}
res-=sz(has[Find(v)])*sz(from2[Find(v)]);
from[Find(v)].insert(Find(u));
to[Find(u)].insert(Find(v));
from2[Find(v)].insert(u);
res+=sz(has[Find(v)])*sz(from2[Find(v)]);
u=Find(u),v=Find(v);
if((!to[u].count(v))||(!to[v].count(u))){
cout << res << "\n";
continue;
}
queue<int> q;
while(1){
res-=sz(has[u])*sz(from2[u]);
res-=sz(has[v])*sz(from2[v]);
res-=sz(has[u])*(sz(has[u])-1);
res-=sz(has[v])*(sz(has[v])-1);
if(sz(has[u])<sz(has[v])) swap(u,v);
for(auto i: has[v]) from2[u].erase(i);
vector<int> tmp;
for(auto i: from2[v]) if(Find(i)==u) tmp.pb(i);
for(auto i: tmp) from2[v].erase(i);
from[u].erase(v),to[u].erase(v);
from[v].erase(u),to[v].erase(u);
for(auto i: from[v]) to[i].erase(v),to[i].insert(u);
for(auto i: to[v]) from[i].erase(v),from[i].insert(u);
for(auto i: from[v]){
if(to[u].count(i)) q.push(i);
else from[u].insert(i);
}
for(auto i: to[v]){
if(from[u].count(i)) q.push(i);
else to[u].insert(i);
}
for(auto i: has[v]) has[u].pb(i);
for(auto i: from2[v]) from2[u].insert(i);
par[v]=u;
res+=sz(has[u])*sz(from2[u]);
res+=sz(has[u])*(sz(has[u])-1);
v=-1;
while(1){
if(q.empty()) break;
v=q.front();
q.pop();
if(v==par[v]) break;
else v=-1;
}
if(v==-1) break;
}
cout << res << "\n";
}
}
signed main(){
ios_base::sync_with_stdio(0),cin.tie(0);
orzck();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
19028 KB |
Output is correct |
2 |
Correct |
11 ms |
19036 KB |
Output is correct |
3 |
Correct |
12 ms |
19124 KB |
Output is correct |
4 |
Correct |
11 ms |
19028 KB |
Output is correct |
5 |
Correct |
11 ms |
19028 KB |
Output is correct |
6 |
Correct |
11 ms |
19128 KB |
Output is correct |
7 |
Correct |
14 ms |
19156 KB |
Output is correct |
8 |
Correct |
14 ms |
19148 KB |
Output is correct |
9 |
Correct |
13 ms |
19156 KB |
Output is correct |
10 |
Correct |
12 ms |
19028 KB |
Output is correct |
11 |
Correct |
12 ms |
19028 KB |
Output is correct |
12 |
Correct |
12 ms |
19028 KB |
Output is correct |
13 |
Correct |
12 ms |
19028 KB |
Output is correct |
14 |
Correct |
13 ms |
19152 KB |
Output is correct |
15 |
Correct |
11 ms |
19084 KB |
Output is correct |
16 |
Correct |
11 ms |
19132 KB |
Output is correct |
17 |
Correct |
11 ms |
19128 KB |
Output is correct |
18 |
Correct |
11 ms |
19032 KB |
Output is correct |
19 |
Correct |
14 ms |
19036 KB |
Output is correct |
20 |
Correct |
11 ms |
19156 KB |
Output is correct |
21 |
Correct |
13 ms |
19132 KB |
Output is correct |
22 |
Correct |
13 ms |
19128 KB |
Output is correct |
23 |
Correct |
12 ms |
19144 KB |
Output is correct |
24 |
Correct |
12 ms |
19140 KB |
Output is correct |
25 |
Correct |
12 ms |
19140 KB |
Output is correct |
26 |
Correct |
12 ms |
19140 KB |
Output is correct |
27 |
Correct |
11 ms |
19156 KB |
Output is correct |
28 |
Correct |
12 ms |
19156 KB |
Output is correct |
29 |
Correct |
12 ms |
19072 KB |
Output is correct |
30 |
Correct |
11 ms |
19052 KB |
Output is correct |
31 |
Correct |
12 ms |
19048 KB |
Output is correct |
32 |
Correct |
11 ms |
19120 KB |
Output is correct |
33 |
Correct |
12 ms |
19156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
19028 KB |
Output is correct |
2 |
Correct |
11 ms |
19036 KB |
Output is correct |
3 |
Correct |
12 ms |
19124 KB |
Output is correct |
4 |
Correct |
11 ms |
19028 KB |
Output is correct |
5 |
Correct |
11 ms |
19028 KB |
Output is correct |
6 |
Correct |
11 ms |
19128 KB |
Output is correct |
7 |
Correct |
14 ms |
19156 KB |
Output is correct |
8 |
Correct |
14 ms |
19148 KB |
Output is correct |
9 |
Correct |
13 ms |
19156 KB |
Output is correct |
10 |
Correct |
12 ms |
19028 KB |
Output is correct |
11 |
Correct |
12 ms |
19028 KB |
Output is correct |
12 |
Correct |
12 ms |
19028 KB |
Output is correct |
13 |
Correct |
12 ms |
19028 KB |
Output is correct |
14 |
Correct |
13 ms |
19152 KB |
Output is correct |
15 |
Correct |
11 ms |
19084 KB |
Output is correct |
16 |
Correct |
11 ms |
19132 KB |
Output is correct |
17 |
Correct |
11 ms |
19128 KB |
Output is correct |
18 |
Correct |
11 ms |
19032 KB |
Output is correct |
19 |
Correct |
14 ms |
19036 KB |
Output is correct |
20 |
Correct |
11 ms |
19156 KB |
Output is correct |
21 |
Correct |
13 ms |
19132 KB |
Output is correct |
22 |
Correct |
13 ms |
19128 KB |
Output is correct |
23 |
Correct |
12 ms |
19144 KB |
Output is correct |
24 |
Correct |
12 ms |
19140 KB |
Output is correct |
25 |
Correct |
12 ms |
19140 KB |
Output is correct |
26 |
Correct |
12 ms |
19140 KB |
Output is correct |
27 |
Correct |
11 ms |
19156 KB |
Output is correct |
28 |
Correct |
12 ms |
19156 KB |
Output is correct |
29 |
Correct |
12 ms |
19072 KB |
Output is correct |
30 |
Correct |
11 ms |
19052 KB |
Output is correct |
31 |
Correct |
12 ms |
19048 KB |
Output is correct |
32 |
Correct |
11 ms |
19120 KB |
Output is correct |
33 |
Correct |
12 ms |
19156 KB |
Output is correct |
34 |
Correct |
14 ms |
19284 KB |
Output is correct |
35 |
Correct |
83 ms |
24836 KB |
Output is correct |
36 |
Correct |
99 ms |
27540 KB |
Output is correct |
37 |
Correct |
97 ms |
27484 KB |
Output is correct |
38 |
Correct |
105 ms |
27216 KB |
Output is correct |
39 |
Correct |
14 ms |
19924 KB |
Output is correct |
40 |
Correct |
16 ms |
20044 KB |
Output is correct |
41 |
Correct |
18 ms |
20044 KB |
Output is correct |
42 |
Correct |
16 ms |
19912 KB |
Output is correct |
43 |
Correct |
16 ms |
20056 KB |
Output is correct |
44 |
Correct |
15 ms |
19944 KB |
Output is correct |
45 |
Correct |
15 ms |
19924 KB |
Output is correct |
46 |
Correct |
15 ms |
19888 KB |
Output is correct |
47 |
Correct |
16 ms |
20052 KB |
Output is correct |
48 |
Correct |
16 ms |
20000 KB |
Output is correct |
49 |
Correct |
24 ms |
20676 KB |
Output is correct |
50 |
Correct |
103 ms |
27728 KB |
Output is correct |
51 |
Correct |
17 ms |
20264 KB |
Output is correct |
52 |
Correct |
89 ms |
26236 KB |
Output is correct |
53 |
Correct |
21 ms |
20564 KB |
Output is correct |
54 |
Correct |
95 ms |
26828 KB |
Output is correct |
55 |
Correct |
18 ms |
20292 KB |
Output is correct |
56 |
Correct |
17 ms |
20308 KB |
Output is correct |
57 |
Correct |
15 ms |
20292 KB |
Output is correct |
58 |
Correct |
17 ms |
20436 KB |
Output is correct |
59 |
Correct |
16 ms |
20068 KB |
Output is correct |
60 |
Correct |
81 ms |
24972 KB |
Output is correct |
61 |
Correct |
16 ms |
20052 KB |
Output is correct |
62 |
Correct |
100 ms |
27084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
19028 KB |
Output is correct |
2 |
Correct |
11 ms |
19036 KB |
Output is correct |
3 |
Correct |
12 ms |
19124 KB |
Output is correct |
4 |
Correct |
11 ms |
19028 KB |
Output is correct |
5 |
Correct |
11 ms |
19028 KB |
Output is correct |
6 |
Correct |
11 ms |
19128 KB |
Output is correct |
7 |
Correct |
14 ms |
19156 KB |
Output is correct |
8 |
Correct |
14 ms |
19148 KB |
Output is correct |
9 |
Correct |
13 ms |
19156 KB |
Output is correct |
10 |
Correct |
12 ms |
19028 KB |
Output is correct |
11 |
Correct |
12 ms |
19028 KB |
Output is correct |
12 |
Correct |
12 ms |
19028 KB |
Output is correct |
13 |
Correct |
12 ms |
19028 KB |
Output is correct |
14 |
Correct |
13 ms |
19152 KB |
Output is correct |
15 |
Correct |
11 ms |
19084 KB |
Output is correct |
16 |
Correct |
11 ms |
19132 KB |
Output is correct |
17 |
Correct |
11 ms |
19128 KB |
Output is correct |
18 |
Correct |
11 ms |
19032 KB |
Output is correct |
19 |
Correct |
14 ms |
19036 KB |
Output is correct |
20 |
Correct |
11 ms |
19156 KB |
Output is correct |
21 |
Correct |
13 ms |
19132 KB |
Output is correct |
22 |
Correct |
13 ms |
19128 KB |
Output is correct |
23 |
Correct |
12 ms |
19144 KB |
Output is correct |
24 |
Correct |
12 ms |
19140 KB |
Output is correct |
25 |
Correct |
12 ms |
19140 KB |
Output is correct |
26 |
Correct |
12 ms |
19140 KB |
Output is correct |
27 |
Correct |
11 ms |
19156 KB |
Output is correct |
28 |
Correct |
12 ms |
19156 KB |
Output is correct |
29 |
Correct |
12 ms |
19072 KB |
Output is correct |
30 |
Correct |
11 ms |
19052 KB |
Output is correct |
31 |
Correct |
12 ms |
19048 KB |
Output is correct |
32 |
Correct |
11 ms |
19120 KB |
Output is correct |
33 |
Correct |
12 ms |
19156 KB |
Output is correct |
34 |
Correct |
14 ms |
19284 KB |
Output is correct |
35 |
Correct |
83 ms |
24836 KB |
Output is correct |
36 |
Correct |
99 ms |
27540 KB |
Output is correct |
37 |
Correct |
97 ms |
27484 KB |
Output is correct |
38 |
Correct |
105 ms |
27216 KB |
Output is correct |
39 |
Correct |
14 ms |
19924 KB |
Output is correct |
40 |
Correct |
16 ms |
20044 KB |
Output is correct |
41 |
Correct |
18 ms |
20044 KB |
Output is correct |
42 |
Correct |
16 ms |
19912 KB |
Output is correct |
43 |
Correct |
16 ms |
20056 KB |
Output is correct |
44 |
Correct |
15 ms |
19944 KB |
Output is correct |
45 |
Correct |
15 ms |
19924 KB |
Output is correct |
46 |
Correct |
15 ms |
19888 KB |
Output is correct |
47 |
Correct |
16 ms |
20052 KB |
Output is correct |
48 |
Correct |
16 ms |
20000 KB |
Output is correct |
49 |
Correct |
24 ms |
20676 KB |
Output is correct |
50 |
Correct |
103 ms |
27728 KB |
Output is correct |
51 |
Correct |
17 ms |
20264 KB |
Output is correct |
52 |
Correct |
89 ms |
26236 KB |
Output is correct |
53 |
Correct |
21 ms |
20564 KB |
Output is correct |
54 |
Correct |
95 ms |
26828 KB |
Output is correct |
55 |
Correct |
18 ms |
20292 KB |
Output is correct |
56 |
Correct |
17 ms |
20308 KB |
Output is correct |
57 |
Correct |
15 ms |
20292 KB |
Output is correct |
58 |
Correct |
17 ms |
20436 KB |
Output is correct |
59 |
Correct |
16 ms |
20068 KB |
Output is correct |
60 |
Correct |
81 ms |
24972 KB |
Output is correct |
61 |
Correct |
16 ms |
20052 KB |
Output is correct |
62 |
Correct |
100 ms |
27084 KB |
Output is correct |
63 |
Correct |
577 ms |
87860 KB |
Output is correct |
64 |
Correct |
578 ms |
87884 KB |
Output is correct |
65 |
Correct |
570 ms |
87904 KB |
Output is correct |
66 |
Correct |
248 ms |
61012 KB |
Output is correct |
67 |
Correct |
554 ms |
72004 KB |
Output is correct |
68 |
Correct |
254 ms |
61088 KB |
Output is correct |
69 |
Correct |
404 ms |
66656 KB |
Output is correct |
70 |
Correct |
244 ms |
61112 KB |
Output is correct |
71 |
Correct |
245 ms |
61072 KB |
Output is correct |
72 |
Correct |
575 ms |
68264 KB |
Output is correct |
73 |
Correct |
575 ms |
68552 KB |
Output is correct |
74 |
Correct |
1113 ms |
87332 KB |
Output is correct |
75 |
Correct |
550 ms |
71504 KB |
Output is correct |
76 |
Correct |
746 ms |
78816 KB |
Output is correct |
77 |
Correct |
755 ms |
78804 KB |
Output is correct |
78 |
Correct |
236 ms |
61164 KB |
Output is correct |
79 |
Correct |
360 ms |
68708 KB |
Output is correct |
80 |
Correct |
212 ms |
55864 KB |
Output is correct |
81 |
Correct |
349 ms |
67432 KB |
Output is correct |
82 |
Correct |
682 ms |
81204 KB |
Output is correct |
83 |
Correct |
713 ms |
81180 KB |
Output is correct |
84 |
Correct |
533 ms |
80252 KB |
Output is correct |
85 |
Correct |
524 ms |
80336 KB |
Output is correct |
86 |
Correct |
349 ms |
71292 KB |
Output is correct |
87 |
Correct |
371 ms |
73396 KB |
Output is correct |
88 |
Correct |
574 ms |
68424 KB |
Output is correct |
89 |
Correct |
701 ms |
77516 KB |
Output is correct |