# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
198166 | model_code | Monthly railway pass (LMIO18_menesinis_bilietas) | C++14 | 1806 ms | 40224 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |