# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
575313 |
2022-06-10T07:39:24 Z |
KoD |
Toy Train (IOI17_train) |
C++17 |
|
0 ms |
0 KB |
#include "train.h"
#include <bits/stdc++.h>
using ll = long long;
using std::vector;
using std::array;
using std::pair;
using std::tuple;
template <class F> struct fixed : private F {
fixed(F&& f) : F(std::forward<F>(f)) {}
template <class... Args> decltype(auto) operator()(Args&&... args) const {
return F::operator()(*this, std::forward<Args>(args)...);
}
};
vector<int> who_wins(vector<int> a, vector<int> r, vector<int> u, vector<int> v) {
const int n = a.size();
const int m = u.size();
vector<char> loop(n);
vector<vector<int>> graph(n), rgraph(n);
for (int i = 0; i < m; ++i) {
if (u[i] == v[i]) {
loop[u[i]] = true;
}
graph[u[i]].push_back(v[i]);
rgraph[v[i]].push_back(u[i]);
}
vector<char> vis(n);
vector<int> stack;
for (int u = 0; u < n; ++u) {
fixed([&](auto&& dfs, const int u) -> void {
if (r[u] or vis[u]) return;
vis[u] = true;
for (const int v : graph[u]) {
dfs(v);
}
stack.push_back(u);
})(u);
}
std::fill(vis.begin(), vis.end(), false);
vector<vector<int>> group;
while (!stack.empty()) {
const int u = stack.back();
stack.pop_back();
if (vis[u]) continue;
auto& list = group.emplace_back();
fixed([&](auto&& dfs, const int u) -> void {
if (r[u] or vis[u]) return;
vis[u] = true;
for (const int v : rgraph[u]) {
dfs(v);
}
list.push_back(u);
})(u);
}
vector<int> ret(n, 1);
for (const auto& list : group) {
if (list.size() == 1) {
const int u = list[0];
if (loop[u]) {
ret[u] = 0;
}
} else {
for (const int u : list) {
ret[u] = true;
}
}
}
std::fill(vis.begin(), vis.end(), false);
vector<int> stack;
for (int u = 0; u < n; ++u) {
fixed([&](auto&& dfs, const int u) -> void {
if (vis[u]) return;
vis[u] = true;
for (const int v : graph[u]) {
dfs(v);
}
stack.push_back(u);
})(u);
}
std::fill(vis.begin(), vis.end(), false);
group.clear();
while (!stack.empty()) {
const int u = stack.back();
stack.pop_back();
if (vis[u]) continue;
auto& list = group.emplace_back();
fixed([&](auto&& dfs, const int u) -> void {
if (vis[u]) return;
vis[u] = true;
for (const int v : rgraph[u]) {
dfs(v);
}
list.push_back(u);
})(u);
}
while (!group.empty()) {
auto list = std::move(group.back());
group.pop_back();
bool found = false;
for (const int u : list) {
for (const int v : graph[u]) {
if (ret[v] == 0) {
ret[u] = 0;
}
}
if (ret[u] == 0) {
found = true;
}
}
if (found) {
for (const int u : list) {
ret[u] = 0;
}
}
}
return ret;
}
Compilation message
train.cpp: In function 'std::vector<int> who_wins(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
train.cpp:72:17: error: redeclaration of 'std::vector<int> stack'
72 | vector<int> stack;
| ^~~~~
train.cpp:31:17: note: 'std::vector<int> stack' previously declared here
31 | vector<int> stack;
| ^~~~~