#include "train.h"
#include<bits/stdc++.h>
using namespace std;
const int nmax=1e5+42;
int nxt[nmax];
vector<int> adj[nmax],trav[nmax],adj_comp[nmax];
int n,owner[nmax],charge[nmax];
int rec(int node)//1-> A wins, 0-> B wins
{
if(nxt[node]!=-1)
{
if(charge[node])return 1;
int other=nxt[node];
while(other!=node)
{
if(charge[other])return 1;
other=nxt[other];
}
return 0;
}
for(auto k:adj[node])
{
nxt[node]=k;
int mem=rec(k);
nxt[node]=-1;
if(mem==owner[node])return mem;
}
return !owner[node];
}
bool been[nmax];
stack<int> st;
void dfs(int node)
{
if(been[node])return;
been[node]=1;
for(auto k:adj[node])
dfs(k);
st.push(node);
}
int pointer,comp[nmax];
int mem[nmax];
int charge_comp[nmax];
void dfs_trav(int node)
{
if(comp[node])return;
comp[node]=pointer;
charge_comp[pointer]=charge_comp[pointer]|charge[node];
for(auto k:trav[node])
dfs_trav(k);
}
int win(int node)
{
if(mem[node]!=-1)return mem[node];
int ret=charge_comp[node];
for(auto k:adj_comp[node])
ret=ret|win(k);
mem[node]=ret;
return ret;
}
std::vector<int> who_wins(std::vector<int> a, std::vector<int> r, std::vector<int> u, std::vector<int> v)
{
memset(mem,-1,sizeof(mem));
memset(nxt,-1,sizeof(nxt));
n=a.size();
for(int i=0;i<a.size();i++)owner[i]=a[i];
for(int i=0;i<r.size();i++)charge[i]=r[i];
for(int i=0;i<u.size();i++)
{
adj[u[i]].push_back(v[i]);
trav[v[i]].push_back(u[i]);
}
//A is owner of all
for(int i=0;i<n;i++)
if(been[i]==0)dfs(i);
memset(been,0,sizeof(been));
while(st.size())
{
int tp=st.top();
//cout<<"tp= "<<tp<<endl;
st.pop();
if(been[tp])continue;
pointer++;
dfs_trav(tp);
}
for(int i=0;i<u.size();i++)
if(comp[u[i]]!=comp[v[i]])
{
//cout<<"adj_comp "<<comp[u[i]]<<" "<<comp[v[i]]<<endl;
adj_comp[comp[u[i]]].push_back(comp[v[i]]);
}
vector<int> outp={};
for(int i=0;i<n;i++)
outp.push_back(win(comp[i]));
return outp;
}
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:100:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
100 | for(int i=0;i<a.size();i++)owner[i]=a[i];
| ~^~~~~~~~~
train.cpp:102:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | for(int i=0;i<r.size();i++)charge[i]=r[i];
| ~^~~~~~~~~
train.cpp:104:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
104 | for(int i=0;i<u.size();i++)
| ~^~~~~~~~~
train.cpp:129:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
129 | for(int i=0;i<u.size();i++)
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
13 ms |
9344 KB |
3rd lines differ - on the 1st token, expected: '0', found: '1' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
8320 KB |
3rd lines differ - on the 1st token, expected: '0', found: '1' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
15 ms |
9856 KB |
3rd lines differ - on the 1st token, expected: '0', found: '1' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
17 ms |
9344 KB |
3rd lines differ - on the 696th token, expected: '0', found: '1' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
26 ms |
9464 KB |
3rd lines differ - on the 2nd token, expected: '0', found: '1' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
13 ms |
9344 KB |
3rd lines differ - on the 1st token, expected: '0', found: '1' |
2 |
Halted |
0 ms |
0 KB |
- |