#include <bits/stdc++.h>
using namespace std;
set<int> used_cc;
vector<int> used;
vector<int> cc_size;
vector<int> distribute_used;
vector<int> answer_used;
vector<int> cc_leader;
vector<vector<int>> bus_g;
vector<vector<int>> train_g;
int calculate_cc_size(int u, int leader) {
used[u] = true;
int curr_size = 1;
for(auto v : train_g[u]) {
if(used[v]) {
continue;
}
cc_leader[v] = leader;
curr_size += calculate_cc_size(v, leader);
}
return curr_size;
}
int calculate_cheap_routes(int u) {
answer_used[u] = true;
int ans = 0;
for(auto v : train_g[u]) {
if(answer_used[v]) {
continue;
}
ans += calculate_cheap_routes(v);
}
for(auto v : bus_g[u]) {
if(used_cc.count(cc_leader[v])) {
continue;
}
used_cc.insert(cc_leader[v]);
ans += cc_size[cc_leader[v]];
}
return ans;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
bus_g = vector<vector<int>>(n);
train_g = vector<vector<int>>(n);
for(int i = 0; i < m; i++) {
int from, to;
char type;
cin >> from >> to >> type;
from--;
to--;
if(type == 'T') {
train_g[to].push_back(from);
train_g[from].push_back(to);
}
if(type == 'A') {
bus_g[to].push_back(from);
bus_g[from].push_back(to);
}
}
cc_leader = vector<int>(n);
for(int i = 0; i < n; i++) {
cc_leader[i] = i;
}
used = vector<int>(n);
cc_size = vector<int>(n);
distribute_used = vector<int>(n);
for(int i = 0; i < n; i++) {
if(!used[i]) {
int curr = calculate_cc_size(i, i);
cc_size[cc_leader[i]] = curr;
/*for(auto c : cc_leader) {
cout << c << ' ';
}
cout << '\n';*/
/*cout << i << ' ' << curr << '\n';
for(auto c : cc_size) {
cout << c << ' ';
}
cout << '\n';*/
}
}
// Calculate the number of reachable cities for every node
int ans = 0;
answer_used = vector<int>(n);
vector<int> cheap_cities(n);
for(int i = 0; i < n; i++) {
if(!answer_used[i]) {
used_cc.clear();
cheap_cities[cc_leader[i]] = cc_size[cc_leader[i]] + calculate_cheap_routes(i);
}
}
for(int i = 0; i < n; i++) {
//cout << c << ' ';
if(cheap_cities[cc_leader[i]] == n) {
ans++;
}
}
cout << '\n';
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
310 ms |
45588 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
16 ms |
33372 KB |
Output is correct |
5 |
Correct |
1 ms |
1372 KB |
Output is correct |
6 |
Correct |
32 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
33372 KB |
Output is correct |
2 |
Correct |
1 ms |
1372 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
177 ms |
14292 KB |
Output is correct |
7 |
Correct |
362 ms |
68456 KB |
Output is correct |
8 |
Correct |
8 ms |
2904 KB |
Output is correct |
9 |
Correct |
12 ms |
3164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Incorrect |
3 ms |
600 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Incorrect |
3 ms |
600 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Incorrect |
3 ms |
600 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |