# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
641998 | QwertyPi | Toy Train (IOI17_train) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define fi first
#define se second
#define hkoi
#ifndef hkoi
#include "train.h"
#endif
using namespace std;
const int N = 5001, M = 20001;
int a[N];
int n, m;
vector<int> G[N], H[N];
std::vector<int> who_wins(std::vector<int> a, std::vector<int> r, std::vector<int> u, std::vector<int> v) {
::n = a.size(), ::m = u.size();
std::vector<int> win(n, 0), charged(n, 0), ans(n, 0), cnt(n, 0);
for(int i = 0; i < m; i++){
G[u[i]].push_back(v[i]);
H[v[i]].push_back(u[i]);
}
fill(win.begin(), win.end(), 0);
fill(cnt.begin(), cnt.end(), 0);
priority_queue<pair<int, int>> pq;
for(int j = 0; j < n; j++){
if(r[j] == 1){
charged[j] = true;
for(auto k : G[j]){
cnt[k]++; pq.push({(a[k] << 16) + cnt[k], k});
}
}
}
while(pq.size()){
pair<int, int> t = pq.top(); pq.pop();
int v = t.fi & ((1 << 16) - 1), c = t.se;
if(win[v]) continue;
if(t.fi >= G[v].size()) {
charged[v] = true;
for(auto k : G[v]){
cnt[k]++; pq.push({(a[k] << 16) + cnt[k], k});
}
}
}
for(int u = 0; u < n; u++){
bool exist = false, all = true;
for(auto v : G[u]){
if(charged[v]) exist = true;
if(!charged[v]) all = false;
}
ans[u] = a[u] ? exist : all;
}
return ans;
}
#ifdef hkoi
int main(){
int n, m; cin >> n >> m;
vector<int> a; for(int i = 0; i < n; i++) {
int v; cin >> v; a.push_back(v);
}
vector<int> r; for(int i = 0; i < n; i++) {
int v; cin >> v; r.push_back(v);
}
vector<int> u, v; for(int i = 0; i < m; i++){
int x, y; cin >> x >> y;
u.push_back(x); v.push_back(y);
}
vector<int> w = who_wins(a, r, u, v);
for(int i = 0; i < n; i++){
cout << w[i] << ' ';
}
cout << '\n';
}
#endif