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 "train.h"
#include <bits/stdc++.h>
using namespace std;
//macros
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, ll> lll;
typedef tuple<int, int, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<ll> vll;
typedef vector<lll> vlll;
#define REP(a,b,c) for(int a=int(b); a<int(c); a++)
#define RE(a,c) REP(a,0,c)
#define RE1(a,c) REP(a,1,c+1)
#define REI(a,b,c) REP(a,b,c+1)
#define REV(a,b,c) for(int a=int(c-1); a>=int(b); a--)
#define INF 1e9
#define pb push_back
#define fi first
#define se second
#define sz size()
const int MX=6000;
int n, m;
vi a, r, u, v;
vi res;
vi adj[MX], rev[MX];
int path[MX];
bitset<MX> visited;
stack<int> scS;
int com[MX];
int scN=0;
set<int> adjC[MX], revC[MX];
bitset<MX> rCom;
int comSZ[MX];
int dp[MX];
bool isLoop(int u, int start) {
if(r[u]) return true;
if(u == start) return false;
return isLoop(adj[u][path[u]], start);
}
bool winner(int u) {
if(a[u]) {
// try to charge station
bool ans = 0;
RE(i,adj[u].sz) {
path[u] = i;
if(path[adj[u][i]] != -1) {
if(isLoop(adj[u][i], u) == 1) {
ans = 1;
break;
}
} else if(winner(adj[u][i]) == 1) {
ans = 1;
break;
}
}
path[u] = -1;
return ans;
} else {
// try to run out of energy
bool ans = 1;
RE(i,adj[u].sz) {
path[u] = i;
if(path[adj[u][i]] != -1) {
if(isLoop(adj[u][i], u) == 0) {
ans = 0;
break;
}
} else if(winner(adj[u][i]) == 0) {
ans = 0;
break;
}
}
path[u] = -1;
return ans;
}
}
void dfsSC1(int u) {
if(visited[u]) return;
visited[u] = 1;
for(int v:adj[u]) dfsSC1(v);
scS.push(u);
}
void dfsSC2(int u) {
if(visited[u]) return;
visited[u] = 1;
com[u] = scN;
comSZ[scN]++;
for(int v:rev[u]) dfsSC2(v);
}
void findSC() {
visited.reset();
RE(i,n) dfsSC1(i);
visited.reset();
while(!scS.empty()) {
int u = scS.top(); scS.pop();
if(visited[u]) continue;
comSZ[scN] = 0;
dfsSC2(u);
scN++;
}
RE(u,n) for(int v:adj[u]) if(com[u] != com[v]) adjC[com[u]].insert(com[v]);
RE(u,n) for(int v:adj[u]) if(com[u] != com[v]) revC[com[v]].insert(com[u]);
}
bool isLoop(int u) {
if(comSZ[com[u]] > 1) return 1;
for(int v:adj[u]) if(v == u) return 1;
return 0;
}
bool getDPA(int u) {
if(dp[u] != -1) return dp[u];
dp[u] = rCom[u];
for(int v:adjC[u]) dp[u] |= getDPA(v);
return dp[u];
}
void fillAllA() {
rCom.reset();
RE(u,n) if(r[u] && isLoop(u)) rCom[com[u]] = 1;
RE(i,scN) dp[i] = -1;
RE(u,n) res[u] = getDPA(com[u]);
}
void findSCB() {
visited.reset();
RE(i,n) if(r[i]) visited[i] = 1;
RE(i,n) dfsSC1(i);
visited.reset();
RE(i,n) if(r[i]) visited[i] = 1;
RE(i,n) com[i] = -1;
while(!scS.empty()) {
int u = scS.top(); scS.pop();
if(visited[u]) continue;
comSZ[scN] = 0;
dfsSC2(u);
scN++;
}
RE(u,n) for(int v:adj[u]) if(com[u] != com[v]) adjC[com[u]].insert(com[v]);
RE(u,n) for(int v:adj[u]) if(com[u] != com[v]) revC[com[v]].insert(com[u]);
}
void dfsB(int u) {
if(res[u] == 0) return;
res[u] = 0;
for(int v:rev[u]) {
dfsB(v);
}
}
void fillAllB() {
RE(u,n) res[u] = 1;
RE(u,n) if(com[u] != -1 && isLoop(u)) dfsB(u);
}
vi who_wins(vi A, vi R, vi U, vi V) {
a = A; r = R; u = U; v = V;
n = a.size(); m = u.size();
res.resize(n);
RE(i,m) adj[u[i]].pb(v[i]);
RE(i,m) rev[v[i]].pb(u[i]);
bool allA = 1;
RE(i,n) if(!a[i]) allA = 0;
if(allA) {
findSC();
fillAllA();
return res;
}
bool allB = 1;
RE(i,n) if(a[i]) allB = 0;
if(allB) {
findSCB();
fillAllB();
return res;
}
RE(i,n) {
RE(i,n) path[i] = -1;
res[i] = winner(i);
}
return res;
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |