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;
const int N = 5001;
int n,is[N],vis[N],own[N],to[N],self[N],flag1 = 1,flag2 = 1,no[N],tim[N],cnt = 0,can[N];
vector <int> g[N],S;
int dfs(int u){
vis[u] = 1;
S.push_back(u);
int ret;
if(own[u] == 0){
ret = 1;
for(int i = 0;i<g[u].size();i++){
int v = g[u][i];
if(vis[v] == 1){
int flag = 0;
for(int j = S.size()-1;j>=0;j--){
if(is[S[j]] == 1){
flag = 1;
break;
}
if(S[j] == v)
break;
}
ret = min(ret,flag);
}else{
ret = min(ret,dfs(v));
}
}
}else{
ret = 0;
for(int i = 0;i<g[u].size();i++){
int v = g[u][i];
if(vis[v] == 1){
int flag = 0;
for(int j = S.size()-1;j>=0;j--){
if(is[S[j]] == 1){
flag = 1;
break;
}
if(S[j] == v)
break;
}
ret = max(ret,flag);
}else{
ret = max(ret,dfs(v));
}
}
}
vis[u] = 0;
S.pop_back();
return ret;
}
int DFS(int u,int found){
tim[u] = ++cnt;
vis[u] = 1;
no[tim[u]] = 1;
int res = 0;
if(is[u] == 1)
found = tim[u];
for(int i = 0;i<g[u].size();i++){
int v = g[u][i];
if(vis[v] == 2){
res = max(res,can[v]);
continue;
}
else if(vis[v] == 1){
if(found != -1)
res = max(res,no[found] & (found >= tim[v]) ? 1 : 0);
continue;
}else{
res = max(res,DFS(v,found));
}
}
no[tim[u]] = 0;
vis[u] = 2;
can[u] |= res;
return res;
}
std::vector<int> who_wins(std::vector<int> a, std::vector<int> r, std::vector<int> u, std::vector<int> v) {
std::vector<int> res;
n = a.size();
for(int i = 0;i<n;i++){
is[i] = self[i] = to[i] = 0;
}
for(int i = 0;i<u.size();i++){
g[u[i]].push_back(v[i]);
if(u[i] == v[i])
self[u[i]] = 1;
if(u[i] + 1 == v[i])
to[u[i]] = 1;
}
for(int i = 0;i<n;i++)
is[i] = r[i];
for(int i = 0;i<n;i++){
own[i] = a[i];
if(a[i] != 1)
flag1 = 0;
if(a[i] != 0)
flag2 = 0;
}
if(n <= 15){
int cnt = 0;
for(int i = 0;i<n;i++){
for(int j = 0;j<n;j++){
vis[j] = 0;
}
cnt++;
res.push_back(dfs(i));
}
}else if(flag1 == 1){
for(int i = 0;i<n;++i){
if(can[i])
res.push_back(1);
else{
for(int j = 0;j<n;j++){
tim[j] = -1;
vis[j] = 0;
}
cnt = 0;
res.push_back(DFS(i,-1));
}
}
}else{
if(is[n-1] && self[n-1])
res.push_back(1);
else
res.push_back(0);
for(int i = n-2;i>=0;i--){
if(own[i] == 0){
int ret = 1;
ret = min(ret,((!is[i] && self[i]) ? 0 : 1));
ret = min(ret,((to[i]) ? res.back() : 1));
res.push_back(ret);
}else{
int ret = 0;
ret = max(ret,((is[i] && self[i]) ? 1 : 0));
ret = max(ret,((to[i]) ? res.back() : 0));
res.push_back(ret);
}
}
reverse(res.begin(),res.end());
}
return res;
}
Compilation message (stderr)
train.cpp: In function 'int dfs(int)':
train.cpp:15:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0;i<g[u].size();i++){
~^~~~~~~~~~~~
train.cpp:34:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0;i<g[u].size();i++){
~^~~~~~~~~~~~
train.cpp: In function 'int DFS(int, int)':
train.cpp:64:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0;i<g[u].size();i++){
~^~~~~~~~~~~~
train.cpp: In function 'std::vector<int> who_wins(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
train.cpp:90:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0;i<u.size();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... |