#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 += 2ll * sz(memb[x]) * sz(memb[y]);
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);
| ~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
14464 KB |
Output is correct |
2 |
Correct |
11 ms |
14464 KB |
Output is correct |
3 |
Correct |
11 ms |
14464 KB |
Output is correct |
4 |
Correct |
10 ms |
14464 KB |
Output is correct |
5 |
Correct |
11 ms |
14464 KB |
Output is correct |
6 |
Correct |
11 ms |
14464 KB |
Output is correct |
7 |
Correct |
12 ms |
14464 KB |
Output is correct |
8 |
Correct |
12 ms |
14464 KB |
Output is correct |
9 |
Correct |
11 ms |
14464 KB |
Output is correct |
10 |
Correct |
11 ms |
14464 KB |
Output is correct |
11 |
Correct |
10 ms |
14464 KB |
Output is correct |
12 |
Correct |
10 ms |
14464 KB |
Output is correct |
13 |
Correct |
10 ms |
14464 KB |
Output is correct |
14 |
Correct |
10 ms |
14464 KB |
Output is correct |
15 |
Correct |
10 ms |
14464 KB |
Output is correct |
16 |
Correct |
10 ms |
14464 KB |
Output is correct |
17 |
Correct |
10 ms |
14464 KB |
Output is correct |
18 |
Correct |
10 ms |
14464 KB |
Output is correct |
19 |
Correct |
11 ms |
14464 KB |
Output is correct |
20 |
Correct |
13 ms |
14464 KB |
Output is correct |
21 |
Correct |
13 ms |
14464 KB |
Output is correct |
22 |
Correct |
11 ms |
14464 KB |
Output is correct |
23 |
Correct |
12 ms |
14464 KB |
Output is correct |
24 |
Correct |
10 ms |
14464 KB |
Output is correct |
25 |
Correct |
13 ms |
14532 KB |
Output is correct |
26 |
Correct |
11 ms |
14464 KB |
Output is correct |
27 |
Correct |
11 ms |
14464 KB |
Output is correct |
28 |
Correct |
12 ms |
14464 KB |
Output is correct |
29 |
Correct |
10 ms |
14464 KB |
Output is correct |
30 |
Correct |
10 ms |
14464 KB |
Output is correct |
31 |
Correct |
14 ms |
14464 KB |
Output is correct |
32 |
Correct |
11 ms |
14464 KB |
Output is correct |
33 |
Correct |
11 ms |
14464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
14464 KB |
Output is correct |
2 |
Correct |
11 ms |
14464 KB |
Output is correct |
3 |
Correct |
11 ms |
14464 KB |
Output is correct |
4 |
Correct |
10 ms |
14464 KB |
Output is correct |
5 |
Correct |
11 ms |
14464 KB |
Output is correct |
6 |
Correct |
11 ms |
14464 KB |
Output is correct |
7 |
Correct |
12 ms |
14464 KB |
Output is correct |
8 |
Correct |
12 ms |
14464 KB |
Output is correct |
9 |
Correct |
11 ms |
14464 KB |
Output is correct |
10 |
Correct |
11 ms |
14464 KB |
Output is correct |
11 |
Correct |
10 ms |
14464 KB |
Output is correct |
12 |
Correct |
10 ms |
14464 KB |
Output is correct |
13 |
Correct |
10 ms |
14464 KB |
Output is correct |
14 |
Correct |
10 ms |
14464 KB |
Output is correct |
15 |
Correct |
10 ms |
14464 KB |
Output is correct |
16 |
Correct |
10 ms |
14464 KB |
Output is correct |
17 |
Correct |
10 ms |
14464 KB |
Output is correct |
18 |
Correct |
10 ms |
14464 KB |
Output is correct |
19 |
Correct |
11 ms |
14464 KB |
Output is correct |
20 |
Correct |
13 ms |
14464 KB |
Output is correct |
21 |
Correct |
13 ms |
14464 KB |
Output is correct |
22 |
Correct |
11 ms |
14464 KB |
Output is correct |
23 |
Correct |
12 ms |
14464 KB |
Output is correct |
24 |
Correct |
10 ms |
14464 KB |
Output is correct |
25 |
Correct |
13 ms |
14532 KB |
Output is correct |
26 |
Correct |
11 ms |
14464 KB |
Output is correct |
27 |
Correct |
11 ms |
14464 KB |
Output is correct |
28 |
Correct |
12 ms |
14464 KB |
Output is correct |
29 |
Correct |
10 ms |
14464 KB |
Output is correct |
30 |
Correct |
10 ms |
14464 KB |
Output is correct |
31 |
Correct |
14 ms |
14464 KB |
Output is correct |
32 |
Correct |
11 ms |
14464 KB |
Output is correct |
33 |
Correct |
11 ms |
14464 KB |
Output is correct |
34 |
Correct |
16 ms |
14720 KB |
Output is correct |
35 |
Correct |
182 ms |
23616 KB |
Output is correct |
36 |
Correct |
273 ms |
25480 KB |
Output is correct |
37 |
Correct |
302 ms |
25488 KB |
Output is correct |
38 |
Correct |
204 ms |
25200 KB |
Output is correct |
39 |
Correct |
14 ms |
14720 KB |
Output is correct |
40 |
Correct |
27 ms |
15080 KB |
Output is correct |
41 |
Correct |
24 ms |
15096 KB |
Output is correct |
42 |
Correct |
12 ms |
14720 KB |
Output is correct |
43 |
Correct |
22 ms |
14848 KB |
Output is correct |
44 |
Correct |
30 ms |
14848 KB |
Output is correct |
45 |
Correct |
16 ms |
14752 KB |
Output is correct |
46 |
Correct |
25 ms |
14824 KB |
Output is correct |
47 |
Correct |
38 ms |
14968 KB |
Output is correct |
48 |
Correct |
22 ms |
14976 KB |
Output is correct |
49 |
Correct |
43 ms |
15608 KB |
Output is correct |
50 |
Correct |
222 ms |
25696 KB |
Output is correct |
51 |
Correct |
25 ms |
15232 KB |
Output is correct |
52 |
Correct |
202 ms |
24312 KB |
Output is correct |
53 |
Correct |
39 ms |
15596 KB |
Output is correct |
54 |
Correct |
223 ms |
24304 KB |
Output is correct |
55 |
Correct |
25 ms |
15104 KB |
Output is correct |
56 |
Correct |
25 ms |
15080 KB |
Output is correct |
57 |
Correct |
25 ms |
15168 KB |
Output is correct |
58 |
Correct |
26 ms |
15104 KB |
Output is correct |
59 |
Correct |
27 ms |
14972 KB |
Output is correct |
60 |
Correct |
157 ms |
22068 KB |
Output is correct |
61 |
Correct |
24 ms |
14968 KB |
Output is correct |
62 |
Correct |
210 ms |
24124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
14464 KB |
Output is correct |
2 |
Correct |
11 ms |
14464 KB |
Output is correct |
3 |
Correct |
11 ms |
14464 KB |
Output is correct |
4 |
Correct |
10 ms |
14464 KB |
Output is correct |
5 |
Correct |
11 ms |
14464 KB |
Output is correct |
6 |
Correct |
11 ms |
14464 KB |
Output is correct |
7 |
Correct |
12 ms |
14464 KB |
Output is correct |
8 |
Correct |
12 ms |
14464 KB |
Output is correct |
9 |
Correct |
11 ms |
14464 KB |
Output is correct |
10 |
Correct |
11 ms |
14464 KB |
Output is correct |
11 |
Correct |
10 ms |
14464 KB |
Output is correct |
12 |
Correct |
10 ms |
14464 KB |
Output is correct |
13 |
Correct |
10 ms |
14464 KB |
Output is correct |
14 |
Correct |
10 ms |
14464 KB |
Output is correct |
15 |
Correct |
10 ms |
14464 KB |
Output is correct |
16 |
Correct |
10 ms |
14464 KB |
Output is correct |
17 |
Correct |
10 ms |
14464 KB |
Output is correct |
18 |
Correct |
10 ms |
14464 KB |
Output is correct |
19 |
Correct |
11 ms |
14464 KB |
Output is correct |
20 |
Correct |
13 ms |
14464 KB |
Output is correct |
21 |
Correct |
13 ms |
14464 KB |
Output is correct |
22 |
Correct |
11 ms |
14464 KB |
Output is correct |
23 |
Correct |
12 ms |
14464 KB |
Output is correct |
24 |
Correct |
10 ms |
14464 KB |
Output is correct |
25 |
Correct |
13 ms |
14532 KB |
Output is correct |
26 |
Correct |
11 ms |
14464 KB |
Output is correct |
27 |
Correct |
11 ms |
14464 KB |
Output is correct |
28 |
Correct |
12 ms |
14464 KB |
Output is correct |
29 |
Correct |
10 ms |
14464 KB |
Output is correct |
30 |
Correct |
10 ms |
14464 KB |
Output is correct |
31 |
Correct |
14 ms |
14464 KB |
Output is correct |
32 |
Correct |
11 ms |
14464 KB |
Output is correct |
33 |
Correct |
11 ms |
14464 KB |
Output is correct |
34 |
Correct |
16 ms |
14720 KB |
Output is correct |
35 |
Correct |
182 ms |
23616 KB |
Output is correct |
36 |
Correct |
273 ms |
25480 KB |
Output is correct |
37 |
Correct |
302 ms |
25488 KB |
Output is correct |
38 |
Correct |
204 ms |
25200 KB |
Output is correct |
39 |
Correct |
14 ms |
14720 KB |
Output is correct |
40 |
Correct |
27 ms |
15080 KB |
Output is correct |
41 |
Correct |
24 ms |
15096 KB |
Output is correct |
42 |
Correct |
12 ms |
14720 KB |
Output is correct |
43 |
Correct |
22 ms |
14848 KB |
Output is correct |
44 |
Correct |
30 ms |
14848 KB |
Output is correct |
45 |
Correct |
16 ms |
14752 KB |
Output is correct |
46 |
Correct |
25 ms |
14824 KB |
Output is correct |
47 |
Correct |
38 ms |
14968 KB |
Output is correct |
48 |
Correct |
22 ms |
14976 KB |
Output is correct |
49 |
Correct |
43 ms |
15608 KB |
Output is correct |
50 |
Correct |
222 ms |
25696 KB |
Output is correct |
51 |
Correct |
25 ms |
15232 KB |
Output is correct |
52 |
Correct |
202 ms |
24312 KB |
Output is correct |
53 |
Correct |
39 ms |
15596 KB |
Output is correct |
54 |
Correct |
223 ms |
24304 KB |
Output is correct |
55 |
Correct |
25 ms |
15104 KB |
Output is correct |
56 |
Correct |
25 ms |
15080 KB |
Output is correct |
57 |
Correct |
25 ms |
15168 KB |
Output is correct |
58 |
Correct |
26 ms |
15104 KB |
Output is correct |
59 |
Correct |
27 ms |
14972 KB |
Output is correct |
60 |
Correct |
157 ms |
22068 KB |
Output is correct |
61 |
Correct |
24 ms |
14968 KB |
Output is correct |
62 |
Correct |
210 ms |
24124 KB |
Output is correct |
63 |
Correct |
1614 ms |
59524 KB |
Output is correct |
64 |
Correct |
1485 ms |
59452 KB |
Output is correct |
65 |
Correct |
1209 ms |
59480 KB |
Output is correct |
66 |
Correct |
281 ms |
29196 KB |
Output is correct |
67 |
Correct |
1936 ms |
47972 KB |
Output is correct |
68 |
Correct |
261 ms |
30064 KB |
Output is correct |
69 |
Correct |
790 ms |
35084 KB |
Output is correct |
70 |
Correct |
233 ms |
31180 KB |
Output is correct |
71 |
Correct |
249 ms |
31076 KB |
Output is correct |
72 |
Correct |
1665 ms |
39748 KB |
Output is correct |
73 |
Correct |
1734 ms |
41224 KB |
Output is correct |
74 |
Correct |
4736 ms |
55728 KB |
Output is correct |
75 |
Correct |
1217 ms |
41584 KB |
Output is correct |
76 |
Correct |
2084 ms |
47940 KB |
Output is correct |
77 |
Correct |
2124 ms |
48016 KB |
Output is correct |
78 |
Correct |
394 ms |
33524 KB |
Output is correct |
79 |
Correct |
748 ms |
39168 KB |
Output is correct |
80 |
Correct |
416 ms |
33516 KB |
Output is correct |
81 |
Correct |
641 ms |
39220 KB |
Output is correct |
82 |
Correct |
1856 ms |
50324 KB |
Output is correct |
83 |
Correct |
1827 ms |
50340 KB |
Output is correct |
84 |
Correct |
1554 ms |
50052 KB |
Output is correct |
85 |
Correct |
1257 ms |
50096 KB |
Output is correct |
86 |
Correct |
1028 ms |
52556 KB |
Output is correct |
87 |
Correct |
1193 ms |
55184 KB |
Output is correct |
88 |
Correct |
1519 ms |
40316 KB |
Output is correct |
89 |
Correct |
2041 ms |
47356 KB |
Output is correct |