#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
using namespace std;
struct vertex {
int group;
vector<int> train_edges;
vector<int> bus_edges;
bool visited;
vertex(): group(-1), visited(false) {}
};
vector<vertex> map;
void paint_graph(int idx, int colour) {
assert(map[idx].group == -1);
vertex& v = map[idx];
v.group = colour;
for (const int& sidx : v.train_edges) {
assert(0 <= sidx && sidx < map.size());
vertex& sibling = map[sidx];
if (sibling.group != -1)
continue;
paint_graph(sidx, colour);
}
}
int find_adjacent_comps(int idx, vector<bool>& adjacent) {
assert(!map[idx].visited);
vertex& v = map[idx];
v.visited = true;
int size = 1;
for (const int& sidx : v.bus_edges) {
assert(0 <= sidx && sidx < map.size());
vertex& sibling = map[sidx];
adjacent[sibling.group] = true;
}
for (const int& sidx : v.train_edges) {
assert(0 <= sidx && sidx < map.size());
vertex& sibling = map[sidx];
if (sibling.visited)
continue;
size += find_adjacent_comps(sidx, adjacent);
}
return size;
}
int find_full_comp_size(int idx, int groups) {
vector<bool> bitset(groups, false);
bitset[map[idx].group] = true; // Always visit yourself
int size = find_adjacent_comps(idx, bitset);
for (int i = 0; i < groups; i++)
if (!bitset[i])
return 0;
return size;
}
int main(int argc, const char * argv[]) {
ios::sync_with_stdio(false), cin.tie(0);
int n, m;
cin >> n >> m;
map.resize(n);
for (int i = 0; i < m; i++) {
int a, b;
char c;
cin >> a >> b >> c;
if (c == 'T') {
map[a-1].train_edges.push_back(b-1);
map[b-1].train_edges.push_back(a-1);
} else {
map[a-1].bus_edges.push_back(b-1);
map[b-1].bus_edges.push_back(a-1);
}
}
int group = 0;
for (int i = 0; i < n; i++)
if (map[i].group == -1)
paint_graph(i, group++);
int size = 0;
for (int i = 0; i < n; i++)
if (!map[i].visited)
size += find_full_comp_size(i, group);
cout << size << endl;
return 0;
}
Compilation message
In file included from /usr/include/c++/7/cassert:44:0,
from menesinis_bilietas.cpp:4:
menesinis_bilietas.cpp: In function 'void paint_graph(int, int)':
menesinis_bilietas.cpp:25:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
assert(0 <= sidx && sidx < map.size());
~~~~~^~~~~~~~~~~~
menesinis_bilietas.cpp: In function 'int find_adjacent_comps(int, std::vector<bool>&)':
menesinis_bilietas.cpp:44:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
assert(0 <= sidx && sidx < map.size());
~~~~~^~~~~~~~~~~~
menesinis_bilietas.cpp:51:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
assert(0 <= sidx && sidx < map.size());
~~~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
574 ms |
36472 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
1806 ms |
29876 KB |
Output is correct |
5 |
Correct |
5 ms |
1144 KB |
Output is correct |
6 |
Correct |
77 ms |
3704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1806 ms |
29876 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
504 KB |
Output is correct |
5 |
Correct |
5 ms |
1144 KB |
Output is correct |
6 |
Correct |
221 ms |
7160 KB |
Output is correct |
7 |
Correct |
1739 ms |
40224 KB |
Output is correct |
8 |
Correct |
12 ms |
1784 KB |
Output is correct |
9 |
Correct |
21 ms |
1912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
4 ms |
376 KB |
Output is correct |
4 |
Correct |
4 ms |
504 KB |
Output is correct |
5 |
Correct |
6 ms |
504 KB |
Output is correct |
6 |
Correct |
4 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
7 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
3 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
3 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
3 ms |
376 KB |
Output is correct |
16 |
Correct |
3 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
5 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
4 ms |
376 KB |
Output is correct |
4 |
Correct |
4 ms |
504 KB |
Output is correct |
5 |
Correct |
6 ms |
504 KB |
Output is correct |
6 |
Correct |
4 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
7 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
3 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
3 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
3 ms |
376 KB |
Output is correct |
16 |
Correct |
11 ms |
888 KB |
Output is correct |
17 |
Correct |
166 ms |
6424 KB |
Output is correct |
18 |
Correct |
44 ms |
2424 KB |
Output is correct |
19 |
Correct |
56 ms |
3068 KB |
Output is correct |
20 |
Correct |
34 ms |
2012 KB |
Output is correct |
21 |
Correct |
86 ms |
4064 KB |
Output is correct |
22 |
Correct |
3 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
5 ms |
504 KB |
Output is correct |
27 |
Correct |
5 ms |
1144 KB |
Output is correct |
28 |
Correct |
12 ms |
1784 KB |
Output is correct |
29 |
Correct |
21 ms |
1912 KB |
Output is correct |
30 |
Correct |
77 ms |
3704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
4 ms |
376 KB |
Output is correct |
4 |
Correct |
4 ms |
504 KB |
Output is correct |
5 |
Correct |
6 ms |
504 KB |
Output is correct |
6 |
Correct |
4 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
7 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
3 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
3 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
3 ms |
376 KB |
Output is correct |
16 |
Correct |
11 ms |
888 KB |
Output is correct |
17 |
Correct |
166 ms |
6424 KB |
Output is correct |
18 |
Correct |
44 ms |
2424 KB |
Output is correct |
19 |
Correct |
56 ms |
3068 KB |
Output is correct |
20 |
Correct |
34 ms |
2012 KB |
Output is correct |
21 |
Correct |
86 ms |
4064 KB |
Output is correct |
22 |
Correct |
574 ms |
36472 KB |
Output is correct |
23 |
Correct |
3 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
1806 ms |
29876 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
2 ms |
376 KB |
Output is correct |
28 |
Correct |
5 ms |
504 KB |
Output is correct |
29 |
Correct |
5 ms |
1144 KB |
Output is correct |
30 |
Correct |
221 ms |
7160 KB |
Output is correct |
31 |
Correct |
1739 ms |
40224 KB |
Output is correct |
32 |
Correct |
45 ms |
3704 KB |
Output is correct |
33 |
Correct |
430 ms |
14700 KB |
Output is correct |
34 |
Correct |
329 ms |
19716 KB |
Output is correct |
35 |
Correct |
12 ms |
1784 KB |
Output is correct |
36 |
Correct |
21 ms |
1912 KB |
Output is correct |
37 |
Correct |
77 ms |
3704 KB |
Output is correct |