#include <stdio.h>
#include <set>
#define MAXN 100000
#define MAXM 300000
struct Edge {
int u, v;
} edges[MAXM];
int sef1[MAXN], sef2[MAXN], sz[MAXN], stiva[MAXN], sp;
std::set<int> flw[MAXN], muchii[MAXN];
std::set<std::pair<int, int>> mp;
long long rez;
static inline long long getrez(int i) {
return 1LL * sz[i] * flw[i].size();
}
int find1(int i) {
if(i == sef1[i]) {
return i;
}
return sef1[i] = find1(sef1[i]);
}
void join1(int i, int j) {
std::set<int>::iterator it;
if((i = find1(i)) != (j = find1(j))) {
if(sz[i] > sz[j]) {
std::swap(i, j);
}
rez -= getrez(i) + getrez(j);
sz[j] += sz[i];
sef1[i] = j;
if(flw[j].size() < flw[i].size()) {
std::swap(flw[i], flw[j]);
}
it = flw[i].begin();
while(it != flw[i].end()) {
flw[j].insert(*it);
it++;
}
rez += getrez(j);
}
}
int find2(int i) {
if(i == sef2[i]) {
return i;
}
return sef2[i] = find2(sef2[i]);
}
void join2(int i, int j) {
std::set<int>::iterator it;
if((i = find2(i)) != (j = find2(j))) {
join1(i, j);
if(muchii[j].size() < muchii[i].size()) {
std::swap(i, j);
}
it = muchii[i].begin();
while(it != muchii[i].end()) {
stiva[sp++] = *it;
muchii[j].insert(*it);
it++;
}
sef2[i] = j;
}
}
int main() {
int n, m, i, u, v;
scanf("%d%d", &n, &m);
for(i = 0; i < n; i++) {
sef1[i] = sef2[i] = i;
flw[i].insert(i);
sz[i] = 1;
}
rez = n;
for(i = 0; i < m; i++) {
scanf("%d%d", &edges[i].u, &edges[i].v);
u = --edges[i].u;
v = find1(--edges[i].v);
rez -= getrez(v);
flw[v].insert(u);
rez += getrez(v);
stiva[sp++] = i;
while(sp > 0) {
sp--;
muchii[u = find2(edges[stiva[sp]].u)].insert(stiva[sp]);
muchii[v = find2(edges[stiva[sp]].v)].insert(stiva[sp]);
mp.insert(std::make_pair(u, v));
if(mp.count(std::make_pair(v, u))) {
join2(u, v);
}
}
printf("%lld\n", rez - n);
}
return 0;
}
Compilation message
joitter2.cpp: In function 'int main()':
joitter2.cpp:79:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
79 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
joitter2.cpp:88:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
88 | scanf("%d%d", &edges[i].u, &edges[i].v);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
11608 KB |
Output is correct |
2 |
Correct |
2 ms |
11608 KB |
Output is correct |
3 |
Correct |
2 ms |
11612 KB |
Output is correct |
4 |
Correct |
2 ms |
11612 KB |
Output is correct |
5 |
Correct |
3 ms |
11612 KB |
Output is correct |
6 |
Correct |
2 ms |
11612 KB |
Output is correct |
7 |
Correct |
3 ms |
11868 KB |
Output is correct |
8 |
Correct |
3 ms |
11868 KB |
Output is correct |
9 |
Correct |
4 ms |
11868 KB |
Output is correct |
10 |
Correct |
3 ms |
11612 KB |
Output is correct |
11 |
Correct |
3 ms |
11612 KB |
Output is correct |
12 |
Correct |
3 ms |
11612 KB |
Output is correct |
13 |
Correct |
2 ms |
11612 KB |
Output is correct |
14 |
Correct |
2 ms |
11612 KB |
Output is correct |
15 |
Correct |
3 ms |
11612 KB |
Output is correct |
16 |
Correct |
3 ms |
11612 KB |
Output is correct |
17 |
Correct |
3 ms |
11608 KB |
Output is correct |
18 |
Correct |
2 ms |
11612 KB |
Output is correct |
19 |
Correct |
3 ms |
11612 KB |
Output is correct |
20 |
Correct |
2 ms |
11676 KB |
Output is correct |
21 |
Correct |
3 ms |
11864 KB |
Output is correct |
22 |
Correct |
3 ms |
11612 KB |
Output is correct |
23 |
Correct |
3 ms |
11612 KB |
Output is correct |
24 |
Correct |
2 ms |
11608 KB |
Output is correct |
25 |
Correct |
4 ms |
11900 KB |
Output is correct |
26 |
Correct |
2 ms |
11612 KB |
Output is correct |
27 |
Correct |
2 ms |
11608 KB |
Output is correct |
28 |
Correct |
3 ms |
11612 KB |
Output is correct |
29 |
Correct |
2 ms |
11756 KB |
Output is correct |
30 |
Correct |
2 ms |
11792 KB |
Output is correct |
31 |
Correct |
4 ms |
12120 KB |
Output is correct |
32 |
Correct |
2 ms |
11608 KB |
Output is correct |
33 |
Correct |
4 ms |
11684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
11608 KB |
Output is correct |
2 |
Correct |
2 ms |
11608 KB |
Output is correct |
3 |
Correct |
2 ms |
11612 KB |
Output is correct |
4 |
Correct |
2 ms |
11612 KB |
Output is correct |
5 |
Correct |
3 ms |
11612 KB |
Output is correct |
6 |
Correct |
2 ms |
11612 KB |
Output is correct |
7 |
Correct |
3 ms |
11868 KB |
Output is correct |
8 |
Correct |
3 ms |
11868 KB |
Output is correct |
9 |
Correct |
4 ms |
11868 KB |
Output is correct |
10 |
Correct |
3 ms |
11612 KB |
Output is correct |
11 |
Correct |
3 ms |
11612 KB |
Output is correct |
12 |
Correct |
3 ms |
11612 KB |
Output is correct |
13 |
Correct |
2 ms |
11612 KB |
Output is correct |
14 |
Correct |
2 ms |
11612 KB |
Output is correct |
15 |
Correct |
3 ms |
11612 KB |
Output is correct |
16 |
Correct |
3 ms |
11612 KB |
Output is correct |
17 |
Correct |
3 ms |
11608 KB |
Output is correct |
18 |
Correct |
2 ms |
11612 KB |
Output is correct |
19 |
Correct |
3 ms |
11612 KB |
Output is correct |
20 |
Correct |
2 ms |
11676 KB |
Output is correct |
21 |
Correct |
3 ms |
11864 KB |
Output is correct |
22 |
Correct |
3 ms |
11612 KB |
Output is correct |
23 |
Correct |
3 ms |
11612 KB |
Output is correct |
24 |
Correct |
2 ms |
11608 KB |
Output is correct |
25 |
Correct |
4 ms |
11900 KB |
Output is correct |
26 |
Correct |
2 ms |
11612 KB |
Output is correct |
27 |
Correct |
2 ms |
11608 KB |
Output is correct |
28 |
Correct |
3 ms |
11612 KB |
Output is correct |
29 |
Correct |
2 ms |
11756 KB |
Output is correct |
30 |
Correct |
2 ms |
11792 KB |
Output is correct |
31 |
Correct |
4 ms |
12120 KB |
Output is correct |
32 |
Correct |
2 ms |
11608 KB |
Output is correct |
33 |
Correct |
4 ms |
11684 KB |
Output is correct |
34 |
Correct |
11 ms |
12376 KB |
Output is correct |
35 |
Correct |
203 ms |
33984 KB |
Output is correct |
36 |
Correct |
253 ms |
37476 KB |
Output is correct |
37 |
Correct |
274 ms |
37716 KB |
Output is correct |
38 |
Correct |
250 ms |
37200 KB |
Output is correct |
39 |
Correct |
6 ms |
12632 KB |
Output is correct |
40 |
Correct |
11 ms |
13660 KB |
Output is correct |
41 |
Correct |
15 ms |
13488 KB |
Output is correct |
42 |
Correct |
6 ms |
12636 KB |
Output is correct |
43 |
Correct |
11 ms |
12636 KB |
Output is correct |
44 |
Correct |
6 ms |
12636 KB |
Output is correct |
45 |
Correct |
6 ms |
12636 KB |
Output is correct |
46 |
Correct |
6 ms |
12428 KB |
Output is correct |
47 |
Correct |
10 ms |
13148 KB |
Output is correct |
48 |
Correct |
10 ms |
13148 KB |
Output is correct |
49 |
Correct |
17 ms |
14172 KB |
Output is correct |
50 |
Correct |
250 ms |
37932 KB |
Output is correct |
51 |
Correct |
12 ms |
13404 KB |
Output is correct |
52 |
Correct |
229 ms |
35664 KB |
Output is correct |
53 |
Correct |
16 ms |
14092 KB |
Output is correct |
54 |
Correct |
259 ms |
36688 KB |
Output is correct |
55 |
Correct |
9 ms |
13084 KB |
Output is correct |
56 |
Correct |
9 ms |
12892 KB |
Output is correct |
57 |
Correct |
10 ms |
12888 KB |
Output is correct |
58 |
Correct |
8 ms |
12892 KB |
Output is correct |
59 |
Correct |
9 ms |
13748 KB |
Output is correct |
60 |
Correct |
193 ms |
34640 KB |
Output is correct |
61 |
Correct |
10 ms |
13048 KB |
Output is correct |
62 |
Correct |
241 ms |
36948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
11608 KB |
Output is correct |
2 |
Correct |
2 ms |
11608 KB |
Output is correct |
3 |
Correct |
2 ms |
11612 KB |
Output is correct |
4 |
Correct |
2 ms |
11612 KB |
Output is correct |
5 |
Correct |
3 ms |
11612 KB |
Output is correct |
6 |
Correct |
2 ms |
11612 KB |
Output is correct |
7 |
Correct |
3 ms |
11868 KB |
Output is correct |
8 |
Correct |
3 ms |
11868 KB |
Output is correct |
9 |
Correct |
4 ms |
11868 KB |
Output is correct |
10 |
Correct |
3 ms |
11612 KB |
Output is correct |
11 |
Correct |
3 ms |
11612 KB |
Output is correct |
12 |
Correct |
3 ms |
11612 KB |
Output is correct |
13 |
Correct |
2 ms |
11612 KB |
Output is correct |
14 |
Correct |
2 ms |
11612 KB |
Output is correct |
15 |
Correct |
3 ms |
11612 KB |
Output is correct |
16 |
Correct |
3 ms |
11612 KB |
Output is correct |
17 |
Correct |
3 ms |
11608 KB |
Output is correct |
18 |
Correct |
2 ms |
11612 KB |
Output is correct |
19 |
Correct |
3 ms |
11612 KB |
Output is correct |
20 |
Correct |
2 ms |
11676 KB |
Output is correct |
21 |
Correct |
3 ms |
11864 KB |
Output is correct |
22 |
Correct |
3 ms |
11612 KB |
Output is correct |
23 |
Correct |
3 ms |
11612 KB |
Output is correct |
24 |
Correct |
2 ms |
11608 KB |
Output is correct |
25 |
Correct |
4 ms |
11900 KB |
Output is correct |
26 |
Correct |
2 ms |
11612 KB |
Output is correct |
27 |
Correct |
2 ms |
11608 KB |
Output is correct |
28 |
Correct |
3 ms |
11612 KB |
Output is correct |
29 |
Correct |
2 ms |
11756 KB |
Output is correct |
30 |
Correct |
2 ms |
11792 KB |
Output is correct |
31 |
Correct |
4 ms |
12120 KB |
Output is correct |
32 |
Correct |
2 ms |
11608 KB |
Output is correct |
33 |
Correct |
4 ms |
11684 KB |
Output is correct |
34 |
Correct |
11 ms |
12376 KB |
Output is correct |
35 |
Correct |
203 ms |
33984 KB |
Output is correct |
36 |
Correct |
253 ms |
37476 KB |
Output is correct |
37 |
Correct |
274 ms |
37716 KB |
Output is correct |
38 |
Correct |
250 ms |
37200 KB |
Output is correct |
39 |
Correct |
6 ms |
12632 KB |
Output is correct |
40 |
Correct |
11 ms |
13660 KB |
Output is correct |
41 |
Correct |
15 ms |
13488 KB |
Output is correct |
42 |
Correct |
6 ms |
12636 KB |
Output is correct |
43 |
Correct |
11 ms |
12636 KB |
Output is correct |
44 |
Correct |
6 ms |
12636 KB |
Output is correct |
45 |
Correct |
6 ms |
12636 KB |
Output is correct |
46 |
Correct |
6 ms |
12428 KB |
Output is correct |
47 |
Correct |
10 ms |
13148 KB |
Output is correct |
48 |
Correct |
10 ms |
13148 KB |
Output is correct |
49 |
Correct |
17 ms |
14172 KB |
Output is correct |
50 |
Correct |
250 ms |
37932 KB |
Output is correct |
51 |
Correct |
12 ms |
13404 KB |
Output is correct |
52 |
Correct |
229 ms |
35664 KB |
Output is correct |
53 |
Correct |
16 ms |
14092 KB |
Output is correct |
54 |
Correct |
259 ms |
36688 KB |
Output is correct |
55 |
Correct |
9 ms |
13084 KB |
Output is correct |
56 |
Correct |
9 ms |
12892 KB |
Output is correct |
57 |
Correct |
10 ms |
12888 KB |
Output is correct |
58 |
Correct |
8 ms |
12892 KB |
Output is correct |
59 |
Correct |
9 ms |
13748 KB |
Output is correct |
60 |
Correct |
193 ms |
34640 KB |
Output is correct |
61 |
Correct |
10 ms |
13048 KB |
Output is correct |
62 |
Correct |
241 ms |
36948 KB |
Output is correct |
63 |
Correct |
812 ms |
80208 KB |
Output is correct |
64 |
Correct |
743 ms |
80068 KB |
Output is correct |
65 |
Correct |
802 ms |
80208 KB |
Output is correct |
66 |
Correct |
433 ms |
59580 KB |
Output is correct |
67 |
Correct |
1089 ms |
137360 KB |
Output is correct |
68 |
Correct |
397 ms |
59472 KB |
Output is correct |
69 |
Correct |
545 ms |
59472 KB |
Output is correct |
70 |
Correct |
445 ms |
59612 KB |
Output is correct |
71 |
Correct |
461 ms |
59564 KB |
Output is correct |
72 |
Correct |
886 ms |
89424 KB |
Output is correct |
73 |
Correct |
970 ms |
94432 KB |
Output is correct |
74 |
Runtime error |
1917 ms |
1048576 KB |
Execution killed with signal 9 |
75 |
Halted |
0 ms |
0 KB |
- |