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 "train.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uset = unordered_set<int>;
const int nmax = 5e3 + 5;
vector<int> g[nmax], t[nmax];
vector<int> own, charge, rez, degree;
int n, m;
int cnt = 0;
void solve(uset& V) {
if(V.size() == 0)
return;
uset S;
for(auto x : V) {
if(charge[x])
S.insert(x);
for(int i = 0; i < g[x].size(); i++) {
if(V.count(g[x][i]) == 0)
swap(g[x][i], g[x].back()), g[x].pop_back(), i--;
}
for(int i = 0; i < t[x].size(); i++) {
if(V.count(t[x][i]) == 0)
swap(t[x][i], t[x].back()), t[x].pop_back(), i--;
}
}
if(S.size() == 0) {
for(auto x : V)
rez[x] = 0;
return;
}
for(auto x : V) {
if(charge[x])
degree[x] = 0;
if(own[x] == 1)
degree[x] = 1;
else
degree[x] = g[x].size();
}
queue<int> que;
function<void()> markall = [&]() {
while (!que.empty()) {
int node = que.front();
for(auto x : t[node]) {
degree[x]--;
if(degree[x] == 0 && !S.count(x))
S.insert(x),
que.push(x);
}
que.pop();
}
que = queue<int>();
return;
};
for(auto x : S)
que.push(x);
markall();
if(S.size() == V.size()) {
for(auto x : V)
rez[x] = 1;
return;
}
uset notS;
for(auto x : V)
if(S.count(x) == 0)
notS.insert(x);
for(auto x : V) {
if(notS.count(x) == 1)
degree[x] = 0;
if(own[x] == 0)
degree[x] = 1;
else
degree[x] = g[x].size();
}
swap(S, notS);
for(auto x : S)
que.push(x);
markall();
for(auto x : S)
rez[x] = 0, // formality
V.erase(x);
solve(V);
}
std::vector<int> who_wins(std::vector<int> A, std::vector<int> R, std::vector<int> U, std::vector<int> V) {
own = move(A);
charge = move(R);
n = own.size();
m = U.size();
degree.resize(n, 0);
rez.resize(n, 0);
for(int i = 0; i < m; i++)
g[U[i]].push_back(V[i]),
t[V[i]].push_back(U[i]);
uset Vertex;
for(int i = 0; i < n; i++)
Vertex.insert(i);
solve(Vertex);
return rez;
}
Compilation message (stderr)
train.cpp: In function 'void solve(uset&)':
train.cpp:22:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for(int i = 0; i < g[x].size(); i++) {
| ~~^~~~~~~~~~~~~
train.cpp:26:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | for(int i = 0; i < t[x].size(); i++) {
| ~~^~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |