# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1077933 | TB_ | Toy Train (IOI17_train) | C++17 | 0 ms | 0 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 <bits/stdc++.h>
using namespace std;
#define ll long long
#define fo(i, n) for(ll i = 0; i<(n); i++)
#define F first
#define S second
#define pb push_back
#define deb(x) cout << #x << " = " << (x) << endl
#define deb2(x, y) cout << #x << " = " << (x) << ", " << #y << " = " << (y) << endl
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> pl;
typedef vector<pl> vpl;
ll n;
vl topo;
int seen[6000];
vvl adj(6000);
vvl adjR(6000);
int toposort(int u){
if(seen[u]) return;
seen[u] = 1;
for(auto &v : adj[u]){
toposort(v);
}
topo.pb(u);
}
int c = 0;
int dfs(int u){
if(seen[u]) return 0;
seen[u] = c;
ll ans = 1;
for(auto &v : adj[u]){
ans+=dfs(v);
}
return ans;
}
vector<int> res;
void dfs2(int u){
if(!res[u]) return;
res[u] = 0;
for(auto &v : adjR[u]){
dfs2(v);
}
}
std::vector<int> who_wins(std::vector<int> a, std::vector<int> r, std::vector<int> u, std::vector<int> v) {
n = a.size();
ll m = u.size();
vl selfEdge(n+1, 0);
fo(i, m){
if(u[i] == v[i]) selfEdge[u[i]] = 1;
else{
if(!r[i])adj[u[i]].pb(v[i]);
adjR[v[i]].pb(u[i]);
}
}
memset(seen, 0, sizeof(seen));
fo(i, n){
if(seen[i]) continue;
toposort(i);
}
reverse(all(topo));
memset(seen, 0, sizeof(seen));
vl good;
fo(i, n){
if(seen[i]) continue;
c++;
ll siz = dfs(i);
if(siz>1 || selfEdge[i]) good.pb(i);
}
res.assign(n, 1);
fo(i, good.size()){
dfs2(good[i]);
}
return res;
}