#include <bits/stdc++.h>
using namespace std;
#define all(s) s.begin(), s.end()
#define ll long long
#define fr first
#define sc second
#define pb push_back
#define mk make_pair
#define pi pair < int, int >
const int N = 3e5 + 7;
const int INF = 1e9 + 7;
ll sum;
ll p[N];
int n, m;
set < int > g[N], q[N];
int findp (int x){
if (p[x] < 0)
return x;
return p[x] = findp(p[x]);
}
bool check (int v, int u){
v = findp(v);
u = findp(u);
if (v != u){
for (auto to : g[u])
if (findp(to) == v)
return true;
}
return false;
}
void unite (int v, int u){
v = findp(v);
u = findp(u);
if (v != u){
sum -= 1ll * int(g[u].size()) * -p[u];
sum -= 1ll * int(g[v].size()) * -p[v];
if (g[u].size() > g[v].size())
swap(v, u);
for (auto to : g[u]){
g[v].insert(to);
}
g[v].insert(u);
g[v].erase(v);
p[v] += p[u];
p[u] = v;
sum += 1ll * int(g[v].size()) * -p[v];
// cout << v << ' ' << u << endl;
for (auto to : g[v]){
if (check(v, to)){
unite(v, to);
}
}
}
}
main (){
ios_base::sync_with_stdio(0);
cin.tie(0);
memset(p, -1, sizeof(p));
cin >> n >> m;
for (int i = 1; i <= m; i++){
int a, b;
cin >> a >> b;
// if (q[findp(b)].find(findp(a)) != q.end())
if (check(b, a))
unite(a, b);
else {
b = findp(b);
sum -= 1ll * int(g[b].size()) * -p[b];
g[b].insert(a);
g[b].erase(b);
// a = findp(a);
// q[a].insert(b);
sum += 1ll * int(g[b].size()) * -p[b];
}
cout << sum << endl;
}
}
Compilation message
joitter2.cpp:63:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
63 | main (){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
30796 KB |
Output is correct |
2 |
Correct |
16 ms |
30796 KB |
Output is correct |
3 |
Correct |
16 ms |
30744 KB |
Output is correct |
4 |
Correct |
16 ms |
30796 KB |
Output is correct |
5 |
Correct |
17 ms |
30796 KB |
Output is correct |
6 |
Correct |
17 ms |
30796 KB |
Output is correct |
7 |
Correct |
21 ms |
30776 KB |
Output is correct |
8 |
Correct |
21 ms |
30796 KB |
Output is correct |
9 |
Correct |
22 ms |
30856 KB |
Output is correct |
10 |
Correct |
17 ms |
30796 KB |
Output is correct |
11 |
Correct |
17 ms |
30780 KB |
Output is correct |
12 |
Correct |
17 ms |
30836 KB |
Output is correct |
13 |
Correct |
17 ms |
30808 KB |
Output is correct |
14 |
Correct |
17 ms |
30776 KB |
Output is correct |
15 |
Correct |
17 ms |
30796 KB |
Output is correct |
16 |
Correct |
18 ms |
30844 KB |
Output is correct |
17 |
Correct |
18 ms |
30796 KB |
Output is correct |
18 |
Correct |
17 ms |
30756 KB |
Output is correct |
19 |
Correct |
17 ms |
30796 KB |
Output is correct |
20 |
Correct |
22 ms |
30756 KB |
Output is correct |
21 |
Correct |
22 ms |
30816 KB |
Output is correct |
22 |
Correct |
18 ms |
30808 KB |
Output is correct |
23 |
Correct |
19 ms |
30852 KB |
Output is correct |
24 |
Correct |
17 ms |
30836 KB |
Output is correct |
25 |
Correct |
23 ms |
30856 KB |
Output is correct |
26 |
Correct |
18 ms |
30788 KB |
Output is correct |
27 |
Correct |
18 ms |
30808 KB |
Output is correct |
28 |
Correct |
19 ms |
30796 KB |
Output is correct |
29 |
Correct |
18 ms |
30840 KB |
Output is correct |
30 |
Correct |
17 ms |
30796 KB |
Output is correct |
31 |
Correct |
24 ms |
30852 KB |
Output is correct |
32 |
Correct |
18 ms |
30796 KB |
Output is correct |
33 |
Correct |
21 ms |
30796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
30796 KB |
Output is correct |
2 |
Correct |
16 ms |
30796 KB |
Output is correct |
3 |
Correct |
16 ms |
30744 KB |
Output is correct |
4 |
Correct |
16 ms |
30796 KB |
Output is correct |
5 |
Correct |
17 ms |
30796 KB |
Output is correct |
6 |
Correct |
17 ms |
30796 KB |
Output is correct |
7 |
Correct |
21 ms |
30776 KB |
Output is correct |
8 |
Correct |
21 ms |
30796 KB |
Output is correct |
9 |
Correct |
22 ms |
30856 KB |
Output is correct |
10 |
Correct |
17 ms |
30796 KB |
Output is correct |
11 |
Correct |
17 ms |
30780 KB |
Output is correct |
12 |
Correct |
17 ms |
30836 KB |
Output is correct |
13 |
Correct |
17 ms |
30808 KB |
Output is correct |
14 |
Correct |
17 ms |
30776 KB |
Output is correct |
15 |
Correct |
17 ms |
30796 KB |
Output is correct |
16 |
Correct |
18 ms |
30844 KB |
Output is correct |
17 |
Correct |
18 ms |
30796 KB |
Output is correct |
18 |
Correct |
17 ms |
30756 KB |
Output is correct |
19 |
Correct |
17 ms |
30796 KB |
Output is correct |
20 |
Correct |
22 ms |
30756 KB |
Output is correct |
21 |
Correct |
22 ms |
30816 KB |
Output is correct |
22 |
Correct |
18 ms |
30808 KB |
Output is correct |
23 |
Correct |
19 ms |
30852 KB |
Output is correct |
24 |
Correct |
17 ms |
30836 KB |
Output is correct |
25 |
Correct |
23 ms |
30856 KB |
Output is correct |
26 |
Correct |
18 ms |
30788 KB |
Output is correct |
27 |
Correct |
18 ms |
30808 KB |
Output is correct |
28 |
Correct |
19 ms |
30796 KB |
Output is correct |
29 |
Correct |
18 ms |
30840 KB |
Output is correct |
30 |
Correct |
17 ms |
30796 KB |
Output is correct |
31 |
Correct |
24 ms |
30852 KB |
Output is correct |
32 |
Correct |
18 ms |
30796 KB |
Output is correct |
33 |
Correct |
21 ms |
30796 KB |
Output is correct |
34 |
Correct |
35 ms |
30956 KB |
Output is correct |
35 |
Correct |
632 ms |
35920 KB |
Output is correct |
36 |
Correct |
728 ms |
37244 KB |
Output is correct |
37 |
Correct |
742 ms |
37300 KB |
Output is correct |
38 |
Correct |
693 ms |
37132 KB |
Output is correct |
39 |
Correct |
103 ms |
30972 KB |
Output is correct |
40 |
Correct |
30 ms |
31316 KB |
Output is correct |
41 |
Correct |
27 ms |
31188 KB |
Output is correct |
42 |
Correct |
113 ms |
31080 KB |
Output is correct |
43 |
Correct |
96 ms |
30960 KB |
Output is correct |
44 |
Correct |
95 ms |
30968 KB |
Output is correct |
45 |
Correct |
95 ms |
31080 KB |
Output is correct |
46 |
Correct |
98 ms |
31040 KB |
Output is correct |
47 |
Correct |
36 ms |
31100 KB |
Output is correct |
48 |
Correct |
36 ms |
31180 KB |
Output is correct |
49 |
Correct |
80 ms |
31448 KB |
Output is correct |
50 |
Correct |
728 ms |
37192 KB |
Output is correct |
51 |
Correct |
117 ms |
31172 KB |
Output is correct |
52 |
Correct |
745 ms |
36684 KB |
Output is correct |
53 |
Correct |
106 ms |
31444 KB |
Output is correct |
54 |
Correct |
704 ms |
36932 KB |
Output is correct |
55 |
Correct |
94 ms |
31300 KB |
Output is correct |
56 |
Correct |
95 ms |
31300 KB |
Output is correct |
57 |
Correct |
94 ms |
31300 KB |
Output is correct |
58 |
Correct |
91 ms |
31360 KB |
Output is correct |
59 |
Correct |
27 ms |
31324 KB |
Output is correct |
60 |
Correct |
706 ms |
36260 KB |
Output is correct |
61 |
Correct |
54 ms |
31052 KB |
Output is correct |
62 |
Correct |
768 ms |
37104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
30796 KB |
Output is correct |
2 |
Correct |
16 ms |
30796 KB |
Output is correct |
3 |
Correct |
16 ms |
30744 KB |
Output is correct |
4 |
Correct |
16 ms |
30796 KB |
Output is correct |
5 |
Correct |
17 ms |
30796 KB |
Output is correct |
6 |
Correct |
17 ms |
30796 KB |
Output is correct |
7 |
Correct |
21 ms |
30776 KB |
Output is correct |
8 |
Correct |
21 ms |
30796 KB |
Output is correct |
9 |
Correct |
22 ms |
30856 KB |
Output is correct |
10 |
Correct |
17 ms |
30796 KB |
Output is correct |
11 |
Correct |
17 ms |
30780 KB |
Output is correct |
12 |
Correct |
17 ms |
30836 KB |
Output is correct |
13 |
Correct |
17 ms |
30808 KB |
Output is correct |
14 |
Correct |
17 ms |
30776 KB |
Output is correct |
15 |
Correct |
17 ms |
30796 KB |
Output is correct |
16 |
Correct |
18 ms |
30844 KB |
Output is correct |
17 |
Correct |
18 ms |
30796 KB |
Output is correct |
18 |
Correct |
17 ms |
30756 KB |
Output is correct |
19 |
Correct |
17 ms |
30796 KB |
Output is correct |
20 |
Correct |
22 ms |
30756 KB |
Output is correct |
21 |
Correct |
22 ms |
30816 KB |
Output is correct |
22 |
Correct |
18 ms |
30808 KB |
Output is correct |
23 |
Correct |
19 ms |
30852 KB |
Output is correct |
24 |
Correct |
17 ms |
30836 KB |
Output is correct |
25 |
Correct |
23 ms |
30856 KB |
Output is correct |
26 |
Correct |
18 ms |
30788 KB |
Output is correct |
27 |
Correct |
18 ms |
30808 KB |
Output is correct |
28 |
Correct |
19 ms |
30796 KB |
Output is correct |
29 |
Correct |
18 ms |
30840 KB |
Output is correct |
30 |
Correct |
17 ms |
30796 KB |
Output is correct |
31 |
Correct |
24 ms |
30852 KB |
Output is correct |
32 |
Correct |
18 ms |
30796 KB |
Output is correct |
33 |
Correct |
21 ms |
30796 KB |
Output is correct |
34 |
Correct |
35 ms |
30956 KB |
Output is correct |
35 |
Correct |
632 ms |
35920 KB |
Output is correct |
36 |
Correct |
728 ms |
37244 KB |
Output is correct |
37 |
Correct |
742 ms |
37300 KB |
Output is correct |
38 |
Correct |
693 ms |
37132 KB |
Output is correct |
39 |
Correct |
103 ms |
30972 KB |
Output is correct |
40 |
Correct |
30 ms |
31316 KB |
Output is correct |
41 |
Correct |
27 ms |
31188 KB |
Output is correct |
42 |
Correct |
113 ms |
31080 KB |
Output is correct |
43 |
Correct |
96 ms |
30960 KB |
Output is correct |
44 |
Correct |
95 ms |
30968 KB |
Output is correct |
45 |
Correct |
95 ms |
31080 KB |
Output is correct |
46 |
Correct |
98 ms |
31040 KB |
Output is correct |
47 |
Correct |
36 ms |
31100 KB |
Output is correct |
48 |
Correct |
36 ms |
31180 KB |
Output is correct |
49 |
Correct |
80 ms |
31448 KB |
Output is correct |
50 |
Correct |
728 ms |
37192 KB |
Output is correct |
51 |
Correct |
117 ms |
31172 KB |
Output is correct |
52 |
Correct |
745 ms |
36684 KB |
Output is correct |
53 |
Correct |
106 ms |
31444 KB |
Output is correct |
54 |
Correct |
704 ms |
36932 KB |
Output is correct |
55 |
Correct |
94 ms |
31300 KB |
Output is correct |
56 |
Correct |
95 ms |
31300 KB |
Output is correct |
57 |
Correct |
94 ms |
31300 KB |
Output is correct |
58 |
Correct |
91 ms |
31360 KB |
Output is correct |
59 |
Correct |
27 ms |
31324 KB |
Output is correct |
60 |
Correct |
706 ms |
36260 KB |
Output is correct |
61 |
Correct |
54 ms |
31052 KB |
Output is correct |
62 |
Correct |
768 ms |
37104 KB |
Output is correct |
63 |
Correct |
853 ms |
50296 KB |
Output is correct |
64 |
Correct |
771 ms |
50272 KB |
Output is correct |
65 |
Correct |
761 ms |
50424 KB |
Output is correct |
66 |
Execution timed out |
5053 ms |
32808 KB |
Time limit exceeded |
67 |
Halted |
0 ms |
0 KB |
- |