#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define int long long
using namespace std;
struct Dsu{
int n;
vector<int> pr;
vector<set<int>> st;
Dsu(int m){
n = m + 4;
pr.resize(n);
st.resize(n);
iota(pr.begin(), pr.end(), 0);
}
int fd(int x){
return x == pr[x] ? x : pr[x] = fd(pr[x]);
}
void unite(int x, int y){
x = fd(x), y = fd(y);
if(x == y) return;
if((int)st[x].size() < (int)st[y].size()){
swap(x, y);
}
while((int)st[y].size()){
st[x].insert(*st[y].begin());
st[y].erase(st[y].begin());
}
pr[y] = x;
}
};
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<vector<int>> r(n), l(n);
for(int i = 0; i < m; ++i){
int x, y;
cin >> x >> y;
--x, --y;
r[x].push_back(y);
l[y].push_back(x);
}
Dsu gr(n);
for(int i = 0; i < n; ++i){
for(auto&nxt:r[i]){
gr.st[i].insert(nxt);
}
}
int ans = 0;
for(int i = 0; i < n; ++i){
for(auto&nxt:l[i]){
gr.unite(nxt, i);
}
// cout << i << endl;
int rt = gr.fd(i);
while(gr.st[rt].size() && *gr.st[rt].begin() <= i){
gr.st[rt].erase(gr.st[rt].begin());
}
ans += (int)gr.st[rt].size();
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
716 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
716 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
41 ms |
7928 KB |
Output is correct |
17 |
Correct |
43 ms |
7772 KB |
Output is correct |
18 |
Correct |
35 ms |
7724 KB |
Output is correct |
19 |
Correct |
36 ms |
7828 KB |
Output is correct |
20 |
Correct |
47 ms |
7924 KB |
Output is correct |
21 |
Correct |
3 ms |
1236 KB |
Output is correct |
22 |
Correct |
50 ms |
7968 KB |
Output is correct |
23 |
Correct |
50 ms |
7744 KB |
Output is correct |
24 |
Correct |
38 ms |
7764 KB |
Output is correct |
25 |
Correct |
56 ms |
7896 KB |
Output is correct |
26 |
Correct |
38 ms |
7848 KB |
Output is correct |
27 |
Correct |
3 ms |
1236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
86 ms |
19636 KB |
Output is correct |
2 |
Correct |
84 ms |
20176 KB |
Output is correct |
3 |
Correct |
94 ms |
20320 KB |
Output is correct |
4 |
Correct |
82 ms |
19592 KB |
Output is correct |
5 |
Correct |
85 ms |
20196 KB |
Output is correct |
6 |
Correct |
67 ms |
20300 KB |
Output is correct |
7 |
Correct |
71 ms |
19668 KB |
Output is correct |
8 |
Correct |
94 ms |
20128 KB |
Output is correct |
9 |
Correct |
78 ms |
20336 KB |
Output is correct |
10 |
Correct |
64 ms |
19660 KB |
Output is correct |
11 |
Correct |
82 ms |
20188 KB |
Output is correct |
12 |
Correct |
70 ms |
20324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
716 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
41 ms |
7928 KB |
Output is correct |
17 |
Correct |
43 ms |
7772 KB |
Output is correct |
18 |
Correct |
35 ms |
7724 KB |
Output is correct |
19 |
Correct |
36 ms |
7828 KB |
Output is correct |
20 |
Correct |
47 ms |
7924 KB |
Output is correct |
21 |
Correct |
3 ms |
1236 KB |
Output is correct |
22 |
Correct |
50 ms |
7968 KB |
Output is correct |
23 |
Correct |
50 ms |
7744 KB |
Output is correct |
24 |
Correct |
38 ms |
7764 KB |
Output is correct |
25 |
Correct |
56 ms |
7896 KB |
Output is correct |
26 |
Correct |
38 ms |
7848 KB |
Output is correct |
27 |
Correct |
3 ms |
1236 KB |
Output is correct |
28 |
Correct |
86 ms |
19636 KB |
Output is correct |
29 |
Correct |
84 ms |
20176 KB |
Output is correct |
30 |
Correct |
94 ms |
20320 KB |
Output is correct |
31 |
Correct |
82 ms |
19592 KB |
Output is correct |
32 |
Correct |
85 ms |
20196 KB |
Output is correct |
33 |
Correct |
67 ms |
20300 KB |
Output is correct |
34 |
Correct |
71 ms |
19668 KB |
Output is correct |
35 |
Correct |
94 ms |
20128 KB |
Output is correct |
36 |
Correct |
78 ms |
20336 KB |
Output is correct |
37 |
Correct |
64 ms |
19660 KB |
Output is correct |
38 |
Correct |
82 ms |
20188 KB |
Output is correct |
39 |
Correct |
70 ms |
20324 KB |
Output is correct |
40 |
Correct |
60 ms |
8420 KB |
Output is correct |
41 |
Correct |
68 ms |
8416 KB |
Output is correct |
42 |
Correct |
86 ms |
12004 KB |
Output is correct |
43 |
Correct |
68 ms |
12016 KB |
Output is correct |
44 |
Correct |
68 ms |
18008 KB |
Output is correct |
45 |
Correct |
67 ms |
17948 KB |
Output is correct |
46 |
Correct |
49 ms |
13132 KB |
Output is correct |
47 |
Correct |
51 ms |
13032 KB |
Output is correct |
48 |
Correct |
61 ms |
18472 KB |
Output is correct |
49 |
Correct |
52 ms |
18344 KB |
Output is correct |
50 |
Correct |
75 ms |
19688 KB |
Output is correct |
51 |
Correct |
81 ms |
20184 KB |
Output is correct |
52 |
Correct |
80 ms |
20336 KB |
Output is correct |
53 |
Correct |
79 ms |
19152 KB |
Output is correct |
54 |
Correct |
72 ms |
19220 KB |
Output is correct |
55 |
Correct |
72 ms |
18132 KB |
Output is correct |
56 |
Correct |
58 ms |
18400 KB |
Output is correct |
57 |
Correct |
50 ms |
19004 KB |
Output is correct |