// Zende bad Shoma nasime faghat !
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'
using namespace std;
typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;
const ll Mod = 1000000007LL;
const int N = 1e5 + 10;
const ll Inf = 2242545357980376863LL;
const ll Log = 30;
ll ans;
int n, m;
set<int> I[N], O[N];
int par[N], sz[N];
set<int> st[N];
vector<pll> U;
ll Calc(int com){
ll res = 0;
res = 1ll * sz[com] * st[com].size();
res -= sz[com];
return res;
}
int Find(int u){
if(par[u] == u) return u;
return par[u] = Find(par[u]);
}
void AddE(int u, int v){
v = Find(v);
ans -= Calc(v);
st[v].insert(u);
ans += Calc(v);
u = Find(u); v = Find(v);
O[u].insert(v);
I[v].insert(u);
if(I[u].count(v)){
U.pb({u, v});
}
}
void Unite(int u, int v){
u = Find(u); v = Find(v);
if(u == v) return ;
if(sz[u] > sz[v]) swap(u, v);
ans -= Calc(u); ans -= Calc(v);
for(auto x : st[u]){
//AddE(x, v);
st[v].insert(x);
}
for(auto x : I[u]){
if(O[v].count(x)){
U.pb({v, x});
}
I[v].insert(x);
O[x].erase(u);
O[x].insert(v);
}
for(auto x : O[u]){
if(I[v].count(x)){
U.pb({v, x});
}
O[v].insert(x);
I[x].erase(u);
I[x].insert(v);
}
st[u].clear(); I[u].clear(); O[u].clear();
//st[u].shrink_to_fit();
par[u] = v;
sz[v] += sz[u];
ans += Calc(v);
}
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
iota(par, par + N, 0);
fill(sz, sz + N, 1);
cin >> n >> m;
for(int i = 1; i <= n; i++) st[i].insert(i);
int u, v;
for(int i = 0; i < m; i++){
cin >> u >> v;
AddE(u, v);
int f1, f2;
while(!U.empty()){
f1 = U.back().F; f2 = U.back().S;
U.pop_back();
Unite(f1, f2);
//cerr << "# " << f1 << ' ' << f2 << '\n';
}
cout << ans << '\n';
}
return 0;
}
/*
4 6
1 2
2 1
3 4
4 3
1 3
4 2
8 14
1 2
2 1
3 4
4 5
5 4
4 3
6 7
7 8
8 7
7 6
1 3
4 7
6 1
8 5
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
15232 KB |
Output is correct |
2 |
Correct |
10 ms |
15360 KB |
Output is correct |
3 |
Correct |
9 ms |
15232 KB |
Output is correct |
4 |
Correct |
9 ms |
15232 KB |
Output is correct |
5 |
Correct |
9 ms |
15232 KB |
Output is correct |
6 |
Correct |
10 ms |
15232 KB |
Output is correct |
7 |
Correct |
10 ms |
15232 KB |
Output is correct |
8 |
Correct |
10 ms |
15232 KB |
Output is correct |
9 |
Correct |
10 ms |
15232 KB |
Output is correct |
10 |
Correct |
9 ms |
15232 KB |
Output is correct |
11 |
Correct |
9 ms |
15232 KB |
Output is correct |
12 |
Correct |
9 ms |
15232 KB |
Output is correct |
13 |
Correct |
10 ms |
15232 KB |
Output is correct |
14 |
Correct |
9 ms |
15232 KB |
Output is correct |
15 |
Correct |
10 ms |
15232 KB |
Output is correct |
16 |
Correct |
9 ms |
15232 KB |
Output is correct |
17 |
Correct |
9 ms |
15232 KB |
Output is correct |
18 |
Correct |
9 ms |
15276 KB |
Output is correct |
19 |
Correct |
10 ms |
15232 KB |
Output is correct |
20 |
Correct |
9 ms |
15232 KB |
Output is correct |
21 |
Correct |
10 ms |
15232 KB |
Output is correct |
22 |
Correct |
9 ms |
15156 KB |
Output is correct |
23 |
Correct |
10 ms |
15232 KB |
Output is correct |
24 |
Correct |
10 ms |
15232 KB |
Output is correct |
25 |
Correct |
11 ms |
15360 KB |
Output is correct |
26 |
Correct |
9 ms |
15232 KB |
Output is correct |
27 |
Correct |
9 ms |
15232 KB |
Output is correct |
28 |
Correct |
9 ms |
15232 KB |
Output is correct |
29 |
Correct |
9 ms |
15232 KB |
Output is correct |
30 |
Correct |
9 ms |
15232 KB |
Output is correct |
31 |
Correct |
10 ms |
15208 KB |
Output is correct |
32 |
Correct |
9 ms |
15232 KB |
Output is correct |
33 |
Correct |
10 ms |
15232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
15232 KB |
Output is correct |
2 |
Correct |
10 ms |
15360 KB |
Output is correct |
3 |
Correct |
9 ms |
15232 KB |
Output is correct |
4 |
Correct |
9 ms |
15232 KB |
Output is correct |
5 |
Correct |
9 ms |
15232 KB |
Output is correct |
6 |
Correct |
10 ms |
15232 KB |
Output is correct |
7 |
Correct |
10 ms |
15232 KB |
Output is correct |
8 |
Correct |
10 ms |
15232 KB |
Output is correct |
9 |
Correct |
10 ms |
15232 KB |
Output is correct |
10 |
Correct |
9 ms |
15232 KB |
Output is correct |
11 |
Correct |
9 ms |
15232 KB |
Output is correct |
12 |
Correct |
9 ms |
15232 KB |
Output is correct |
13 |
Correct |
10 ms |
15232 KB |
Output is correct |
14 |
Correct |
9 ms |
15232 KB |
Output is correct |
15 |
Correct |
10 ms |
15232 KB |
Output is correct |
16 |
Correct |
9 ms |
15232 KB |
Output is correct |
17 |
Correct |
9 ms |
15232 KB |
Output is correct |
18 |
Correct |
9 ms |
15276 KB |
Output is correct |
19 |
Correct |
10 ms |
15232 KB |
Output is correct |
20 |
Correct |
9 ms |
15232 KB |
Output is correct |
21 |
Correct |
10 ms |
15232 KB |
Output is correct |
22 |
Correct |
9 ms |
15156 KB |
Output is correct |
23 |
Correct |
10 ms |
15232 KB |
Output is correct |
24 |
Correct |
10 ms |
15232 KB |
Output is correct |
25 |
Correct |
11 ms |
15360 KB |
Output is correct |
26 |
Correct |
9 ms |
15232 KB |
Output is correct |
27 |
Correct |
9 ms |
15232 KB |
Output is correct |
28 |
Correct |
9 ms |
15232 KB |
Output is correct |
29 |
Correct |
9 ms |
15232 KB |
Output is correct |
30 |
Correct |
9 ms |
15232 KB |
Output is correct |
31 |
Correct |
10 ms |
15208 KB |
Output is correct |
32 |
Correct |
9 ms |
15232 KB |
Output is correct |
33 |
Correct |
10 ms |
15232 KB |
Output is correct |
34 |
Correct |
14 ms |
15360 KB |
Output is correct |
35 |
Correct |
150 ms |
21380 KB |
Output is correct |
36 |
Correct |
183 ms |
24048 KB |
Output is correct |
37 |
Correct |
184 ms |
24176 KB |
Output is correct |
38 |
Correct |
184 ms |
23796 KB |
Output is correct |
39 |
Correct |
12 ms |
15360 KB |
Output is correct |
40 |
Correct |
14 ms |
15648 KB |
Output is correct |
41 |
Correct |
15 ms |
15616 KB |
Output is correct |
42 |
Correct |
13 ms |
15360 KB |
Output is correct |
43 |
Correct |
14 ms |
15616 KB |
Output is correct |
44 |
Correct |
14 ms |
15488 KB |
Output is correct |
45 |
Correct |
12 ms |
15360 KB |
Output is correct |
46 |
Correct |
13 ms |
15360 KB |
Output is correct |
47 |
Correct |
15 ms |
15488 KB |
Output is correct |
48 |
Correct |
15 ms |
15616 KB |
Output is correct |
49 |
Correct |
25 ms |
16256 KB |
Output is correct |
50 |
Correct |
197 ms |
24180 KB |
Output is correct |
51 |
Correct |
19 ms |
15872 KB |
Output is correct |
52 |
Correct |
175 ms |
22392 KB |
Output is correct |
53 |
Correct |
23 ms |
16256 KB |
Output is correct |
54 |
Correct |
177 ms |
23284 KB |
Output is correct |
55 |
Correct |
18 ms |
16000 KB |
Output is correct |
56 |
Correct |
18 ms |
16000 KB |
Output is correct |
57 |
Correct |
17 ms |
16000 KB |
Output is correct |
58 |
Correct |
18 ms |
16000 KB |
Output is correct |
59 |
Correct |
16 ms |
15488 KB |
Output is correct |
60 |
Correct |
148 ms |
20472 KB |
Output is correct |
61 |
Correct |
17 ms |
15616 KB |
Output is correct |
62 |
Correct |
184 ms |
23544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
15232 KB |
Output is correct |
2 |
Correct |
10 ms |
15360 KB |
Output is correct |
3 |
Correct |
9 ms |
15232 KB |
Output is correct |
4 |
Correct |
9 ms |
15232 KB |
Output is correct |
5 |
Correct |
9 ms |
15232 KB |
Output is correct |
6 |
Correct |
10 ms |
15232 KB |
Output is correct |
7 |
Correct |
10 ms |
15232 KB |
Output is correct |
8 |
Correct |
10 ms |
15232 KB |
Output is correct |
9 |
Correct |
10 ms |
15232 KB |
Output is correct |
10 |
Correct |
9 ms |
15232 KB |
Output is correct |
11 |
Correct |
9 ms |
15232 KB |
Output is correct |
12 |
Correct |
9 ms |
15232 KB |
Output is correct |
13 |
Correct |
10 ms |
15232 KB |
Output is correct |
14 |
Correct |
9 ms |
15232 KB |
Output is correct |
15 |
Correct |
10 ms |
15232 KB |
Output is correct |
16 |
Correct |
9 ms |
15232 KB |
Output is correct |
17 |
Correct |
9 ms |
15232 KB |
Output is correct |
18 |
Correct |
9 ms |
15276 KB |
Output is correct |
19 |
Correct |
10 ms |
15232 KB |
Output is correct |
20 |
Correct |
9 ms |
15232 KB |
Output is correct |
21 |
Correct |
10 ms |
15232 KB |
Output is correct |
22 |
Correct |
9 ms |
15156 KB |
Output is correct |
23 |
Correct |
10 ms |
15232 KB |
Output is correct |
24 |
Correct |
10 ms |
15232 KB |
Output is correct |
25 |
Correct |
11 ms |
15360 KB |
Output is correct |
26 |
Correct |
9 ms |
15232 KB |
Output is correct |
27 |
Correct |
9 ms |
15232 KB |
Output is correct |
28 |
Correct |
9 ms |
15232 KB |
Output is correct |
29 |
Correct |
9 ms |
15232 KB |
Output is correct |
30 |
Correct |
9 ms |
15232 KB |
Output is correct |
31 |
Correct |
10 ms |
15208 KB |
Output is correct |
32 |
Correct |
9 ms |
15232 KB |
Output is correct |
33 |
Correct |
10 ms |
15232 KB |
Output is correct |
34 |
Correct |
14 ms |
15360 KB |
Output is correct |
35 |
Correct |
150 ms |
21380 KB |
Output is correct |
36 |
Correct |
183 ms |
24048 KB |
Output is correct |
37 |
Correct |
184 ms |
24176 KB |
Output is correct |
38 |
Correct |
184 ms |
23796 KB |
Output is correct |
39 |
Correct |
12 ms |
15360 KB |
Output is correct |
40 |
Correct |
14 ms |
15648 KB |
Output is correct |
41 |
Correct |
15 ms |
15616 KB |
Output is correct |
42 |
Correct |
13 ms |
15360 KB |
Output is correct |
43 |
Correct |
14 ms |
15616 KB |
Output is correct |
44 |
Correct |
14 ms |
15488 KB |
Output is correct |
45 |
Correct |
12 ms |
15360 KB |
Output is correct |
46 |
Correct |
13 ms |
15360 KB |
Output is correct |
47 |
Correct |
15 ms |
15488 KB |
Output is correct |
48 |
Correct |
15 ms |
15616 KB |
Output is correct |
49 |
Correct |
25 ms |
16256 KB |
Output is correct |
50 |
Correct |
197 ms |
24180 KB |
Output is correct |
51 |
Correct |
19 ms |
15872 KB |
Output is correct |
52 |
Correct |
175 ms |
22392 KB |
Output is correct |
53 |
Correct |
23 ms |
16256 KB |
Output is correct |
54 |
Correct |
177 ms |
23284 KB |
Output is correct |
55 |
Correct |
18 ms |
16000 KB |
Output is correct |
56 |
Correct |
18 ms |
16000 KB |
Output is correct |
57 |
Correct |
17 ms |
16000 KB |
Output is correct |
58 |
Correct |
18 ms |
16000 KB |
Output is correct |
59 |
Correct |
16 ms |
15488 KB |
Output is correct |
60 |
Correct |
148 ms |
20472 KB |
Output is correct |
61 |
Correct |
17 ms |
15616 KB |
Output is correct |
62 |
Correct |
184 ms |
23544 KB |
Output is correct |
63 |
Correct |
694 ms |
67576 KB |
Output is correct |
64 |
Correct |
757 ms |
67704 KB |
Output is correct |
65 |
Correct |
739 ms |
67708 KB |
Output is correct |
66 |
Correct |
275 ms |
24312 KB |
Output is correct |
67 |
Correct |
656 ms |
32760 KB |
Output is correct |
68 |
Correct |
284 ms |
24440 KB |
Output is correct |
69 |
Correct |
676 ms |
31608 KB |
Output is correct |
70 |
Correct |
308 ms |
24396 KB |
Output is correct |
71 |
Correct |
301 ms |
24312 KB |
Output is correct |
72 |
Correct |
732 ms |
32480 KB |
Output is correct |
73 |
Correct |
733 ms |
32344 KB |
Output is correct |
74 |
Correct |
1430 ms |
45660 KB |
Output is correct |
75 |
Correct |
939 ms |
37240 KB |
Output is correct |
76 |
Correct |
1115 ms |
43508 KB |
Output is correct |
77 |
Correct |
1069 ms |
43516 KB |
Output is correct |
78 |
Correct |
297 ms |
32504 KB |
Output is correct |
79 |
Correct |
587 ms |
35704 KB |
Output is correct |
80 |
Correct |
325 ms |
34808 KB |
Output is correct |
81 |
Correct |
604 ms |
38136 KB |
Output is correct |
82 |
Correct |
850 ms |
51704 KB |
Output is correct |
83 |
Correct |
904 ms |
51704 KB |
Output is correct |
84 |
Correct |
985 ms |
54008 KB |
Output is correct |
85 |
Correct |
1097 ms |
53868 KB |
Output is correct |
86 |
Correct |
419 ms |
28280 KB |
Output is correct |
87 |
Correct |
542 ms |
30712 KB |
Output is correct |
88 |
Correct |
659 ms |
32276 KB |
Output is correct |
89 |
Correct |
1022 ms |
42900 KB |
Output is correct |