#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];
lint tot;
vector<int> memb[MAXN], inc[MAXN];
int f(int x){ return sz(memb[x]) + sz(inc[x]); }
queue<pi> que;
void uni(int x, int y){
x = pa[x]; y = pa[y];
if(x == y) return;
if(f(x) < f(y)) swap(x, y);
tot += sz(memb[x]) * sz(memb[y]) * 2;
for(auto &i : memb[y]){
memb[x].push_back(i);
pa[i] = x;
}
for(auto &i : inc[y]){
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]);
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]);
inc[pa[x]].push_back(i);
inc[pa[y]].push_back(i);
sa.emplace(x, pa[y]);
sb.emplace(pa[x], pa[y]);
if(sb.find(pi(pa[y], pa[x])) != sb.end()){
que.emplace(x, y);
}
while(sz(que)){
int x, y;
tie(x, y) = que.front(); que.pop();
uni(x, y);
}
lint ret = tot;
for(auto &[u, v] : sa){
if(pa[u] != pa[v]) ret += sz(memb[v]);
}
printf("%lld\n", ret);
}
}
Compilation message
joitter2.cpp: In function 'int main()':
joitter2.cpp:46:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
46 | scanf("%d %d",&n,&m);
| ~~~~~^~~~~~~~~~~~~~~
joitter2.cpp:52:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
52 | int x, y; scanf("%d %d",&x,&y);
| ~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
11 ms |
14464 KB |
Output is correct |
5 |
Correct |
10 ms |
14464 KB |
Output is correct |
6 |
Correct |
14 ms |
14464 KB |
Output is correct |
7 |
Correct |
14 ms |
14604 KB |
Output is correct |
8 |
Correct |
15 ms |
14592 KB |
Output is correct |
9 |
Correct |
12 ms |
14592 KB |
Output is correct |
10 |
Correct |
12 ms |
14464 KB |
Output is correct |
11 |
Correct |
11 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 |
12 ms |
14464 KB |
Output is correct |
15 |
Correct |
12 ms |
14464 KB |
Output is correct |
16 |
Correct |
10 ms |
14592 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 |
14592 KB |
Output is correct |
22 |
Correct |
11 ms |
14464 KB |
Output is correct |
23 |
Correct |
14 ms |
14464 KB |
Output is correct |
24 |
Correct |
11 ms |
14464 KB |
Output is correct |
25 |
Correct |
13 ms |
14496 KB |
Output is correct |
26 |
Correct |
10 ms |
14592 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 |
12 ms |
14464 KB |
Output is correct |
31 |
Correct |
12 ms |
14592 KB |
Output is correct |
32 |
Correct |
12 ms |
14464 KB |
Output is correct |
33 |
Correct |
12 ms |
14592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
11 ms |
14464 KB |
Output is correct |
5 |
Correct |
10 ms |
14464 KB |
Output is correct |
6 |
Correct |
14 ms |
14464 KB |
Output is correct |
7 |
Correct |
14 ms |
14604 KB |
Output is correct |
8 |
Correct |
15 ms |
14592 KB |
Output is correct |
9 |
Correct |
12 ms |
14592 KB |
Output is correct |
10 |
Correct |
12 ms |
14464 KB |
Output is correct |
11 |
Correct |
11 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 |
12 ms |
14464 KB |
Output is correct |
15 |
Correct |
12 ms |
14464 KB |
Output is correct |
16 |
Correct |
10 ms |
14592 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 |
14592 KB |
Output is correct |
22 |
Correct |
11 ms |
14464 KB |
Output is correct |
23 |
Correct |
14 ms |
14464 KB |
Output is correct |
24 |
Correct |
11 ms |
14464 KB |
Output is correct |
25 |
Correct |
13 ms |
14496 KB |
Output is correct |
26 |
Correct |
10 ms |
14592 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 |
12 ms |
14464 KB |
Output is correct |
31 |
Correct |
12 ms |
14592 KB |
Output is correct |
32 |
Correct |
12 ms |
14464 KB |
Output is correct |
33 |
Correct |
12 ms |
14592 KB |
Output is correct |
34 |
Correct |
27 ms |
14840 KB |
Output is correct |
35 |
Execution timed out |
5040 ms |
25988 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
11 ms |
14464 KB |
Output is correct |
5 |
Correct |
10 ms |
14464 KB |
Output is correct |
6 |
Correct |
14 ms |
14464 KB |
Output is correct |
7 |
Correct |
14 ms |
14604 KB |
Output is correct |
8 |
Correct |
15 ms |
14592 KB |
Output is correct |
9 |
Correct |
12 ms |
14592 KB |
Output is correct |
10 |
Correct |
12 ms |
14464 KB |
Output is correct |
11 |
Correct |
11 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 |
12 ms |
14464 KB |
Output is correct |
15 |
Correct |
12 ms |
14464 KB |
Output is correct |
16 |
Correct |
10 ms |
14592 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 |
14592 KB |
Output is correct |
22 |
Correct |
11 ms |
14464 KB |
Output is correct |
23 |
Correct |
14 ms |
14464 KB |
Output is correct |
24 |
Correct |
11 ms |
14464 KB |
Output is correct |
25 |
Correct |
13 ms |
14496 KB |
Output is correct |
26 |
Correct |
10 ms |
14592 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 |
12 ms |
14464 KB |
Output is correct |
31 |
Correct |
12 ms |
14592 KB |
Output is correct |
32 |
Correct |
12 ms |
14464 KB |
Output is correct |
33 |
Correct |
12 ms |
14592 KB |
Output is correct |
34 |
Correct |
27 ms |
14840 KB |
Output is correct |
35 |
Execution timed out |
5040 ms |
25988 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |