#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);
| ~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
14464 KB |
Output is correct |
2 |
Correct |
10 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 |
10 ms |
14464 KB |
Output is correct |
6 |
Correct |
10 ms |
14464 KB |
Output is correct |
7 |
Correct |
11 ms |
14464 KB |
Output is correct |
8 |
Correct |
11 ms |
14464 KB |
Output is correct |
9 |
Correct |
12 ms |
14464 KB |
Output is correct |
10 |
Correct |
11 ms |
14464 KB |
Output is correct |
11 |
Correct |
11 ms |
14464 KB |
Output is correct |
12 |
Correct |
11 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 |
16 ms |
14464 KB |
Output is correct |
16 |
Correct |
11 ms |
14464 KB |
Output is correct |
17 |
Correct |
12 ms |
14464 KB |
Output is correct |
18 |
Correct |
13 ms |
14464 KB |
Output is correct |
19 |
Correct |
12 ms |
14464 KB |
Output is correct |
20 |
Correct |
12 ms |
14464 KB |
Output is correct |
21 |
Correct |
13 ms |
14464 KB |
Output is correct |
22 |
Correct |
13 ms |
14464 KB |
Output is correct |
23 |
Correct |
13 ms |
14464 KB |
Output is correct |
24 |
Correct |
11 ms |
14464 KB |
Output is correct |
25 |
Correct |
11 ms |
14464 KB |
Output is correct |
26 |
Correct |
12 ms |
14464 KB |
Output is correct |
27 |
Correct |
10 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 |
11 ms |
14464 KB |
Output is correct |
31 |
Correct |
13 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 |
11 ms |
14464 KB |
Output is correct |
2 |
Correct |
10 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 |
10 ms |
14464 KB |
Output is correct |
6 |
Correct |
10 ms |
14464 KB |
Output is correct |
7 |
Correct |
11 ms |
14464 KB |
Output is correct |
8 |
Correct |
11 ms |
14464 KB |
Output is correct |
9 |
Correct |
12 ms |
14464 KB |
Output is correct |
10 |
Correct |
11 ms |
14464 KB |
Output is correct |
11 |
Correct |
11 ms |
14464 KB |
Output is correct |
12 |
Correct |
11 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 |
16 ms |
14464 KB |
Output is correct |
16 |
Correct |
11 ms |
14464 KB |
Output is correct |
17 |
Correct |
12 ms |
14464 KB |
Output is correct |
18 |
Correct |
13 ms |
14464 KB |
Output is correct |
19 |
Correct |
12 ms |
14464 KB |
Output is correct |
20 |
Correct |
12 ms |
14464 KB |
Output is correct |
21 |
Correct |
13 ms |
14464 KB |
Output is correct |
22 |
Correct |
13 ms |
14464 KB |
Output is correct |
23 |
Correct |
13 ms |
14464 KB |
Output is correct |
24 |
Correct |
11 ms |
14464 KB |
Output is correct |
25 |
Correct |
11 ms |
14464 KB |
Output is correct |
26 |
Correct |
12 ms |
14464 KB |
Output is correct |
27 |
Correct |
10 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 |
11 ms |
14464 KB |
Output is correct |
31 |
Correct |
13 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 |
162 ms |
22524 KB |
Output is correct |
36 |
Correct |
231 ms |
24564 KB |
Output is correct |
37 |
Correct |
220 ms |
24688 KB |
Output is correct |
38 |
Correct |
214 ms |
24312 KB |
Output is correct |
39 |
Correct |
13 ms |
14720 KB |
Output is correct |
40 |
Correct |
28 ms |
14968 KB |
Output is correct |
41 |
Correct |
29 ms |
14976 KB |
Output is correct |
42 |
Correct |
14 ms |
14720 KB |
Output is correct |
43 |
Correct |
16 ms |
14848 KB |
Output is correct |
44 |
Correct |
20 ms |
14848 KB |
Output is correct |
45 |
Correct |
13 ms |
14720 KB |
Output is correct |
46 |
Correct |
16 ms |
14720 KB |
Output is correct |
47 |
Correct |
22 ms |
14840 KB |
Output is correct |
48 |
Correct |
22 ms |
14848 KB |
Output is correct |
49 |
Correct |
39 ms |
15480 KB |
Output is correct |
50 |
Correct |
238 ms |
24564 KB |
Output is correct |
51 |
Correct |
24 ms |
15104 KB |
Output is correct |
52 |
Correct |
185 ms |
23248 KB |
Output is correct |
53 |
Correct |
41 ms |
15480 KB |
Output is correct |
54 |
Correct |
177 ms |
23924 KB |
Output is correct |
55 |
Correct |
25 ms |
15104 KB |
Output is correct |
56 |
Correct |
22 ms |
15104 KB |
Output is correct |
57 |
Correct |
22 ms |
15104 KB |
Output is correct |
58 |
Correct |
23 ms |
15104 KB |
Output is correct |
59 |
Correct |
21 ms |
14976 KB |
Output is correct |
60 |
Correct |
139 ms |
22028 KB |
Output is correct |
61 |
Correct |
22 ms |
14976 KB |
Output is correct |
62 |
Correct |
198 ms |
24112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
14464 KB |
Output is correct |
2 |
Correct |
10 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 |
10 ms |
14464 KB |
Output is correct |
6 |
Correct |
10 ms |
14464 KB |
Output is correct |
7 |
Correct |
11 ms |
14464 KB |
Output is correct |
8 |
Correct |
11 ms |
14464 KB |
Output is correct |
9 |
Correct |
12 ms |
14464 KB |
Output is correct |
10 |
Correct |
11 ms |
14464 KB |
Output is correct |
11 |
Correct |
11 ms |
14464 KB |
Output is correct |
12 |
Correct |
11 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 |
16 ms |
14464 KB |
Output is correct |
16 |
Correct |
11 ms |
14464 KB |
Output is correct |
17 |
Correct |
12 ms |
14464 KB |
Output is correct |
18 |
Correct |
13 ms |
14464 KB |
Output is correct |
19 |
Correct |
12 ms |
14464 KB |
Output is correct |
20 |
Correct |
12 ms |
14464 KB |
Output is correct |
21 |
Correct |
13 ms |
14464 KB |
Output is correct |
22 |
Correct |
13 ms |
14464 KB |
Output is correct |
23 |
Correct |
13 ms |
14464 KB |
Output is correct |
24 |
Correct |
11 ms |
14464 KB |
Output is correct |
25 |
Correct |
11 ms |
14464 KB |
Output is correct |
26 |
Correct |
12 ms |
14464 KB |
Output is correct |
27 |
Correct |
10 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 |
11 ms |
14464 KB |
Output is correct |
31 |
Correct |
13 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 |
162 ms |
22524 KB |
Output is correct |
36 |
Correct |
231 ms |
24564 KB |
Output is correct |
37 |
Correct |
220 ms |
24688 KB |
Output is correct |
38 |
Correct |
214 ms |
24312 KB |
Output is correct |
39 |
Correct |
13 ms |
14720 KB |
Output is correct |
40 |
Correct |
28 ms |
14968 KB |
Output is correct |
41 |
Correct |
29 ms |
14976 KB |
Output is correct |
42 |
Correct |
14 ms |
14720 KB |
Output is correct |
43 |
Correct |
16 ms |
14848 KB |
Output is correct |
44 |
Correct |
20 ms |
14848 KB |
Output is correct |
45 |
Correct |
13 ms |
14720 KB |
Output is correct |
46 |
Correct |
16 ms |
14720 KB |
Output is correct |
47 |
Correct |
22 ms |
14840 KB |
Output is correct |
48 |
Correct |
22 ms |
14848 KB |
Output is correct |
49 |
Correct |
39 ms |
15480 KB |
Output is correct |
50 |
Correct |
238 ms |
24564 KB |
Output is correct |
51 |
Correct |
24 ms |
15104 KB |
Output is correct |
52 |
Correct |
185 ms |
23248 KB |
Output is correct |
53 |
Correct |
41 ms |
15480 KB |
Output is correct |
54 |
Correct |
177 ms |
23924 KB |
Output is correct |
55 |
Correct |
25 ms |
15104 KB |
Output is correct |
56 |
Correct |
22 ms |
15104 KB |
Output is correct |
57 |
Correct |
22 ms |
15104 KB |
Output is correct |
58 |
Correct |
23 ms |
15104 KB |
Output is correct |
59 |
Correct |
21 ms |
14976 KB |
Output is correct |
60 |
Correct |
139 ms |
22028 KB |
Output is correct |
61 |
Correct |
22 ms |
14976 KB |
Output is correct |
62 |
Correct |
198 ms |
24112 KB |
Output is correct |
63 |
Correct |
1105 ms |
59600 KB |
Output is correct |
64 |
Correct |
1080 ms |
59612 KB |
Output is correct |
65 |
Correct |
1141 ms |
59692 KB |
Output is correct |
66 |
Correct |
230 ms |
29288 KB |
Output is correct |
67 |
Correct |
1932 ms |
48088 KB |
Output is correct |
68 |
Correct |
249 ms |
29220 KB |
Output is correct |
69 |
Correct |
765 ms |
33492 KB |
Output is correct |
70 |
Correct |
267 ms |
29220 KB |
Output is correct |
71 |
Correct |
270 ms |
29124 KB |
Output is correct |
72 |
Correct |
1615 ms |
37808 KB |
Output is correct |
73 |
Correct |
1597 ms |
39276 KB |
Output is correct |
74 |
Correct |
3227 ms |
53244 KB |
Output is correct |
75 |
Correct |
1157 ms |
39072 KB |
Output is correct |
76 |
Correct |
2124 ms |
45464 KB |
Output is correct |
77 |
Correct |
2256 ms |
45672 KB |
Output is correct |
78 |
Correct |
482 ms |
31652 KB |
Output is correct |
79 |
Correct |
763 ms |
36592 KB |
Output is correct |
80 |
Correct |
386 ms |
31684 KB |
Output is correct |
81 |
Correct |
776 ms |
36620 KB |
Output is correct |
82 |
Correct |
1768 ms |
48328 KB |
Output is correct |
83 |
Correct |
2243 ms |
48312 KB |
Output is correct |
84 |
Correct |
1727 ms |
48048 KB |
Output is correct |
85 |
Correct |
1421 ms |
48140 KB |
Output is correct |
86 |
Correct |
1215 ms |
50460 KB |
Output is correct |
87 |
Correct |
1186 ms |
52744 KB |
Output is correct |
88 |
Correct |
1655 ms |
38276 KB |
Output is correct |
89 |
Correct |
2029 ms |
44688 KB |
Output is correct |