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>
#define vi vector<int>
#define pb push_back
//#include "train.h"
using namespace std;
int n,m;
vi X,Y,good,who,adj[5009];
vi CASE1() {
vi ans(n);
for (int i = n-1; i >= 0; i--) {
bool flag = 0;
for (int j : adj[i]) {
if (j == i && who[i] == good[i]) {
ans[i] = who[i];
flag = 1;
break;
}
if (who[i] == ans[j] && j != i) {
flag = 1;
ans[i] = ans[j];
break;
}
}
if (flag == 0) ans[i] = (!who[i]);
}
return ans;
}
vector<int> vis(5005),P(5005),G(5005),depth(5005);
bool dfs(int v) {
vis[v] = 1;
if (good[v]) G[v] = v;
else if (P[v] != -1)G[v] = G[P[v]];
else G[v] = -1;
bool ans = 0;
for (int i : adj[v]) {
if (vis[i]) {
if (G[v] != -1 && depth[G[v]] >= depth[i])
ans = 1;
}
else {depth[i] = depth[v] + 1; P[i] = v;ans |= dfs(i);}
}
return ans;
}
vi CASE2() {
vi ans(n);//,vis(n),P(n),G(n),depth(n);
for (int i = 0; i < n; i++) {
P[i] = -1;
ans[i] = dfs(i);
for (int i = 0 ; i < n; i++) {
P[i] = G[i] = vis[i] = depth[i] = 0;
}
/* fill(P.begin(),P.end(),0);
fill(G.begin(),G.end(),0);
fill(vis.begin(),vis.end(),0);
fill(depth.begin(),depth.end(),0);*/
}
return ans;
}
vi who_wins(vi a1, vi r, vi st, vi en) {
n = a1.size(), m = st.size();
X = st; Y = en;
for (int i = 0; i < m; i++)
adj[X[i]].pb(Y[i]);
good = r;
who = a1;
bool case1 = 1,case2 = 1;
for (int i = 0; i < m; i++) {
if (X[i] != Y[i] && X[i]+1 != Y[i])
case1 = 0;
}
for (int i = 0;i < n; i++)
if (who[i] == 0) case2 = 0;
if (case1) return CASE1();
if (case2) return CASE2();
// return res;
return vector<int>();
}
/*
main() {
cin >> n >> m;
vi r(n),a1(n),st(m),en(m);
for (int i = 0; i < n; i++)
cin >> a1[i];
for (int i = 0; i < n ;i++)
cin >> r[i];
for (int i = 0; i < m ;i++) {
int x,y;
cin >> st[i] >> en[i];
}
vi ans = who_wins(a1,r,st,en);
for (int i :ans) cout << i <<' ';
}*/
# | 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... |