#include <bits/stdc++.h>
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
using namespace std;
using lint = long long;
using pi = pair<int, int>;
const int MAXN = 300005;
const int mod = 1e9 + 7;
int n, m;
pi a[MAXN], b[MAXN];
set<pi> sa, sb;
lint InSaYEnd[MAXN];
int pa[MAXN];
vector<int> memb[MAXN], inc[MAXN];
int f(int x){ return sz(memb[x]) + sz(inc[x]); }
queue<pi> que;
lint dap;
void uni(int x, int y){
x = pa[x]; y = pa[y];
if(x == y) return;
if(f(x) < f(y)) swap(x, y);
dap += sz(memb[x]) * sz(memb[y]) * 2;
dap -= sz(memb[y]) * InSaYEnd[y];
dap -= sz(memb[x]) * InSaYEnd[x];
for(auto &i : memb[y]){
memb[x].push_back(i);
pa[i] = x;
}
for(auto &i : inc[y]){
if(sa.count(a[i])){
if(a[i].second != x && a[i].second != y) dap -= sz(memb[a[i].second]);
InSaYEnd[a[i].second]--;
sa.erase(a[i]);
}
sb.erase(b[i]);
a[i] = pi(a[i].first, pa[a[i].second]);
b[i] = pi(pa[a[i].first], pa[a[i].second]);
if(pa[a[i].first] != pa[a[i].second] && !sa.count(a[i])){
if(a[i].second != x) dap += sz(memb[pa[a[i].second]]);
InSaYEnd[a[i].second]++;
sa.insert(a[i]);
}
sb.insert(b[i]);
if(sb.find(pi(b[i].second, b[i].first)) != sb.end()) que.push(b[i]);
inc[x].push_back(i);
}
dap += InSaYEnd[x] * sz(memb[x]);
inc[y].clear();
memb[y].clear();
}
int main(){
scanf("%d %d",&n,&m);
for(int i=0; i<n; i++){
memb[i].push_back(i);
pa[i] = i;
}
for(int i=0; i<m; i++){
int x, y; scanf("%d %d",&x,&y);
x--; y--;
a[i] = pi(x, pa[y]);
b[i] = pi(pa[x], pa[y]);
if(pa[x] != pa[y]){
inc[pa[x]].push_back(i);
inc[pa[y]].push_back(i);
if(!sa.count(pi(x, pa[y]))){
dap += sz(memb[pa[y]]);
InSaYEnd[pa[y]]++;
sa.emplace(x, pa[y]);
}
sb.emplace(pa[x], pa[y]);
if(sb.count(pi(pa[y], pa[x]))){
que.emplace(x, y);
}
}
while(sz(que)){
int x, y;
tie(x, y) = que.front(); que.pop();
uni(x, y);
}
printf("%lld\n", dap);
}
}
Compilation message
joitter2.cpp: In function 'int main()':
joitter2.cpp:58:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
58 | scanf("%d %d",&n,&m);
| ~~~~~^~~~~~~~~~~~~~~
joitter2.cpp:64:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
64 | int x, y; scanf("%d %d",&x,&y);
| ~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14464 KB |
Output is correct |
2 |
Correct |
11 ms |
14464 KB |
Output is correct |
3 |
Correct |
12 ms |
14396 KB |
Output is correct |
4 |
Correct |
10 ms |
14464 KB |
Output is correct |
5 |
Correct |
11 ms |
14476 KB |
Output is correct |
6 |
Correct |
10 ms |
14464 KB |
Output is correct |
7 |
Correct |
11 ms |
14464 KB |
Output is correct |
8 |
Correct |
14 ms |
14464 KB |
Output is correct |
9 |
Correct |
12 ms |
14464 KB |
Output is correct |
10 |
Correct |
12 ms |
14592 KB |
Output is correct |
11 |
Correct |
11 ms |
14464 KB |
Output is correct |
12 |
Correct |
10 ms |
14464 KB |
Output is correct |
13 |
Correct |
12 ms |
14464 KB |
Output is correct |
14 |
Correct |
14 ms |
14464 KB |
Output is correct |
15 |
Correct |
10 ms |
14464 KB |
Output is correct |
16 |
Correct |
13 ms |
14464 KB |
Output is correct |
17 |
Correct |
10 ms |
14464 KB |
Output is correct |
18 |
Correct |
12 ms |
14504 KB |
Output is correct |
19 |
Correct |
10 ms |
14464 KB |
Output is correct |
20 |
Correct |
13 ms |
14440 KB |
Output is correct |
21 |
Correct |
11 ms |
14464 KB |
Output is correct |
22 |
Correct |
11 ms |
14464 KB |
Output is correct |
23 |
Correct |
11 ms |
14464 KB |
Output is correct |
24 |
Correct |
13 ms |
14464 KB |
Output is correct |
25 |
Correct |
12 ms |
14464 KB |
Output is correct |
26 |
Correct |
10 ms |
14464 KB |
Output is correct |
27 |
Correct |
10 ms |
14464 KB |
Output is correct |
28 |
Correct |
14 ms |
14464 KB |
Output is correct |
29 |
Correct |
10 ms |
14464 KB |
Output is correct |
30 |
Correct |
11 ms |
14464 KB |
Output is correct |
31 |
Correct |
15 ms |
14464 KB |
Output is correct |
32 |
Correct |
12 ms |
14464 KB |
Output is correct |
33 |
Correct |
13 ms |
14464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14464 KB |
Output is correct |
2 |
Correct |
11 ms |
14464 KB |
Output is correct |
3 |
Correct |
12 ms |
14396 KB |
Output is correct |
4 |
Correct |
10 ms |
14464 KB |
Output is correct |
5 |
Correct |
11 ms |
14476 KB |
Output is correct |
6 |
Correct |
10 ms |
14464 KB |
Output is correct |
7 |
Correct |
11 ms |
14464 KB |
Output is correct |
8 |
Correct |
14 ms |
14464 KB |
Output is correct |
9 |
Correct |
12 ms |
14464 KB |
Output is correct |
10 |
Correct |
12 ms |
14592 KB |
Output is correct |
11 |
Correct |
11 ms |
14464 KB |
Output is correct |
12 |
Correct |
10 ms |
14464 KB |
Output is correct |
13 |
Correct |
12 ms |
14464 KB |
Output is correct |
14 |
Correct |
14 ms |
14464 KB |
Output is correct |
15 |
Correct |
10 ms |
14464 KB |
Output is correct |
16 |
Correct |
13 ms |
14464 KB |
Output is correct |
17 |
Correct |
10 ms |
14464 KB |
Output is correct |
18 |
Correct |
12 ms |
14504 KB |
Output is correct |
19 |
Correct |
10 ms |
14464 KB |
Output is correct |
20 |
Correct |
13 ms |
14440 KB |
Output is correct |
21 |
Correct |
11 ms |
14464 KB |
Output is correct |
22 |
Correct |
11 ms |
14464 KB |
Output is correct |
23 |
Correct |
11 ms |
14464 KB |
Output is correct |
24 |
Correct |
13 ms |
14464 KB |
Output is correct |
25 |
Correct |
12 ms |
14464 KB |
Output is correct |
26 |
Correct |
10 ms |
14464 KB |
Output is correct |
27 |
Correct |
10 ms |
14464 KB |
Output is correct |
28 |
Correct |
14 ms |
14464 KB |
Output is correct |
29 |
Correct |
10 ms |
14464 KB |
Output is correct |
30 |
Correct |
11 ms |
14464 KB |
Output is correct |
31 |
Correct |
15 ms |
14464 KB |
Output is correct |
32 |
Correct |
12 ms |
14464 KB |
Output is correct |
33 |
Correct |
13 ms |
14464 KB |
Output is correct |
34 |
Correct |
15 ms |
14720 KB |
Output is correct |
35 |
Correct |
157 ms |
22572 KB |
Output is correct |
36 |
Correct |
211 ms |
24480 KB |
Output is correct |
37 |
Correct |
209 ms |
24564 KB |
Output is correct |
38 |
Correct |
198 ms |
24308 KB |
Output is correct |
39 |
Correct |
14 ms |
14720 KB |
Output is correct |
40 |
Correct |
42 ms |
14968 KB |
Output is correct |
41 |
Correct |
24 ms |
14976 KB |
Output is correct |
42 |
Correct |
26 ms |
14720 KB |
Output is correct |
43 |
Correct |
16 ms |
14848 KB |
Output is correct |
44 |
Correct |
31 ms |
14840 KB |
Output is correct |
45 |
Correct |
16 ms |
14720 KB |
Output is correct |
46 |
Correct |
12 ms |
14720 KB |
Output is correct |
47 |
Correct |
26 ms |
14952 KB |
Output is correct |
48 |
Correct |
25 ms |
14848 KB |
Output is correct |
49 |
Correct |
64 ms |
15480 KB |
Output is correct |
50 |
Correct |
236 ms |
24624 KB |
Output is correct |
51 |
Correct |
23 ms |
15104 KB |
Output is correct |
52 |
Correct |
181 ms |
23292 KB |
Output is correct |
53 |
Correct |
39 ms |
15480 KB |
Output is correct |
54 |
Correct |
196 ms |
24308 KB |
Output is correct |
55 |
Correct |
25 ms |
15104 KB |
Output is correct |
56 |
Correct |
27 ms |
15104 KB |
Output is correct |
57 |
Correct |
22 ms |
15104 KB |
Output is correct |
58 |
Correct |
22 ms |
15104 KB |
Output is correct |
59 |
Correct |
30 ms |
15096 KB |
Output is correct |
60 |
Correct |
176 ms |
23036 KB |
Output is correct |
61 |
Correct |
33 ms |
14968 KB |
Output is correct |
62 |
Correct |
202 ms |
25052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14464 KB |
Output is correct |
2 |
Correct |
11 ms |
14464 KB |
Output is correct |
3 |
Correct |
12 ms |
14396 KB |
Output is correct |
4 |
Correct |
10 ms |
14464 KB |
Output is correct |
5 |
Correct |
11 ms |
14476 KB |
Output is correct |
6 |
Correct |
10 ms |
14464 KB |
Output is correct |
7 |
Correct |
11 ms |
14464 KB |
Output is correct |
8 |
Correct |
14 ms |
14464 KB |
Output is correct |
9 |
Correct |
12 ms |
14464 KB |
Output is correct |
10 |
Correct |
12 ms |
14592 KB |
Output is correct |
11 |
Correct |
11 ms |
14464 KB |
Output is correct |
12 |
Correct |
10 ms |
14464 KB |
Output is correct |
13 |
Correct |
12 ms |
14464 KB |
Output is correct |
14 |
Correct |
14 ms |
14464 KB |
Output is correct |
15 |
Correct |
10 ms |
14464 KB |
Output is correct |
16 |
Correct |
13 ms |
14464 KB |
Output is correct |
17 |
Correct |
10 ms |
14464 KB |
Output is correct |
18 |
Correct |
12 ms |
14504 KB |
Output is correct |
19 |
Correct |
10 ms |
14464 KB |
Output is correct |
20 |
Correct |
13 ms |
14440 KB |
Output is correct |
21 |
Correct |
11 ms |
14464 KB |
Output is correct |
22 |
Correct |
11 ms |
14464 KB |
Output is correct |
23 |
Correct |
11 ms |
14464 KB |
Output is correct |
24 |
Correct |
13 ms |
14464 KB |
Output is correct |
25 |
Correct |
12 ms |
14464 KB |
Output is correct |
26 |
Correct |
10 ms |
14464 KB |
Output is correct |
27 |
Correct |
10 ms |
14464 KB |
Output is correct |
28 |
Correct |
14 ms |
14464 KB |
Output is correct |
29 |
Correct |
10 ms |
14464 KB |
Output is correct |
30 |
Correct |
11 ms |
14464 KB |
Output is correct |
31 |
Correct |
15 ms |
14464 KB |
Output is correct |
32 |
Correct |
12 ms |
14464 KB |
Output is correct |
33 |
Correct |
13 ms |
14464 KB |
Output is correct |
34 |
Correct |
15 ms |
14720 KB |
Output is correct |
35 |
Correct |
157 ms |
22572 KB |
Output is correct |
36 |
Correct |
211 ms |
24480 KB |
Output is correct |
37 |
Correct |
209 ms |
24564 KB |
Output is correct |
38 |
Correct |
198 ms |
24308 KB |
Output is correct |
39 |
Correct |
14 ms |
14720 KB |
Output is correct |
40 |
Correct |
42 ms |
14968 KB |
Output is correct |
41 |
Correct |
24 ms |
14976 KB |
Output is correct |
42 |
Correct |
26 ms |
14720 KB |
Output is correct |
43 |
Correct |
16 ms |
14848 KB |
Output is correct |
44 |
Correct |
31 ms |
14840 KB |
Output is correct |
45 |
Correct |
16 ms |
14720 KB |
Output is correct |
46 |
Correct |
12 ms |
14720 KB |
Output is correct |
47 |
Correct |
26 ms |
14952 KB |
Output is correct |
48 |
Correct |
25 ms |
14848 KB |
Output is correct |
49 |
Correct |
64 ms |
15480 KB |
Output is correct |
50 |
Correct |
236 ms |
24624 KB |
Output is correct |
51 |
Correct |
23 ms |
15104 KB |
Output is correct |
52 |
Correct |
181 ms |
23292 KB |
Output is correct |
53 |
Correct |
39 ms |
15480 KB |
Output is correct |
54 |
Correct |
196 ms |
24308 KB |
Output is correct |
55 |
Correct |
25 ms |
15104 KB |
Output is correct |
56 |
Correct |
27 ms |
15104 KB |
Output is correct |
57 |
Correct |
22 ms |
15104 KB |
Output is correct |
58 |
Correct |
22 ms |
15104 KB |
Output is correct |
59 |
Correct |
30 ms |
15096 KB |
Output is correct |
60 |
Correct |
176 ms |
23036 KB |
Output is correct |
61 |
Correct |
33 ms |
14968 KB |
Output is correct |
62 |
Correct |
202 ms |
25052 KB |
Output is correct |
63 |
Correct |
1294 ms |
60908 KB |
Output is correct |
64 |
Correct |
1509 ms |
60512 KB |
Output is correct |
65 |
Correct |
1235 ms |
60528 KB |
Output is correct |
66 |
Correct |
244 ms |
30180 KB |
Output is correct |
67 |
Incorrect |
1774 ms |
48992 KB |
Output isn't correct |
68 |
Halted |
0 ms |
0 KB |
- |