#include <bits/stdc++.h>
#include "train.h"
using namespace std;
const int N = 5001 ;
vector<int> adj[N] ;
void dfs(int i , vector<int>&vis,bool flg , vector<int>& r ){
vis[i]=1 ;
for(int x: adj[i]){
if(flg){
if(vis[x]==0&&r[x]==0){
dfs(x,vis,flg,r);
}
continue ;
}
if(vis[x]==0) dfs(x,vis,flg,r) ;
}
return ;
}
vector<int> who_wins(vector<int> a,vector<int> r,vector<int> u,vector<int>v){
int n = a.size() ;
for(int i=0;i<u.size();i++)adj[u[i]].push_back(v[i]) ;
vector<vector<int>> reachable(n) ;
for(int i=0;i<n;i++){
reachable[i].resize(n) ;
dfs(i,reachable[i],0,r);
reachable[i][i]=0 ;
for(int x : adj[i]) if(x==i) reachable[i][i]=1;
}
vector<int> cycle(n) ;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(reachable[i][j]&&reachable[j][i]){
cycle[i]=1;
}
}
}
vector<vector<int>> reachablee(n) ;
for(int i=0;i<n;i++){
reachablee[i].resize(n);
if(r[i]) continue ;
dfs(i,reachablee[i],1,r) ;
reachablee[i][i]=0;
for(int x: adj[i]) if(x==i) reachablee[i][i]=1 ;
}
vector<int> cyclee(n) ;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(reachablee[i][j]&&reachablee[j][i]){
cycle[j]=1;
}
}
}
bool flg = 0;
for(int i=0;i<n;i++) if(adj[i].empty()) flg=1;
vector<int> ans(n) ;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(adj[j].empty()&&(reachable[i][j]||i==j)){
ans[i]=1;
break;
}
if(cyclee[j]&&(reachable[i][j]||i==j)){
ans[i]=1 ;
break ;
}
}
}
return ans ;
}
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:21:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | for(int i=0;i<u.size();i++)adj[u[i]].push_back(v[i]) ;
| ~^~~~~~~~~
train.cpp:53:10: warning: variable 'flg' set but not used [-Wunused-but-set-variable]
53 | bool flg = 0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
511 ms |
197068 KB |
3rd lines differ - on the 14th token, expected: '1', found: '0' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
3rd lines differ - on the 2nd token, expected: '1', found: '0' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
700 ms |
197336 KB |
Output is correct |
2 |
Correct |
612 ms |
197376 KB |
Output is correct |
3 |
Correct |
569 ms |
197256 KB |
Output is correct |
4 |
Execution timed out |
2099 ms |
197212 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1188 ms |
197180 KB |
3rd lines differ - on the 1st token, expected: '1', found: '0' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2094 ms |
163712 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
511 ms |
197068 KB |
3rd lines differ - on the 14th token, expected: '1', found: '0' |
2 |
Halted |
0 ms |
0 KB |
- |