#include <bits/stdc++.h>
using namespace std;
vector<int> train[500069], bus[500069];
set<int> adj[500069];
int par[500069], sz[500069];
bool visited[500069];
int pars;
void init(){
for(int i=0; i<500069; i++){
par[i] = i;
sz[i] = 1;
visited[i] = 0;
}
}
int fp(int x){
if(par[x]==x) return x;
else return par[x]=fp(par[x]);
}
void merge(int x, int y){
int px, py;
px = fp(x), py = fp(y);
if(sz[px] > sz[py]){
par[py] = px;
sz[px] += sz[py];
} else {
par[px] = py;
sz[py] += sz[px];
}
pars--;
}
void search(int x){
if(visited[x]) return;
visited[x] = true;
for(int i: train[x]){
if(visited[i]) continue;
if(fp(x) != fp(i)) merge(x,i);
search(x);
}
}
int main(){
ios::sync_with_stdio(0);
cout.tie(0); cin.tie(0);
init();
int n,m; cin >> n >> m;
pars = n;
for(int i=0; i<m; i++){
int a,b; char c;
cin >> a >> b >> c;
a--; b--;
if(c=='A'){
bus[a].push_back(b);
bus[b].push_back(a);
} else {
train[a].push_back(b);
train[b].push_back(a);
}
}
for(int i=0; i<n; i++){
if(!visited[i])search(i);
}
for(int i=0; i<n; i++){
for(int j: bus[i]){
int px = fp(i), py = fp(j);
if(px==py) continue;
//cout << px << " " << py << "\n";
adj[px].insert(py);
adj[py].insert(px);
}
}
int ans = 0;
/*
if(pars == 1){
cout << n << "\n";
return 0;
}
*/
//cout << pars << "\n";
for(int i=0; i<n; i++){
if(fp(i)!=i) continue;
//if(adj[i].size() != 0) cout << i << " " << adj[i].size() << "\n";
if(((int)adj[i].size()) >= pars-1) ans += sz[fp(i)];
}
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
296 ms |
62292 KB |
Output is correct |
2 |
Correct |
24 ms |
51692 KB |
Output is correct |
3 |
Correct |
25 ms |
51644 KB |
Output is correct |
4 |
Correct |
33 ms |
51684 KB |
Output is correct |
5 |
Correct |
24 ms |
51668 KB |
Output is correct |
6 |
Correct |
67 ms |
54068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
51684 KB |
Output is correct |
2 |
Correct |
24 ms |
51668 KB |
Output is correct |
3 |
Correct |
32 ms |
51668 KB |
Output is correct |
4 |
Correct |
26 ms |
51776 KB |
Output is correct |
5 |
Correct |
31 ms |
52404 KB |
Output is correct |
6 |
Correct |
435 ms |
86456 KB |
Output is correct |
7 |
Correct |
723 ms |
104036 KB |
Output is correct |
8 |
Correct |
36 ms |
53580 KB |
Output is correct |
9 |
Correct |
45 ms |
54880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
51692 KB |
Output is correct |
2 |
Correct |
25 ms |
51644 KB |
Output is correct |
3 |
Correct |
32 ms |
51668 KB |
Output is correct |
4 |
Correct |
26 ms |
51776 KB |
Output is correct |
5 |
Correct |
31 ms |
52404 KB |
Output is correct |
6 |
Correct |
25 ms |
51676 KB |
Output is correct |
7 |
Correct |
23 ms |
51668 KB |
Output is correct |
8 |
Correct |
25 ms |
51764 KB |
Output is correct |
9 |
Correct |
27 ms |
51752 KB |
Output is correct |
10 |
Correct |
26 ms |
51788 KB |
Output is correct |
11 |
Correct |
26 ms |
51884 KB |
Output is correct |
12 |
Correct |
24 ms |
51736 KB |
Output is correct |
13 |
Correct |
29 ms |
51708 KB |
Output is correct |
14 |
Correct |
28 ms |
52044 KB |
Output is correct |
15 |
Correct |
24 ms |
51688 KB |
Output is correct |
16 |
Correct |
25 ms |
51668 KB |
Output is correct |
17 |
Correct |
26 ms |
51668 KB |
Output is correct |
18 |
Correct |
32 ms |
51788 KB |
Output is correct |
19 |
Correct |
29 ms |
51744 KB |
Output is correct |
20 |
Correct |
26 ms |
51828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
51676 KB |
Output is correct |
2 |
Correct |
23 ms |
51668 KB |
Output is correct |
3 |
Correct |
25 ms |
51764 KB |
Output is correct |
4 |
Correct |
27 ms |
51752 KB |
Output is correct |
5 |
Correct |
26 ms |
51788 KB |
Output is correct |
6 |
Correct |
26 ms |
51884 KB |
Output is correct |
7 |
Correct |
24 ms |
51736 KB |
Output is correct |
8 |
Correct |
29 ms |
51708 KB |
Output is correct |
9 |
Correct |
28 ms |
52044 KB |
Output is correct |
10 |
Correct |
24 ms |
51688 KB |
Output is correct |
11 |
Correct |
25 ms |
51668 KB |
Output is correct |
12 |
Correct |
26 ms |
51668 KB |
Output is correct |
13 |
Correct |
32 ms |
51788 KB |
Output is correct |
14 |
Correct |
29 ms |
51744 KB |
Output is correct |
15 |
Correct |
26 ms |
51828 KB |
Output is correct |
16 |
Correct |
24 ms |
51692 KB |
Output is correct |
17 |
Correct |
25 ms |
51644 KB |
Output is correct |
18 |
Correct |
32 ms |
51668 KB |
Output is correct |
19 |
Correct |
26 ms |
51776 KB |
Output is correct |
20 |
Correct |
31 ms |
52404 KB |
Output is correct |
21 |
Correct |
24 ms |
51668 KB |
Output is correct |
22 |
Correct |
36 ms |
53580 KB |
Output is correct |
23 |
Correct |
45 ms |
54880 KB |
Output is correct |
24 |
Correct |
67 ms |
54068 KB |
Output is correct |
25 |
Correct |
43 ms |
52428 KB |
Output is correct |
26 |
Correct |
189 ms |
58092 KB |
Output is correct |
27 |
Correct |
77 ms |
54856 KB |
Output is correct |
28 |
Correct |
204 ms |
66644 KB |
Output is correct |
29 |
Correct |
64 ms |
54736 KB |
Output is correct |
30 |
Correct |
199 ms |
64968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
51676 KB |
Output is correct |
2 |
Correct |
23 ms |
51668 KB |
Output is correct |
3 |
Correct |
25 ms |
51764 KB |
Output is correct |
4 |
Correct |
27 ms |
51752 KB |
Output is correct |
5 |
Correct |
26 ms |
51788 KB |
Output is correct |
6 |
Correct |
26 ms |
51884 KB |
Output is correct |
7 |
Correct |
24 ms |
51736 KB |
Output is correct |
8 |
Correct |
29 ms |
51708 KB |
Output is correct |
9 |
Correct |
28 ms |
52044 KB |
Output is correct |
10 |
Correct |
24 ms |
51688 KB |
Output is correct |
11 |
Correct |
25 ms |
51668 KB |
Output is correct |
12 |
Correct |
26 ms |
51668 KB |
Output is correct |
13 |
Correct |
32 ms |
51788 KB |
Output is correct |
14 |
Correct |
29 ms |
51744 KB |
Output is correct |
15 |
Correct |
26 ms |
51828 KB |
Output is correct |
16 |
Correct |
43 ms |
52428 KB |
Output is correct |
17 |
Correct |
189 ms |
58092 KB |
Output is correct |
18 |
Correct |
77 ms |
54856 KB |
Output is correct |
19 |
Correct |
204 ms |
66644 KB |
Output is correct |
20 |
Correct |
64 ms |
54736 KB |
Output is correct |
21 |
Correct |
199 ms |
64968 KB |
Output is correct |
22 |
Correct |
296 ms |
62292 KB |
Output is correct |
23 |
Correct |
24 ms |
51692 KB |
Output is correct |
24 |
Correct |
25 ms |
51644 KB |
Output is correct |
25 |
Correct |
33 ms |
51684 KB |
Output is correct |
26 |
Correct |
32 ms |
51668 KB |
Output is correct |
27 |
Correct |
26 ms |
51776 KB |
Output is correct |
28 |
Correct |
31 ms |
52404 KB |
Output is correct |
29 |
Correct |
24 ms |
51668 KB |
Output is correct |
30 |
Correct |
435 ms |
86456 KB |
Output is correct |
31 |
Correct |
723 ms |
104036 KB |
Output is correct |
32 |
Correct |
36 ms |
53580 KB |
Output is correct |
33 |
Correct |
45 ms |
54880 KB |
Output is correct |
34 |
Correct |
67 ms |
54068 KB |
Output is correct |
35 |
Correct |
62 ms |
55764 KB |
Output is correct |
36 |
Correct |
418 ms |
65696 KB |
Output is correct |
37 |
Correct |
160 ms |
59536 KB |
Output is correct |