#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<bool> vb;
typedef tuple<ll,ll,ll> PP;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define REP(i,k,n) for(ll i=(ll)(k);i<(ll)(n);i++)
#define all(a) a.begin(),a.end()
#define lb(v,k) (lower_bound(all(v),k)-v.begin())
#define fi first
#define se second
#define pb emplace_back
template<class T> void out(T a){cout<<a<<endl;}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<endl;}
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
const ll inf=1001001001001001001;
#include "train.h"
std::vector<int> who_wins(std::vector<int> a, std::vector<int> r, std::vector<int> u, std::vector<int> v) {
ll n=a.size();
vvi g(n),rg(n);
rep(i,u.size()){
g[u[i]].pb(v[i]);
rg[v[i]].pb(u[i]);
}
vb flag(n,false);
while(true){
rep(i,n)flag[i]=false;
queue<ll> que;
rep(s,n)if(r[s]){
flag[s]=true;que.push(s);
}
vi cnt_out(n);
rep(i,n)cnt_out[i]=g[i].size();
while(!que.empty()){
ll i=que.front();que.pop();
for(ll x:rg[i])if(!flag[x]){
cnt_out[x]--;
if(a[x]==1||cnt_out[x]==0){
flag[x]=true;que.push(x);
}
}
}
rep(s,n)if(!flag[s])que.push(s);
rep(i,n)cnt_out[i]=g[i].size();
bool upd=false;
while(!que.empty()){
ll i=que.front();que.pop();
if(r[i]==1){
upd=true;r[i]=0;
}
for(ll x:rg[i])if(flag[x]){
cnt_out[x]--;
if(a[x]==0||cnt_out[x]==0){
flag[x]=false;que.push(x);
}
}
}
if(!upd)break;
}
vector<int> res(a.size());
rep(i,n)res[i]=flag[i];
return res;
}
std::vector<int> who_wins3(std::vector<int> a, std::vector<int> r, std::vector<int> u, std::vector<int> v) {
ll n=a.size();
vvi g(n),rg(n);
rep(i,u.size()){
g[u[i]].pb(v[i]);
rg[v[i]].pb(u[i]);
}
vb flag(n,false);
rep(s,n)if(r[s]){
vb tmp(n,false);
queue<ll> que;que.push(s);
while(!que.empty()){
ll i=que.front();que.pop();
for(ll x:g[i])if(!tmp[x]){
tmp[x]=true;que.push(x);
}
}
if(tmp[s])flag[s]=true;
}
queue<ll> que;
rep(i,n)if(flag[i])que.push(i);
while(!que.empty()){
ll i=que.front();que.pop();
for(ll x:rg[i])if(!flag[x]){
flag[x]=true;que.push(x);
}
}
vector<int> res(a.size());
rep(i,n)res[i]=flag[i];
return res;
}
/*
int main(){
rep(tt,10000){
ll n=rand()%5+2;
vector<int> u,v;
rep(i,n){
ll k=rand()%n+1;
set<ll> S;
rep(j,k){
S.insert(rand()%n);
}
for(ll x:S){
u.pb(i);v.pb(x);
}
}
vector<int> a(n),r(n);
rep(i,n)a[i]=1;
rep(i,n)r[i]=rand()%2;
r[0]=1;
vector<int> res3=who_wins3(a,r,u,v);
vector<int> res4=who_wins4(a,r,u,v);
if(res3!=res4){
out(tt);
outv(a);outv(r);outv(u);outv(v);
outv(res3);outv(res4);break;
}
}
}
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
980 KB |
Output is correct |
2 |
Correct |
6 ms |
1112 KB |
Output is correct |
3 |
Correct |
5 ms |
1176 KB |
Output is correct |
4 |
Correct |
5 ms |
1108 KB |
Output is correct |
5 |
Correct |
5 ms |
1108 KB |
Output is correct |
6 |
Correct |
5 ms |
1108 KB |
Output is correct |
7 |
Correct |
5 ms |
1108 KB |
Output is correct |
8 |
Correct |
5 ms |
1108 KB |
Output is correct |
9 |
Correct |
5 ms |
1104 KB |
Output is correct |
10 |
Correct |
4 ms |
1108 KB |
Output is correct |
11 |
Correct |
4 ms |
1108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
296 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
304 KB |
Output is correct |
13 |
Correct |
1 ms |
300 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
1600 KB |
Output is correct |
2 |
Correct |
143 ms |
1580 KB |
Output is correct |
3 |
Correct |
214 ms |
1564 KB |
Output is correct |
4 |
Correct |
8 ms |
1620 KB |
Output is correct |
5 |
Correct |
9 ms |
1620 KB |
Output is correct |
6 |
Correct |
9 ms |
1636 KB |
Output is correct |
7 |
Correct |
8 ms |
1492 KB |
Output is correct |
8 |
Correct |
8 ms |
1492 KB |
Output is correct |
9 |
Correct |
7 ms |
1492 KB |
Output is correct |
10 |
Correct |
7 ms |
1492 KB |
Output is correct |
11 |
Correct |
8 ms |
1492 KB |
Output is correct |
12 |
Correct |
7 ms |
1496 KB |
Output is correct |
13 |
Correct |
8 ms |
1620 KB |
Output is correct |
14 |
Correct |
7 ms |
1620 KB |
Output is correct |
15 |
Correct |
7 ms |
1620 KB |
Output is correct |
16 |
Correct |
7 ms |
1620 KB |
Output is correct |
17 |
Correct |
7 ms |
1560 KB |
Output is correct |
18 |
Correct |
304 ms |
1156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1364 KB |
Output is correct |
2 |
Correct |
8 ms |
1364 KB |
Output is correct |
3 |
Correct |
8 ms |
1520 KB |
Output is correct |
4 |
Correct |
9 ms |
1492 KB |
Output is correct |
5 |
Correct |
7 ms |
1528 KB |
Output is correct |
6 |
Correct |
10 ms |
1492 KB |
Output is correct |
7 |
Correct |
8 ms |
1492 KB |
Output is correct |
8 |
Correct |
7 ms |
1492 KB |
Output is correct |
9 |
Correct |
7 ms |
1384 KB |
Output is correct |
10 |
Correct |
8 ms |
1640 KB |
Output is correct |
11 |
Correct |
8 ms |
1620 KB |
Output is correct |
12 |
Correct |
8 ms |
1652 KB |
Output is correct |
13 |
Correct |
8 ms |
1612 KB |
Output is correct |
14 |
Correct |
7 ms |
1412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
1520 KB |
Output is correct |
2 |
Correct |
8 ms |
1620 KB |
Output is correct |
3 |
Correct |
8 ms |
1516 KB |
Output is correct |
4 |
Correct |
7 ms |
1492 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
4 ms |
980 KB |
Output is correct |
7 |
Correct |
5 ms |
1116 KB |
Output is correct |
8 |
Correct |
5 ms |
1108 KB |
Output is correct |
9 |
Correct |
5 ms |
1208 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
5 ms |
1108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
980 KB |
Output is correct |
2 |
Correct |
6 ms |
1112 KB |
Output is correct |
3 |
Correct |
5 ms |
1176 KB |
Output is correct |
4 |
Correct |
5 ms |
1108 KB |
Output is correct |
5 |
Correct |
5 ms |
1108 KB |
Output is correct |
6 |
Correct |
5 ms |
1108 KB |
Output is correct |
7 |
Correct |
5 ms |
1108 KB |
Output is correct |
8 |
Correct |
5 ms |
1108 KB |
Output is correct |
9 |
Correct |
5 ms |
1104 KB |
Output is correct |
10 |
Correct |
4 ms |
1108 KB |
Output is correct |
11 |
Correct |
4 ms |
1108 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
296 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
304 KB |
Output is correct |
24 |
Correct |
1 ms |
300 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
76 ms |
1600 KB |
Output is correct |
33 |
Correct |
143 ms |
1580 KB |
Output is correct |
34 |
Correct |
214 ms |
1564 KB |
Output is correct |
35 |
Correct |
8 ms |
1620 KB |
Output is correct |
36 |
Correct |
9 ms |
1620 KB |
Output is correct |
37 |
Correct |
9 ms |
1636 KB |
Output is correct |
38 |
Correct |
8 ms |
1492 KB |
Output is correct |
39 |
Correct |
8 ms |
1492 KB |
Output is correct |
40 |
Correct |
7 ms |
1492 KB |
Output is correct |
41 |
Correct |
7 ms |
1492 KB |
Output is correct |
42 |
Correct |
8 ms |
1492 KB |
Output is correct |
43 |
Correct |
7 ms |
1496 KB |
Output is correct |
44 |
Correct |
8 ms |
1620 KB |
Output is correct |
45 |
Correct |
7 ms |
1620 KB |
Output is correct |
46 |
Correct |
7 ms |
1620 KB |
Output is correct |
47 |
Correct |
7 ms |
1620 KB |
Output is correct |
48 |
Correct |
7 ms |
1560 KB |
Output is correct |
49 |
Correct |
304 ms |
1156 KB |
Output is correct |
50 |
Correct |
6 ms |
1364 KB |
Output is correct |
51 |
Correct |
8 ms |
1364 KB |
Output is correct |
52 |
Correct |
8 ms |
1520 KB |
Output is correct |
53 |
Correct |
9 ms |
1492 KB |
Output is correct |
54 |
Correct |
7 ms |
1528 KB |
Output is correct |
55 |
Correct |
10 ms |
1492 KB |
Output is correct |
56 |
Correct |
8 ms |
1492 KB |
Output is correct |
57 |
Correct |
7 ms |
1492 KB |
Output is correct |
58 |
Correct |
7 ms |
1384 KB |
Output is correct |
59 |
Correct |
8 ms |
1640 KB |
Output is correct |
60 |
Correct |
8 ms |
1620 KB |
Output is correct |
61 |
Correct |
8 ms |
1652 KB |
Output is correct |
62 |
Correct |
8 ms |
1612 KB |
Output is correct |
63 |
Correct |
7 ms |
1412 KB |
Output is correct |
64 |
Correct |
8 ms |
1520 KB |
Output is correct |
65 |
Correct |
8 ms |
1620 KB |
Output is correct |
66 |
Correct |
8 ms |
1516 KB |
Output is correct |
67 |
Correct |
7 ms |
1492 KB |
Output is correct |
68 |
Correct |
1 ms |
340 KB |
Output is correct |
69 |
Correct |
4 ms |
980 KB |
Output is correct |
70 |
Correct |
5 ms |
1116 KB |
Output is correct |
71 |
Correct |
5 ms |
1108 KB |
Output is correct |
72 |
Correct |
5 ms |
1208 KB |
Output is correct |
73 |
Correct |
2 ms |
596 KB |
Output is correct |
74 |
Correct |
5 ms |
1108 KB |
Output is correct |
75 |
Correct |
131 ms |
1748 KB |
Output is correct |
76 |
Correct |
173 ms |
1788 KB |
Output is correct |
77 |
Correct |
242 ms |
1768 KB |
Output is correct |
78 |
Correct |
236 ms |
1776 KB |
Output is correct |
79 |
Correct |
242 ms |
1772 KB |
Output is correct |
80 |
Correct |
8 ms |
1720 KB |
Output is correct |
81 |
Correct |
8 ms |
1720 KB |
Output is correct |
82 |
Correct |
8 ms |
1720 KB |
Output is correct |
83 |
Correct |
10 ms |
1720 KB |
Output is correct |
84 |
Correct |
10 ms |
1720 KB |
Output is correct |
85 |
Correct |
8 ms |
1848 KB |
Output is correct |
86 |
Correct |
9 ms |
1848 KB |
Output is correct |
87 |
Correct |
8 ms |
1748 KB |
Output is correct |
88 |
Correct |
11 ms |
1816 KB |
Output is correct |
89 |
Correct |
9 ms |
1728 KB |
Output is correct |
90 |
Correct |
14 ms |
1808 KB |
Output is correct |
91 |
Correct |
14 ms |
1756 KB |
Output is correct |
92 |
Correct |
22 ms |
1852 KB |
Output is correct |
93 |
Correct |
23 ms |
1748 KB |
Output is correct |
94 |
Correct |
22 ms |
1828 KB |
Output is correct |
95 |
Correct |
22 ms |
1844 KB |
Output is correct |
96 |
Correct |
63 ms |
1688 KB |
Output is correct |
97 |
Correct |
327 ms |
1852 KB |
Output is correct |
98 |
Correct |
478 ms |
1808 KB |
Output is correct |
99 |
Correct |
537 ms |
1892 KB |
Output is correct |
100 |
Correct |
301 ms |
1276 KB |
Output is correct |