#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];
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);
}
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);
}
for(auto i : incoming[x]) {
if(root(i.second) == y) {
outgoing[y].erase(make_pair(i.second, i.first));
continue;
}
incoming[y].insert(i);
}
for(auto i : outgoing[x]) {
if(root(i.second) == y) {
incoming[y].erase(make_pair(i.second, i.first));
continue;
}
outgoing[y].insert(i);
}
par[x] = y;
sub[y] += sub[x];
}
}
bool exists(int i, int j) {
i = root(i); j = root(j);
return out[i].count(j);
}
int sz(int i) {
set <int> s;
for(auto j : incoming[i]) {
s.insert(j.second);
}
return s.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));
}
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 = 0;
for(int i = 1; i <= n; i++) {
if(root(i) == i) {
ans += 1LL * sub[i] * (sub[i] - 1);
ans += 1LL * sub[i] * sz(i);
}
}
printf("%lld\n", ans);
}
}
Compilation message
joitter2.cpp: In function 'int main()':
joitter2.cpp:61: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:69: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 |
12 ms |
19072 KB |
Output is correct |
2 |
Correct |
12 ms |
19072 KB |
Output is correct |
3 |
Correct |
12 ms |
19072 KB |
Output is correct |
4 |
Correct |
11 ms |
19072 KB |
Output is correct |
5 |
Correct |
11 ms |
19072 KB |
Output is correct |
6 |
Correct |
11 ms |
19072 KB |
Output is correct |
7 |
Correct |
14 ms |
19200 KB |
Output is correct |
8 |
Correct |
14 ms |
19200 KB |
Output is correct |
9 |
Correct |
14 ms |
19200 KB |
Output is correct |
10 |
Correct |
11 ms |
19072 KB |
Output is correct |
11 |
Correct |
12 ms |
19072 KB |
Output is correct |
12 |
Correct |
11 ms |
19072 KB |
Output is correct |
13 |
Correct |
11 ms |
19072 KB |
Output is correct |
14 |
Correct |
14 ms |
19072 KB |
Output is correct |
15 |
Correct |
12 ms |
19072 KB |
Output is correct |
16 |
Correct |
15 ms |
19072 KB |
Output is correct |
17 |
Correct |
12 ms |
19072 KB |
Output is correct |
18 |
Correct |
11 ms |
19072 KB |
Output is correct |
19 |
Correct |
13 ms |
19072 KB |
Output is correct |
20 |
Correct |
14 ms |
19128 KB |
Output is correct |
21 |
Correct |
14 ms |
19200 KB |
Output is correct |
22 |
Correct |
12 ms |
19072 KB |
Output is correct |
23 |
Correct |
14 ms |
19200 KB |
Output is correct |
24 |
Correct |
13 ms |
19200 KB |
Output is correct |
25 |
Correct |
14 ms |
19200 KB |
Output is correct |
26 |
Correct |
13 ms |
19072 KB |
Output is correct |
27 |
Correct |
12 ms |
19072 KB |
Output is correct |
28 |
Correct |
12 ms |
19072 KB |
Output is correct |
29 |
Correct |
12 ms |
19072 KB |
Output is correct |
30 |
Correct |
11 ms |
19072 KB |
Output is correct |
31 |
Correct |
13 ms |
19200 KB |
Output is correct |
32 |
Correct |
12 ms |
19072 KB |
Output is correct |
33 |
Correct |
13 ms |
19200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
19072 KB |
Output is correct |
2 |
Correct |
12 ms |
19072 KB |
Output is correct |
3 |
Correct |
12 ms |
19072 KB |
Output is correct |
4 |
Correct |
11 ms |
19072 KB |
Output is correct |
5 |
Correct |
11 ms |
19072 KB |
Output is correct |
6 |
Correct |
11 ms |
19072 KB |
Output is correct |
7 |
Correct |
14 ms |
19200 KB |
Output is correct |
8 |
Correct |
14 ms |
19200 KB |
Output is correct |
9 |
Correct |
14 ms |
19200 KB |
Output is correct |
10 |
Correct |
11 ms |
19072 KB |
Output is correct |
11 |
Correct |
12 ms |
19072 KB |
Output is correct |
12 |
Correct |
11 ms |
19072 KB |
Output is correct |
13 |
Correct |
11 ms |
19072 KB |
Output is correct |
14 |
Correct |
14 ms |
19072 KB |
Output is correct |
15 |
Correct |
12 ms |
19072 KB |
Output is correct |
16 |
Correct |
15 ms |
19072 KB |
Output is correct |
17 |
Correct |
12 ms |
19072 KB |
Output is correct |
18 |
Correct |
11 ms |
19072 KB |
Output is correct |
19 |
Correct |
13 ms |
19072 KB |
Output is correct |
20 |
Correct |
14 ms |
19128 KB |
Output is correct |
21 |
Correct |
14 ms |
19200 KB |
Output is correct |
22 |
Correct |
12 ms |
19072 KB |
Output is correct |
23 |
Correct |
14 ms |
19200 KB |
Output is correct |
24 |
Correct |
13 ms |
19200 KB |
Output is correct |
25 |
Correct |
14 ms |
19200 KB |
Output is correct |
26 |
Correct |
13 ms |
19072 KB |
Output is correct |
27 |
Correct |
12 ms |
19072 KB |
Output is correct |
28 |
Correct |
12 ms |
19072 KB |
Output is correct |
29 |
Correct |
12 ms |
19072 KB |
Output is correct |
30 |
Correct |
11 ms |
19072 KB |
Output is correct |
31 |
Correct |
13 ms |
19200 KB |
Output is correct |
32 |
Correct |
12 ms |
19072 KB |
Output is correct |
33 |
Correct |
13 ms |
19200 KB |
Output is correct |
34 |
Correct |
24 ms |
19328 KB |
Output is correct |
35 |
Correct |
4115 ms |
25688 KB |
Output is correct |
36 |
Execution timed out |
5041 ms |
22204 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
19072 KB |
Output is correct |
2 |
Correct |
12 ms |
19072 KB |
Output is correct |
3 |
Correct |
12 ms |
19072 KB |
Output is correct |
4 |
Correct |
11 ms |
19072 KB |
Output is correct |
5 |
Correct |
11 ms |
19072 KB |
Output is correct |
6 |
Correct |
11 ms |
19072 KB |
Output is correct |
7 |
Correct |
14 ms |
19200 KB |
Output is correct |
8 |
Correct |
14 ms |
19200 KB |
Output is correct |
9 |
Correct |
14 ms |
19200 KB |
Output is correct |
10 |
Correct |
11 ms |
19072 KB |
Output is correct |
11 |
Correct |
12 ms |
19072 KB |
Output is correct |
12 |
Correct |
11 ms |
19072 KB |
Output is correct |
13 |
Correct |
11 ms |
19072 KB |
Output is correct |
14 |
Correct |
14 ms |
19072 KB |
Output is correct |
15 |
Correct |
12 ms |
19072 KB |
Output is correct |
16 |
Correct |
15 ms |
19072 KB |
Output is correct |
17 |
Correct |
12 ms |
19072 KB |
Output is correct |
18 |
Correct |
11 ms |
19072 KB |
Output is correct |
19 |
Correct |
13 ms |
19072 KB |
Output is correct |
20 |
Correct |
14 ms |
19128 KB |
Output is correct |
21 |
Correct |
14 ms |
19200 KB |
Output is correct |
22 |
Correct |
12 ms |
19072 KB |
Output is correct |
23 |
Correct |
14 ms |
19200 KB |
Output is correct |
24 |
Correct |
13 ms |
19200 KB |
Output is correct |
25 |
Correct |
14 ms |
19200 KB |
Output is correct |
26 |
Correct |
13 ms |
19072 KB |
Output is correct |
27 |
Correct |
12 ms |
19072 KB |
Output is correct |
28 |
Correct |
12 ms |
19072 KB |
Output is correct |
29 |
Correct |
12 ms |
19072 KB |
Output is correct |
30 |
Correct |
11 ms |
19072 KB |
Output is correct |
31 |
Correct |
13 ms |
19200 KB |
Output is correct |
32 |
Correct |
12 ms |
19072 KB |
Output is correct |
33 |
Correct |
13 ms |
19200 KB |
Output is correct |
34 |
Correct |
24 ms |
19328 KB |
Output is correct |
35 |
Correct |
4115 ms |
25688 KB |
Output is correct |
36 |
Execution timed out |
5041 ms |
22204 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |