#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld double
#define sz(x) (int)x.size()
#define all(x) x.begin(),x.end()
#define pb emplace_back
#define X first
#define Y second
const int N = 1e5 + 5;
typedef pair<int,int> ii;
int p[N];
int s[N];
ll ans = 0;
queue<ii> pending;
int lead(int x) {
return p[x] == x ? x : p[x] = lead(p[x]);
}
struct Edge {
int pa, a;
int pb;
Edge(int _a = 0,int _b = 0) : pa(lead(_a)), a(_a), pb(lead(_b)) {}
};
bool operator < (const Edge&x,const Edge&y) {
auto tmp1 = make_tuple(x.pa,x.pb,x.a);
auto tmp2 = make_tuple(y.pa,y.pb,y.a);
return tmp1 < tmp2;
}
set<Edge> fr[N];
set<Edge> to[N];
unordered_map<int,int> cnt[N];
void addEdge(int x,int y) { //an egde directed from x to y
int a = lead(x);
int b = lead(y);
if (a == b) return;
Edge E(y,x); E.a = 0;
auto it = fr[b].lower_bound(E);
if (it == fr[b].end() || (*it).pb != a) {
E = Edge(x,y);
if(!fr[a].count(E)) {
fr[a].insert(E);
to[b].insert(E); ans += s[b];
}
return;
}
pending.push(ii(a,b));
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n; cin >> n;
int m; cin >> m;
iota(p + 1,p + 1 + n,1);
fill(s + 1,s + 1 + n,1);
for(int i = 0 ; i < m ; ++i) {
int x; cin >> x;
int y; cin >> y; addEdge(x,y);
while (pending.size()) {
int x = lead(pending.front().X);
int y = lead(pending.front().Y);
pending.pop();
if (x == y) continue;
Edge E(x,y); E.a = 0;
Edge F(y,x); F.a = 0;
vector<Edge> remA;
vector<Edge> remB;
for(auto it = fr[x].lower_bound(E) ; it != fr[x].end() && (*it).pb == y ; remA.pb(*it++));
for(auto it = fr[y].lower_bound(F) ; it != fr[y].end() && (*it).pb == x ; remB.pb(*it++));
for(Edge E : remA) fr[x].erase(E), to[y].erase(E);
for(Edge E : remB) fr[y].erase(E), to[x].erase(E);
ans -= 1ll * sz(remA) * s[y];
ans -= 1ll * sz(remB) * s[x];
if (sz(fr[x]) + sz(to[x]) < sz(fr[y]) + sz(to[y]))
swap(x,y);
ans += 2ll * s[x] * s[y];
ans += 1ll * s[y] * sz(to[x]);
ans -= 1ll * s[y] * sz(to[y]);
p[y] = x;
s[x] += s[y];
for(Edge E : to[y]) fr[E.pa].erase(E), addEdge(E.a,x);
for(Edge E : fr[y]) to[E.pb].erase(E), addEdge(E.a,E.pb),
ans -= s[E.pb];
fr[y].clear();
to[y].clear();
}
// ll ans = 0;
//
// for(int j = 1 ; j <= n ; ++j)
// if (j == p[j])
// ans += 1ll * s[j] * (s[j] - 1),
// ans += 1ll * s[j] * sz(to[j]);
cout << ans << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
15232 KB |
Output is correct |
2 |
Correct |
18 ms |
15232 KB |
Output is correct |
3 |
Correct |
13 ms |
15232 KB |
Output is correct |
4 |
Correct |
13 ms |
15232 KB |
Output is correct |
5 |
Correct |
15 ms |
15232 KB |
Output is correct |
6 |
Correct |
14 ms |
15232 KB |
Output is correct |
7 |
Correct |
14 ms |
15232 KB |
Output is correct |
8 |
Correct |
14 ms |
15232 KB |
Output is correct |
9 |
Correct |
14 ms |
15232 KB |
Output is correct |
10 |
Correct |
13 ms |
15232 KB |
Output is correct |
11 |
Correct |
13 ms |
15232 KB |
Output is correct |
12 |
Correct |
13 ms |
15232 KB |
Output is correct |
13 |
Correct |
13 ms |
15232 KB |
Output is correct |
14 |
Correct |
13 ms |
15232 KB |
Output is correct |
15 |
Correct |
13 ms |
15232 KB |
Output is correct |
16 |
Correct |
13 ms |
15232 KB |
Output is correct |
17 |
Correct |
14 ms |
15232 KB |
Output is correct |
18 |
Correct |
13 ms |
15232 KB |
Output is correct |
19 |
Correct |
13 ms |
15232 KB |
Output is correct |
20 |
Correct |
13 ms |
15232 KB |
Output is correct |
21 |
Correct |
16 ms |
15232 KB |
Output is correct |
22 |
Correct |
14 ms |
15232 KB |
Output is correct |
23 |
Correct |
14 ms |
15232 KB |
Output is correct |
24 |
Correct |
13 ms |
15232 KB |
Output is correct |
25 |
Correct |
14 ms |
15232 KB |
Output is correct |
26 |
Correct |
14 ms |
15232 KB |
Output is correct |
27 |
Correct |
13 ms |
15232 KB |
Output is correct |
28 |
Correct |
13 ms |
15232 KB |
Output is correct |
29 |
Correct |
13 ms |
15232 KB |
Output is correct |
30 |
Correct |
13 ms |
15232 KB |
Output is correct |
31 |
Correct |
14 ms |
15232 KB |
Output is correct |
32 |
Correct |
14 ms |
15232 KB |
Output is correct |
33 |
Correct |
15 ms |
15232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
15232 KB |
Output is correct |
2 |
Correct |
18 ms |
15232 KB |
Output is correct |
3 |
Correct |
13 ms |
15232 KB |
Output is correct |
4 |
Correct |
13 ms |
15232 KB |
Output is correct |
5 |
Correct |
15 ms |
15232 KB |
Output is correct |
6 |
Correct |
14 ms |
15232 KB |
Output is correct |
7 |
Correct |
14 ms |
15232 KB |
Output is correct |
8 |
Correct |
14 ms |
15232 KB |
Output is correct |
9 |
Correct |
14 ms |
15232 KB |
Output is correct |
10 |
Correct |
13 ms |
15232 KB |
Output is correct |
11 |
Correct |
13 ms |
15232 KB |
Output is correct |
12 |
Correct |
13 ms |
15232 KB |
Output is correct |
13 |
Correct |
13 ms |
15232 KB |
Output is correct |
14 |
Correct |
13 ms |
15232 KB |
Output is correct |
15 |
Correct |
13 ms |
15232 KB |
Output is correct |
16 |
Correct |
13 ms |
15232 KB |
Output is correct |
17 |
Correct |
14 ms |
15232 KB |
Output is correct |
18 |
Correct |
13 ms |
15232 KB |
Output is correct |
19 |
Correct |
13 ms |
15232 KB |
Output is correct |
20 |
Correct |
13 ms |
15232 KB |
Output is correct |
21 |
Correct |
16 ms |
15232 KB |
Output is correct |
22 |
Correct |
14 ms |
15232 KB |
Output is correct |
23 |
Correct |
14 ms |
15232 KB |
Output is correct |
24 |
Correct |
13 ms |
15232 KB |
Output is correct |
25 |
Correct |
14 ms |
15232 KB |
Output is correct |
26 |
Correct |
14 ms |
15232 KB |
Output is correct |
27 |
Correct |
13 ms |
15232 KB |
Output is correct |
28 |
Correct |
13 ms |
15232 KB |
Output is correct |
29 |
Correct |
13 ms |
15232 KB |
Output is correct |
30 |
Correct |
13 ms |
15232 KB |
Output is correct |
31 |
Correct |
14 ms |
15232 KB |
Output is correct |
32 |
Correct |
14 ms |
15232 KB |
Output is correct |
33 |
Correct |
15 ms |
15232 KB |
Output is correct |
34 |
Correct |
16 ms |
15360 KB |
Output is correct |
35 |
Correct |
101 ms |
18552 KB |
Output is correct |
36 |
Correct |
125 ms |
20532 KB |
Output is correct |
37 |
Correct |
134 ms |
20668 KB |
Output is correct |
38 |
Correct |
124 ms |
20344 KB |
Output is correct |
39 |
Correct |
15 ms |
15232 KB |
Output is correct |
40 |
Correct |
16 ms |
15360 KB |
Output is correct |
41 |
Correct |
18 ms |
15360 KB |
Output is correct |
42 |
Correct |
15 ms |
15232 KB |
Output is correct |
43 |
Correct |
16 ms |
15360 KB |
Output is correct |
44 |
Correct |
16 ms |
15360 KB |
Output is correct |
45 |
Correct |
15 ms |
15232 KB |
Output is correct |
46 |
Correct |
20 ms |
15232 KB |
Output is correct |
47 |
Correct |
16 ms |
15360 KB |
Output is correct |
48 |
Correct |
16 ms |
15360 KB |
Output is correct |
49 |
Correct |
22 ms |
15872 KB |
Output is correct |
50 |
Correct |
126 ms |
20856 KB |
Output is correct |
51 |
Correct |
21 ms |
15616 KB |
Output is correct |
52 |
Correct |
111 ms |
19320 KB |
Output is correct |
53 |
Correct |
23 ms |
15872 KB |
Output is correct |
54 |
Correct |
117 ms |
19964 KB |
Output is correct |
55 |
Correct |
18 ms |
15744 KB |
Output is correct |
56 |
Correct |
18 ms |
15744 KB |
Output is correct |
57 |
Correct |
18 ms |
15744 KB |
Output is correct |
58 |
Correct |
18 ms |
15744 KB |
Output is correct |
59 |
Correct |
15 ms |
15232 KB |
Output is correct |
60 |
Correct |
96 ms |
17784 KB |
Output is correct |
61 |
Correct |
17 ms |
15360 KB |
Output is correct |
62 |
Correct |
120 ms |
20216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
15232 KB |
Output is correct |
2 |
Correct |
18 ms |
15232 KB |
Output is correct |
3 |
Correct |
13 ms |
15232 KB |
Output is correct |
4 |
Correct |
13 ms |
15232 KB |
Output is correct |
5 |
Correct |
15 ms |
15232 KB |
Output is correct |
6 |
Correct |
14 ms |
15232 KB |
Output is correct |
7 |
Correct |
14 ms |
15232 KB |
Output is correct |
8 |
Correct |
14 ms |
15232 KB |
Output is correct |
9 |
Correct |
14 ms |
15232 KB |
Output is correct |
10 |
Correct |
13 ms |
15232 KB |
Output is correct |
11 |
Correct |
13 ms |
15232 KB |
Output is correct |
12 |
Correct |
13 ms |
15232 KB |
Output is correct |
13 |
Correct |
13 ms |
15232 KB |
Output is correct |
14 |
Correct |
13 ms |
15232 KB |
Output is correct |
15 |
Correct |
13 ms |
15232 KB |
Output is correct |
16 |
Correct |
13 ms |
15232 KB |
Output is correct |
17 |
Correct |
14 ms |
15232 KB |
Output is correct |
18 |
Correct |
13 ms |
15232 KB |
Output is correct |
19 |
Correct |
13 ms |
15232 KB |
Output is correct |
20 |
Correct |
13 ms |
15232 KB |
Output is correct |
21 |
Correct |
16 ms |
15232 KB |
Output is correct |
22 |
Correct |
14 ms |
15232 KB |
Output is correct |
23 |
Correct |
14 ms |
15232 KB |
Output is correct |
24 |
Correct |
13 ms |
15232 KB |
Output is correct |
25 |
Correct |
14 ms |
15232 KB |
Output is correct |
26 |
Correct |
14 ms |
15232 KB |
Output is correct |
27 |
Correct |
13 ms |
15232 KB |
Output is correct |
28 |
Correct |
13 ms |
15232 KB |
Output is correct |
29 |
Correct |
13 ms |
15232 KB |
Output is correct |
30 |
Correct |
13 ms |
15232 KB |
Output is correct |
31 |
Correct |
14 ms |
15232 KB |
Output is correct |
32 |
Correct |
14 ms |
15232 KB |
Output is correct |
33 |
Correct |
15 ms |
15232 KB |
Output is correct |
34 |
Correct |
16 ms |
15360 KB |
Output is correct |
35 |
Correct |
101 ms |
18552 KB |
Output is correct |
36 |
Correct |
125 ms |
20532 KB |
Output is correct |
37 |
Correct |
134 ms |
20668 KB |
Output is correct |
38 |
Correct |
124 ms |
20344 KB |
Output is correct |
39 |
Correct |
15 ms |
15232 KB |
Output is correct |
40 |
Correct |
16 ms |
15360 KB |
Output is correct |
41 |
Correct |
18 ms |
15360 KB |
Output is correct |
42 |
Correct |
15 ms |
15232 KB |
Output is correct |
43 |
Correct |
16 ms |
15360 KB |
Output is correct |
44 |
Correct |
16 ms |
15360 KB |
Output is correct |
45 |
Correct |
15 ms |
15232 KB |
Output is correct |
46 |
Correct |
20 ms |
15232 KB |
Output is correct |
47 |
Correct |
16 ms |
15360 KB |
Output is correct |
48 |
Correct |
16 ms |
15360 KB |
Output is correct |
49 |
Correct |
22 ms |
15872 KB |
Output is correct |
50 |
Correct |
126 ms |
20856 KB |
Output is correct |
51 |
Correct |
21 ms |
15616 KB |
Output is correct |
52 |
Correct |
111 ms |
19320 KB |
Output is correct |
53 |
Correct |
23 ms |
15872 KB |
Output is correct |
54 |
Correct |
117 ms |
19964 KB |
Output is correct |
55 |
Correct |
18 ms |
15744 KB |
Output is correct |
56 |
Correct |
18 ms |
15744 KB |
Output is correct |
57 |
Correct |
18 ms |
15744 KB |
Output is correct |
58 |
Correct |
18 ms |
15744 KB |
Output is correct |
59 |
Correct |
15 ms |
15232 KB |
Output is correct |
60 |
Correct |
96 ms |
17784 KB |
Output is correct |
61 |
Correct |
17 ms |
15360 KB |
Output is correct |
62 |
Correct |
120 ms |
20216 KB |
Output is correct |
63 |
Correct |
443 ms |
55928 KB |
Output is correct |
64 |
Correct |
434 ms |
55672 KB |
Output is correct |
65 |
Correct |
427 ms |
55708 KB |
Output is correct |
66 |
Correct |
130 ms |
18296 KB |
Output is correct |
67 |
Correct |
231 ms |
23928 KB |
Output is correct |
68 |
Correct |
125 ms |
18296 KB |
Output is correct |
69 |
Correct |
324 ms |
24568 KB |
Output is correct |
70 |
Correct |
140 ms |
18300 KB |
Output is correct |
71 |
Correct |
141 ms |
18172 KB |
Output is correct |
72 |
Correct |
269 ms |
24056 KB |
Output is correct |
73 |
Correct |
259 ms |
23800 KB |
Output is correct |
74 |
Correct |
816 ms |
32820 KB |
Output is correct |
75 |
Correct |
484 ms |
29040 KB |
Output is correct |
76 |
Correct |
618 ms |
32632 KB |
Output is correct |
77 |
Correct |
582 ms |
32760 KB |
Output is correct |
78 |
Correct |
198 ms |
24952 KB |
Output is correct |
79 |
Correct |
384 ms |
29432 KB |
Output is correct |
80 |
Correct |
179 ms |
25464 KB |
Output is correct |
81 |
Correct |
284 ms |
28772 KB |
Output is correct |
82 |
Correct |
629 ms |
44664 KB |
Output is correct |
83 |
Correct |
652 ms |
44920 KB |
Output is correct |
84 |
Correct |
496 ms |
44664 KB |
Output is correct |
85 |
Correct |
489 ms |
44664 KB |
Output is correct |
86 |
Correct |
142 ms |
19576 KB |
Output is correct |
87 |
Correct |
178 ms |
21880 KB |
Output is correct |
88 |
Correct |
278 ms |
26132 KB |
Output is correct |
89 |
Correct |
555 ms |
35704 KB |
Output is correct |