#include <bits/stdc++.h>
using namespace std;
const int mxN = 5e5 + 5;
struct DSU{
vector<int>par;
int comps;
DSU(int n){
par.assign(n + 1, -1);
comps = n;
}
int FindRep(int u){
if(par[u] < 0)return u;
return par[u] = FindRep(par[u]);
}
void Union(int u, int v){
u = FindRep(u);
v = FindRep(v);
if(u == v)return;
if(par[u] > par[v])swap(u, v);
par[u] += par[v];
par[v] = u;
comps--;
}
};
struct T{
int u, v;
char c;
};
vector<int> G[mxN];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
/// A - bus, T - train
vector<T>edges(m);
DSU dsu(n);
for(int i = 0;i < m;++i){
int u, v;
char c;
cin >> u >> v >> c;
edges[i] = {u, v, c};
if(c == 'T')dsu.Union(u, v);
}
set<pair<int, int>>s;
for(int i = 0;i < m;++i){
int u = dsu.FindRep(edges[i].u), v = dsu.FindRep(edges[i].v), c = edges[i].c;
if(c == 'T' || u == v)continue;
s.insert({u, v});
}
for(auto i : s){
int u = i.first, v = i.second;
G[u].push_back(v);
G[v].push_back(u);
}
int res = 0, comps = dsu.comps;
for(int i = 1;i <= n;++i){
int u = dsu.FindRep(i);
res += ((int)G[u].size() == comps - 1);
}
cout << res << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
88 ms |
18744 KB |
Output is correct |
2 |
Correct |
3 ms |
12124 KB |
Output is correct |
3 |
Correct |
5 ms |
12124 KB |
Output is correct |
4 |
Correct |
5 ms |
13912 KB |
Output is correct |
5 |
Correct |
3 ms |
12124 KB |
Output is correct |
6 |
Correct |
28 ms |
14428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
13912 KB |
Output is correct |
2 |
Correct |
3 ms |
12124 KB |
Output is correct |
3 |
Correct |
4 ms |
12120 KB |
Output is correct |
4 |
Correct |
3 ms |
12124 KB |
Output is correct |
5 |
Correct |
7 ms |
12636 KB |
Output is correct |
6 |
Correct |
224 ms |
35308 KB |
Output is correct |
7 |
Correct |
314 ms |
51768 KB |
Output is correct |
8 |
Correct |
9 ms |
13656 KB |
Output is correct |
9 |
Correct |
17 ms |
14464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12124 KB |
Output is correct |
2 |
Correct |
5 ms |
12124 KB |
Output is correct |
3 |
Correct |
4 ms |
12120 KB |
Output is correct |
4 |
Correct |
3 ms |
12124 KB |
Output is correct |
5 |
Correct |
7 ms |
12636 KB |
Output is correct |
6 |
Correct |
3 ms |
12124 KB |
Output is correct |
7 |
Correct |
3 ms |
12200 KB |
Output is correct |
8 |
Correct |
4 ms |
12124 KB |
Output is correct |
9 |
Correct |
4 ms |
12124 KB |
Output is correct |
10 |
Correct |
5 ms |
12376 KB |
Output is correct |
11 |
Correct |
5 ms |
12376 KB |
Output is correct |
12 |
Correct |
3 ms |
12120 KB |
Output is correct |
13 |
Correct |
3 ms |
12120 KB |
Output is correct |
14 |
Correct |
6 ms |
12376 KB |
Output is correct |
15 |
Correct |
3 ms |
12124 KB |
Output is correct |
16 |
Correct |
3 ms |
12128 KB |
Output is correct |
17 |
Correct |
3 ms |
12124 KB |
Output is correct |
18 |
Correct |
5 ms |
12124 KB |
Output is correct |
19 |
Correct |
3 ms |
12124 KB |
Output is correct |
20 |
Correct |
4 ms |
12124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12124 KB |
Output is correct |
2 |
Correct |
3 ms |
12200 KB |
Output is correct |
3 |
Correct |
4 ms |
12124 KB |
Output is correct |
4 |
Correct |
4 ms |
12124 KB |
Output is correct |
5 |
Correct |
5 ms |
12376 KB |
Output is correct |
6 |
Correct |
5 ms |
12376 KB |
Output is correct |
7 |
Correct |
3 ms |
12120 KB |
Output is correct |
8 |
Correct |
3 ms |
12120 KB |
Output is correct |
9 |
Correct |
6 ms |
12376 KB |
Output is correct |
10 |
Correct |
3 ms |
12124 KB |
Output is correct |
11 |
Correct |
3 ms |
12128 KB |
Output is correct |
12 |
Correct |
3 ms |
12124 KB |
Output is correct |
13 |
Correct |
5 ms |
12124 KB |
Output is correct |
14 |
Correct |
3 ms |
12124 KB |
Output is correct |
15 |
Correct |
4 ms |
12124 KB |
Output is correct |
16 |
Correct |
3 ms |
12124 KB |
Output is correct |
17 |
Correct |
5 ms |
12124 KB |
Output is correct |
18 |
Correct |
4 ms |
12120 KB |
Output is correct |
19 |
Correct |
3 ms |
12124 KB |
Output is correct |
20 |
Correct |
7 ms |
12636 KB |
Output is correct |
21 |
Correct |
3 ms |
12124 KB |
Output is correct |
22 |
Correct |
9 ms |
13656 KB |
Output is correct |
23 |
Correct |
17 ms |
14464 KB |
Output is correct |
24 |
Correct |
28 ms |
14428 KB |
Output is correct |
25 |
Correct |
9 ms |
12832 KB |
Output is correct |
26 |
Correct |
86 ms |
21492 KB |
Output is correct |
27 |
Correct |
34 ms |
15680 KB |
Output is correct |
28 |
Correct |
95 ms |
23236 KB |
Output is correct |
29 |
Correct |
29 ms |
14940 KB |
Output is correct |
30 |
Correct |
70 ms |
22380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12124 KB |
Output is correct |
2 |
Correct |
3 ms |
12200 KB |
Output is correct |
3 |
Correct |
4 ms |
12124 KB |
Output is correct |
4 |
Correct |
4 ms |
12124 KB |
Output is correct |
5 |
Correct |
5 ms |
12376 KB |
Output is correct |
6 |
Correct |
5 ms |
12376 KB |
Output is correct |
7 |
Correct |
3 ms |
12120 KB |
Output is correct |
8 |
Correct |
3 ms |
12120 KB |
Output is correct |
9 |
Correct |
6 ms |
12376 KB |
Output is correct |
10 |
Correct |
3 ms |
12124 KB |
Output is correct |
11 |
Correct |
3 ms |
12128 KB |
Output is correct |
12 |
Correct |
3 ms |
12124 KB |
Output is correct |
13 |
Correct |
5 ms |
12124 KB |
Output is correct |
14 |
Correct |
3 ms |
12124 KB |
Output is correct |
15 |
Correct |
4 ms |
12124 KB |
Output is correct |
16 |
Correct |
9 ms |
12832 KB |
Output is correct |
17 |
Correct |
86 ms |
21492 KB |
Output is correct |
18 |
Correct |
34 ms |
15680 KB |
Output is correct |
19 |
Correct |
95 ms |
23236 KB |
Output is correct |
20 |
Correct |
29 ms |
14940 KB |
Output is correct |
21 |
Correct |
70 ms |
22380 KB |
Output is correct |
22 |
Correct |
88 ms |
18744 KB |
Output is correct |
23 |
Correct |
3 ms |
12124 KB |
Output is correct |
24 |
Correct |
5 ms |
12124 KB |
Output is correct |
25 |
Correct |
5 ms |
13912 KB |
Output is correct |
26 |
Correct |
4 ms |
12120 KB |
Output is correct |
27 |
Correct |
3 ms |
12124 KB |
Output is correct |
28 |
Correct |
7 ms |
12636 KB |
Output is correct |
29 |
Correct |
3 ms |
12124 KB |
Output is correct |
30 |
Correct |
224 ms |
35308 KB |
Output is correct |
31 |
Correct |
314 ms |
51768 KB |
Output is correct |
32 |
Correct |
9 ms |
13656 KB |
Output is correct |
33 |
Correct |
17 ms |
14464 KB |
Output is correct |
34 |
Correct |
28 ms |
14428 KB |
Output is correct |
35 |
Correct |
25 ms |
14676 KB |
Output is correct |
36 |
Correct |
135 ms |
26624 KB |
Output is correct |
37 |
Correct |
46 ms |
18260 KB |
Output is correct |