#ifdef NYAOWO
#include "grader.cpp"
#endif
#include "train.h"
#include <bits/stdc++.h>
#define For(i, a, b) for(int i = a; i <= b; i++)
#define Forr(i, a, b) for(int i = a; i >= b; i--)
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define sz(x) ((int)x.size())
#define eb emplace_back
#define int LL
using namespace std;
using i32 = int32_t;
using LL = long long;
using pii = pair<int, int>;
const int MAXN = 5000;
vector<int> adj[MAXN + 10];
vector<int> rev[MAXN + 10];
int odeg[MAXN + 10];
int alive[MAXN + 10];
int tag[MAXN + 10];
void build_graph(int n) {
For(i, 0, n - 1) {
odeg[i] = 0;
rev[i].clear();
if(!alive[i]) adj[i].clear();
}
For(i, 0, n - 1) if(alive[i]) {
int ptr = 0;
while(ptr < sz(adj[i])) {
if(alive[adj[i][ptr]]) ptr++;
else {
swap(adj[i][ptr], adj[i].back());
adj[i].pop_back();
}
}
}
For(i, 0, n - 1) for(auto &j:adj[i]) {
rev[j].eb(i);
}
}
int walk(int n, vector<i32> a, vector<int> s, int c) {
For(i, 0, n - 1) {
odeg[i] = sz(adj[i]);
tag[i] = 0;
}
queue<int> que;
for(auto &i:s) if(alive[i]) que.emplace(i);
int cnt = 0;
while(!que.empty()) {
int cur = que.front(); que.pop();
if(tag[cur]) continue;
tag[cur] = 1;
cnt++;
for(auto &i:rev[cur]) {
odeg[i]--;
if(a[i] == c) {
que.emplace(i);
} else if(odeg[i] == 0) {
que.emplace(i);
}
}
}
return cnt;
}
vector<i32> who_wins(vector<i32> a, vector<i32> r, vector<i32> u, vector<i32> v) {
int n = sz(a);
int m = sz(u);
For(i, 0, m - 1) {
int x = u[i];
int y = v[i];
adj[x].eb(y);
adj[y].eb(x);
}
For(i, 0, n - 1) alive[i] = 1;
int node_cnt = n;
while(node_cnt) {
build_graph(n);
int cnt2 = walk(n, a, vector<int>(all(r)), 1);
if(cnt2 == node_cnt) break;
vector<int> s;
For(i, 0, n - 1) if(alive[i] && !tag[i]) s.eb(i);
int cnt3 = walk(n, a, s, 0);
For(i, 0, n - 1) alive[i] = alive[i] && (!tag[i]);
node_cnt -= cnt3;
assert(cnt3 > 0);
}
vector<i32> w;
For(i, 0, n - 1) w.eb(alive[i]);
return w;
}
/*
2 4
0 1
1 0
0 0
0 1
1 0
1 1
1 1
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
1364 KB |
3rd lines differ - on the 1st token, expected: '0', found: '1' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
468 KB |
3rd lines differ - on the 1st token, expected: '0', found: '1' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
2388 KB |
3rd lines differ - on the 1st token, expected: '0', found: '1' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
2132 KB |
3rd lines differ - on the 1st token, expected: '1', found: '0' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
2388 KB |
3rd lines differ - on the 1st token, expected: '1', found: '0' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
1364 KB |
3rd lines differ - on the 1st token, expected: '0', found: '1' |
2 |
Halted |
0 ms |
0 KB |
- |