#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
set <int> in[maxn], out[maxn];
set <pair <int, int>> incoming[maxn], outgoing[maxn];
set <int> cont[maxn];
long long edgeCnt;
queue <pair <int, int>> Q;
int par[maxn];
int sub[maxn];
int root(int x) {
if(par[x] == x) return x;
return par[x] = root(par[x]);
}
bool change(set <int> &t, set <int> &r, int i, int j) {
t.erase(i); t.insert(j);
return r.count(j);
}
long long totalCont(int i) {
return 1LL * cont[i].size() * sub[i];
}
void join(int x, int y) {
x = root(x);
y = root(y);
if(incoming[x].size() + outgoing[x].size() > incoming[y].size() + outgoing[y].size()) swap(x, y);
if(x != y) {
for(int i : in[x]) {
in[y].insert(i);
if(change(out[i], in[i], x, y)) Q.emplace(i, y);
}
for(int i : out[x]) {
out[y].insert(i);
if(change(in[i], out[i], x, y)) Q.emplace(i, y);
}
edgeCnt -= totalCont(x) + totalCont(y);
for(auto i : incoming[x]) {
if(root(i.second) == y) {
outgoing[y].erase(make_pair(i.second, i.first));
continue;
}
incoming[y].insert(i);
cont[y].insert(i.second);
}
for(auto i : outgoing[x]) {
if(root(i.second) == y) {
incoming[y].erase(make_pair(i.second, i.first));
cont[y].erase(i.first);
continue;
}
outgoing[y].insert(i);
}
par[x] = y;
sub[y] += sub[x];
edgeCnt += totalCont(y);
}
}
bool exists(int i, int j) {
i = root(i); j = root(j);
return out[i].count(j);
}
int sz(int i) {
return cont[i].size();
}
int main() {
int n, m;
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; i++) {
par[i] = i;
sub[i] = 1;
}
vector <pair <int, int>> edge;
for(int x = 1; x <= m; x++) {
int p, q;
scanf("%d %d", &p, &q);
if(root(p) != root(q)) {
incoming[root(q)].insert(make_pair(q, p));
outgoing[root(p)].insert(make_pair(p, q));
edgeCnt -= totalCont(root(q));
cont[root(q)].insert(p);
edgeCnt += totalCont(root(q));
}
p = root(p);
q = root(q);
out[p].insert(q);
in[q].insert(p);
if(exists(p, q) && exists(q, p)) Q.emplace(root(p), root(q));
while(!Q.empty()) {
int p = Q.front().first;
int q = Q.front().second;
Q.pop();
join(p, q);
}
long long ans = edgeCnt;
for(int i = 1; i <= n; i++) {
if(root(i) == i) {
ans += 1LL * sub[i] * (sub[i] - 1);
}
}
printf("%lld\n", ans);
}
}
Compilation message
joitter2.cpp: In function 'int main()':
joitter2.cpp:67:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~~
joitter2.cpp:75:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &p, &q);
~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
23808 KB |
Output is correct |
2 |
Correct |
16 ms |
23808 KB |
Output is correct |
3 |
Correct |
15 ms |
23808 KB |
Output is correct |
4 |
Correct |
16 ms |
23808 KB |
Output is correct |
5 |
Correct |
14 ms |
23808 KB |
Output is correct |
6 |
Correct |
14 ms |
23808 KB |
Output is correct |
7 |
Correct |
16 ms |
23808 KB |
Output is correct |
8 |
Correct |
15 ms |
23936 KB |
Output is correct |
9 |
Correct |
15 ms |
23808 KB |
Output is correct |
10 |
Correct |
14 ms |
23808 KB |
Output is correct |
11 |
Correct |
14 ms |
23808 KB |
Output is correct |
12 |
Correct |
14 ms |
23808 KB |
Output is correct |
13 |
Correct |
19 ms |
23808 KB |
Output is correct |
14 |
Correct |
15 ms |
23808 KB |
Output is correct |
15 |
Correct |
19 ms |
23808 KB |
Output is correct |
16 |
Correct |
13 ms |
23808 KB |
Output is correct |
17 |
Correct |
19 ms |
23808 KB |
Output is correct |
18 |
Correct |
14 ms |
23808 KB |
Output is correct |
19 |
Correct |
16 ms |
23808 KB |
Output is correct |
20 |
Correct |
14 ms |
23808 KB |
Output is correct |
21 |
Correct |
15 ms |
23808 KB |
Output is correct |
22 |
Correct |
13 ms |
23808 KB |
Output is correct |
23 |
Correct |
14 ms |
23808 KB |
Output is correct |
24 |
Correct |
15 ms |
23808 KB |
Output is correct |
25 |
Correct |
18 ms |
23808 KB |
Output is correct |
26 |
Correct |
14 ms |
23808 KB |
Output is correct |
27 |
Correct |
16 ms |
23808 KB |
Output is correct |
28 |
Correct |
14 ms |
23808 KB |
Output is correct |
29 |
Correct |
14 ms |
23808 KB |
Output is correct |
30 |
Correct |
14 ms |
23808 KB |
Output is correct |
31 |
Correct |
15 ms |
23808 KB |
Output is correct |
32 |
Correct |
14 ms |
23808 KB |
Output is correct |
33 |
Correct |
16 ms |
23808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
23808 KB |
Output is correct |
2 |
Correct |
16 ms |
23808 KB |
Output is correct |
3 |
Correct |
15 ms |
23808 KB |
Output is correct |
4 |
Correct |
16 ms |
23808 KB |
Output is correct |
5 |
Correct |
14 ms |
23808 KB |
Output is correct |
6 |
Correct |
14 ms |
23808 KB |
Output is correct |
7 |
Correct |
16 ms |
23808 KB |
Output is correct |
8 |
Correct |
15 ms |
23936 KB |
Output is correct |
9 |
Correct |
15 ms |
23808 KB |
Output is correct |
10 |
Correct |
14 ms |
23808 KB |
Output is correct |
11 |
Correct |
14 ms |
23808 KB |
Output is correct |
12 |
Correct |
14 ms |
23808 KB |
Output is correct |
13 |
Correct |
19 ms |
23808 KB |
Output is correct |
14 |
Correct |
15 ms |
23808 KB |
Output is correct |
15 |
Correct |
19 ms |
23808 KB |
Output is correct |
16 |
Correct |
13 ms |
23808 KB |
Output is correct |
17 |
Correct |
19 ms |
23808 KB |
Output is correct |
18 |
Correct |
14 ms |
23808 KB |
Output is correct |
19 |
Correct |
16 ms |
23808 KB |
Output is correct |
20 |
Correct |
14 ms |
23808 KB |
Output is correct |
21 |
Correct |
15 ms |
23808 KB |
Output is correct |
22 |
Correct |
13 ms |
23808 KB |
Output is correct |
23 |
Correct |
14 ms |
23808 KB |
Output is correct |
24 |
Correct |
15 ms |
23808 KB |
Output is correct |
25 |
Correct |
18 ms |
23808 KB |
Output is correct |
26 |
Correct |
14 ms |
23808 KB |
Output is correct |
27 |
Correct |
16 ms |
23808 KB |
Output is correct |
28 |
Correct |
14 ms |
23808 KB |
Output is correct |
29 |
Correct |
14 ms |
23808 KB |
Output is correct |
30 |
Correct |
14 ms |
23808 KB |
Output is correct |
31 |
Correct |
15 ms |
23808 KB |
Output is correct |
32 |
Correct |
14 ms |
23808 KB |
Output is correct |
33 |
Correct |
16 ms |
23808 KB |
Output is correct |
34 |
Correct |
23 ms |
23936 KB |
Output is correct |
35 |
Correct |
967 ms |
28408 KB |
Output is correct |
36 |
Correct |
1786 ms |
32356 KB |
Output is correct |
37 |
Correct |
1806 ms |
32500 KB |
Output is correct |
38 |
Correct |
1821 ms |
31932 KB |
Output is correct |
39 |
Correct |
41 ms |
24312 KB |
Output is correct |
40 |
Correct |
39 ms |
24440 KB |
Output is correct |
41 |
Correct |
40 ms |
24444 KB |
Output is correct |
42 |
Correct |
42 ms |
24312 KB |
Output is correct |
43 |
Correct |
40 ms |
24312 KB |
Output is correct |
44 |
Correct |
42 ms |
24320 KB |
Output is correct |
45 |
Correct |
40 ms |
24312 KB |
Output is correct |
46 |
Correct |
44 ms |
24444 KB |
Output is correct |
47 |
Correct |
43 ms |
24440 KB |
Output is correct |
48 |
Correct |
39 ms |
24448 KB |
Output is correct |
49 |
Correct |
76 ms |
25720 KB |
Output is correct |
50 |
Correct |
1794 ms |
32740 KB |
Output is correct |
51 |
Correct |
81 ms |
24696 KB |
Output is correct |
52 |
Correct |
1777 ms |
29676 KB |
Output is correct |
53 |
Correct |
81 ms |
25592 KB |
Output is correct |
54 |
Correct |
1782 ms |
31308 KB |
Output is correct |
55 |
Correct |
34 ms |
24952 KB |
Output is correct |
56 |
Correct |
36 ms |
24960 KB |
Output is correct |
57 |
Correct |
34 ms |
24900 KB |
Output is correct |
58 |
Correct |
38 ms |
24824 KB |
Output is correct |
59 |
Correct |
47 ms |
24320 KB |
Output is correct |
60 |
Correct |
1780 ms |
26764 KB |
Output is correct |
61 |
Correct |
44 ms |
24568 KB |
Output is correct |
62 |
Correct |
1764 ms |
31752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
23808 KB |
Output is correct |
2 |
Correct |
16 ms |
23808 KB |
Output is correct |
3 |
Correct |
15 ms |
23808 KB |
Output is correct |
4 |
Correct |
16 ms |
23808 KB |
Output is correct |
5 |
Correct |
14 ms |
23808 KB |
Output is correct |
6 |
Correct |
14 ms |
23808 KB |
Output is correct |
7 |
Correct |
16 ms |
23808 KB |
Output is correct |
8 |
Correct |
15 ms |
23936 KB |
Output is correct |
9 |
Correct |
15 ms |
23808 KB |
Output is correct |
10 |
Correct |
14 ms |
23808 KB |
Output is correct |
11 |
Correct |
14 ms |
23808 KB |
Output is correct |
12 |
Correct |
14 ms |
23808 KB |
Output is correct |
13 |
Correct |
19 ms |
23808 KB |
Output is correct |
14 |
Correct |
15 ms |
23808 KB |
Output is correct |
15 |
Correct |
19 ms |
23808 KB |
Output is correct |
16 |
Correct |
13 ms |
23808 KB |
Output is correct |
17 |
Correct |
19 ms |
23808 KB |
Output is correct |
18 |
Correct |
14 ms |
23808 KB |
Output is correct |
19 |
Correct |
16 ms |
23808 KB |
Output is correct |
20 |
Correct |
14 ms |
23808 KB |
Output is correct |
21 |
Correct |
15 ms |
23808 KB |
Output is correct |
22 |
Correct |
13 ms |
23808 KB |
Output is correct |
23 |
Correct |
14 ms |
23808 KB |
Output is correct |
24 |
Correct |
15 ms |
23808 KB |
Output is correct |
25 |
Correct |
18 ms |
23808 KB |
Output is correct |
26 |
Correct |
14 ms |
23808 KB |
Output is correct |
27 |
Correct |
16 ms |
23808 KB |
Output is correct |
28 |
Correct |
14 ms |
23808 KB |
Output is correct |
29 |
Correct |
14 ms |
23808 KB |
Output is correct |
30 |
Correct |
14 ms |
23808 KB |
Output is correct |
31 |
Correct |
15 ms |
23808 KB |
Output is correct |
32 |
Correct |
14 ms |
23808 KB |
Output is correct |
33 |
Correct |
16 ms |
23808 KB |
Output is correct |
34 |
Correct |
23 ms |
23936 KB |
Output is correct |
35 |
Correct |
967 ms |
28408 KB |
Output is correct |
36 |
Correct |
1786 ms |
32356 KB |
Output is correct |
37 |
Correct |
1806 ms |
32500 KB |
Output is correct |
38 |
Correct |
1821 ms |
31932 KB |
Output is correct |
39 |
Correct |
41 ms |
24312 KB |
Output is correct |
40 |
Correct |
39 ms |
24440 KB |
Output is correct |
41 |
Correct |
40 ms |
24444 KB |
Output is correct |
42 |
Correct |
42 ms |
24312 KB |
Output is correct |
43 |
Correct |
40 ms |
24312 KB |
Output is correct |
44 |
Correct |
42 ms |
24320 KB |
Output is correct |
45 |
Correct |
40 ms |
24312 KB |
Output is correct |
46 |
Correct |
44 ms |
24444 KB |
Output is correct |
47 |
Correct |
43 ms |
24440 KB |
Output is correct |
48 |
Correct |
39 ms |
24448 KB |
Output is correct |
49 |
Correct |
76 ms |
25720 KB |
Output is correct |
50 |
Correct |
1794 ms |
32740 KB |
Output is correct |
51 |
Correct |
81 ms |
24696 KB |
Output is correct |
52 |
Correct |
1777 ms |
29676 KB |
Output is correct |
53 |
Correct |
81 ms |
25592 KB |
Output is correct |
54 |
Correct |
1782 ms |
31308 KB |
Output is correct |
55 |
Correct |
34 ms |
24952 KB |
Output is correct |
56 |
Correct |
36 ms |
24960 KB |
Output is correct |
57 |
Correct |
34 ms |
24900 KB |
Output is correct |
58 |
Correct |
38 ms |
24824 KB |
Output is correct |
59 |
Correct |
47 ms |
24320 KB |
Output is correct |
60 |
Correct |
1780 ms |
26764 KB |
Output is correct |
61 |
Correct |
44 ms |
24568 KB |
Output is correct |
62 |
Correct |
1764 ms |
31752 KB |
Output is correct |
63 |
Execution timed out |
5068 ms |
34976 KB |
Time limit exceeded |
64 |
Halted |
0 ms |
0 KB |
- |