# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
133423 | icypiggy | 장난감 기차 (IOI17_train) | C++14 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <bitset>
#include <queue>
#include <string.h>
#include <assert.h>
using namespace std;
const int n_max = 6000;
vector<int> backward[n_max]; // edges run in the reverse direction!
bool whowin[n_max][n_max]; // position, then state. true = unknown, false = b wins
int deg[n_max][n_max]; // this is the out degree
void init(vector<int> &u, vector<int> &v) {
memset(deg, 0, sizeof(deg));
for(int i=0; i<u.size(); i++) {
backward[v[i]].push_back(u[i]);
for(int j=0; j<n_max; j++) {
deg[u[i]][j]++;
}
}
}
vector<int> who_wins(vector<int> a, vector<int> r, vector<int> u, vector<int> v) {
init(u,v);
int n = a.size();
queue<pair<int,int>> bfs;
for(int i=0; i<n; i++) {
for(int j=0; j<=n; j++) {
whowin[i][j] = true;
}
if(!r[i]) {
whowin[i][0] = false;
bfs.push(make_pair(i,0));
}
}
while(!bfs.empty()) {
pair<int,int> next = bfs.front();
bfs.pop();
if(r[next.first] && next.second==n) {
for(int i: backward[next.first]) {
if(a[i]) {
for(int j=0; j<=n; j++) {
deg[i][j]--;
if(deg[i][j]==0 && whowin[i][j]) {
whowin[i][j] = false;
bfs.push(make_pair(i,j));
}
}
} else {
for(int j=0; j<=n; j++) {
if(whowin[i][j]) {
whowin[i][j] = false;
bfs.push(make_pair(i,j));
}
}
}
}
} else if (r[next.first]==0) {
for(int i: backward[next.first]) {
int j = min(n, next.second + 1);
if(a[i] && n!=next.second) {
deg[i][j]--;
if(deg[i][j]==0 && whowin[i][j]) {
whowin[i][j] = false;
bfs.push(make_pair(i,j));
}
} else {
if(whowin[i][j]) {
whowin[i][j] = false;
bfs.push(make_pair(i,j));
}
}
}
}
}
vector<int> ans(n,0);
for(int i=0; i<n; i++) {
ans[i] = whowin[i][n];
}
return ans;
}
int main()
{
vector<int> a = {0,1,1,1,0};
vector<int> r = {0,1,1,1,1};
vector<int> u = {0,1,2,3,4};
vector<int> v = {1,2,3,4,4};
vector<int> x = who_wins(a,r,u,v);
for(int i: x) {
cout << i;
}
return 0;
}