#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;
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;
for(auto &i : inc[y]){
if(sa.count(a[i])){
dap -= sz(memb[pa[a[i].second]]);
sa.erase(a[i]);
}
}
for(auto &i : inc[x]){
if(sa.count(a[i])){
dap -= sz(memb[pa[a[i].second]]);
sa.erase(a[i]);
}
}
for(auto &i : memb[y]){
memb[x].push_back(i);
pa[i] = x;
}
for(auto &i : inc[x]){
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])){
dap += sz(memb[pa[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]);
}
for(auto &i : inc[y]){
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])){
dap += sz(memb[pa[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);
}
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]]);
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:71:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
71 | scanf("%d %d",&n,&m);
| ~~~~~^~~~~~~~~~~~~~~
joitter2.cpp:77:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
77 | int x, y; scanf("%d %d",&x,&y);
| ~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14464 KB |
Output is correct |
2 |
Correct |
12 ms |
14464 KB |
Output is correct |
3 |
Correct |
11 ms |
14512 KB |
Output is correct |
4 |
Correct |
11 ms |
14464 KB |
Output is correct |
5 |
Correct |
11 ms |
14464 KB |
Output is correct |
6 |
Correct |
9 ms |
14464 KB |
Output is correct |
7 |
Correct |
12 ms |
14464 KB |
Output is correct |
8 |
Correct |
13 ms |
14464 KB |
Output is correct |
9 |
Correct |
13 ms |
14464 KB |
Output is correct |
10 |
Correct |
10 ms |
14464 KB |
Output is correct |
11 |
Correct |
10 ms |
14464 KB |
Output is correct |
12 |
Correct |
13 ms |
14464 KB |
Output is correct |
13 |
Correct |
10 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 |
14 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 |
10 ms |
14464 KB |
Output is correct |
20 |
Correct |
11 ms |
14464 KB |
Output is correct |
21 |
Correct |
14 ms |
14464 KB |
Output is correct |
22 |
Correct |
16 ms |
14464 KB |
Output is correct |
23 |
Correct |
12 ms |
14464 KB |
Output is correct |
24 |
Correct |
14 ms |
14464 KB |
Output is correct |
25 |
Correct |
13 ms |
14464 KB |
Output is correct |
26 |
Correct |
13 ms |
14464 KB |
Output is correct |
27 |
Correct |
15 ms |
14464 KB |
Output is correct |
28 |
Correct |
13 ms |
14464 KB |
Output is correct |
29 |
Correct |
11 ms |
14464 KB |
Output is correct |
30 |
Correct |
11 ms |
14464 KB |
Output is correct |
31 |
Correct |
10 ms |
14464 KB |
Output is correct |
32 |
Correct |
11 ms |
14464 KB |
Output is correct |
33 |
Correct |
15 ms |
14464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14464 KB |
Output is correct |
2 |
Correct |
12 ms |
14464 KB |
Output is correct |
3 |
Correct |
11 ms |
14512 KB |
Output is correct |
4 |
Correct |
11 ms |
14464 KB |
Output is correct |
5 |
Correct |
11 ms |
14464 KB |
Output is correct |
6 |
Correct |
9 ms |
14464 KB |
Output is correct |
7 |
Correct |
12 ms |
14464 KB |
Output is correct |
8 |
Correct |
13 ms |
14464 KB |
Output is correct |
9 |
Correct |
13 ms |
14464 KB |
Output is correct |
10 |
Correct |
10 ms |
14464 KB |
Output is correct |
11 |
Correct |
10 ms |
14464 KB |
Output is correct |
12 |
Correct |
13 ms |
14464 KB |
Output is correct |
13 |
Correct |
10 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 |
14 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 |
10 ms |
14464 KB |
Output is correct |
20 |
Correct |
11 ms |
14464 KB |
Output is correct |
21 |
Correct |
14 ms |
14464 KB |
Output is correct |
22 |
Correct |
16 ms |
14464 KB |
Output is correct |
23 |
Correct |
12 ms |
14464 KB |
Output is correct |
24 |
Correct |
14 ms |
14464 KB |
Output is correct |
25 |
Correct |
13 ms |
14464 KB |
Output is correct |
26 |
Correct |
13 ms |
14464 KB |
Output is correct |
27 |
Correct |
15 ms |
14464 KB |
Output is correct |
28 |
Correct |
13 ms |
14464 KB |
Output is correct |
29 |
Correct |
11 ms |
14464 KB |
Output is correct |
30 |
Correct |
11 ms |
14464 KB |
Output is correct |
31 |
Correct |
10 ms |
14464 KB |
Output is correct |
32 |
Correct |
11 ms |
14464 KB |
Output is correct |
33 |
Correct |
15 ms |
14464 KB |
Output is correct |
34 |
Correct |
32 ms |
14812 KB |
Output is correct |
35 |
Correct |
4792 ms |
74884 KB |
Output is correct |
36 |
Execution timed out |
5031 ms |
51112 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14464 KB |
Output is correct |
2 |
Correct |
12 ms |
14464 KB |
Output is correct |
3 |
Correct |
11 ms |
14512 KB |
Output is correct |
4 |
Correct |
11 ms |
14464 KB |
Output is correct |
5 |
Correct |
11 ms |
14464 KB |
Output is correct |
6 |
Correct |
9 ms |
14464 KB |
Output is correct |
7 |
Correct |
12 ms |
14464 KB |
Output is correct |
8 |
Correct |
13 ms |
14464 KB |
Output is correct |
9 |
Correct |
13 ms |
14464 KB |
Output is correct |
10 |
Correct |
10 ms |
14464 KB |
Output is correct |
11 |
Correct |
10 ms |
14464 KB |
Output is correct |
12 |
Correct |
13 ms |
14464 KB |
Output is correct |
13 |
Correct |
10 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 |
14 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 |
10 ms |
14464 KB |
Output is correct |
20 |
Correct |
11 ms |
14464 KB |
Output is correct |
21 |
Correct |
14 ms |
14464 KB |
Output is correct |
22 |
Correct |
16 ms |
14464 KB |
Output is correct |
23 |
Correct |
12 ms |
14464 KB |
Output is correct |
24 |
Correct |
14 ms |
14464 KB |
Output is correct |
25 |
Correct |
13 ms |
14464 KB |
Output is correct |
26 |
Correct |
13 ms |
14464 KB |
Output is correct |
27 |
Correct |
15 ms |
14464 KB |
Output is correct |
28 |
Correct |
13 ms |
14464 KB |
Output is correct |
29 |
Correct |
11 ms |
14464 KB |
Output is correct |
30 |
Correct |
11 ms |
14464 KB |
Output is correct |
31 |
Correct |
10 ms |
14464 KB |
Output is correct |
32 |
Correct |
11 ms |
14464 KB |
Output is correct |
33 |
Correct |
15 ms |
14464 KB |
Output is correct |
34 |
Correct |
32 ms |
14812 KB |
Output is correct |
35 |
Correct |
4792 ms |
74884 KB |
Output is correct |
36 |
Execution timed out |
5031 ms |
51112 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |