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>
using namespace std;
#define ll long long
#define fo(i, n) for(ll i = 0; i<(n); i++)
#define F first
#define S second
#define pb push_back
#define deb(x) cout << #x << " = " << (x) << endl
#define deb2(x, y) cout << #x << " = " << (x) << ", " << #y << " = " << (y) << endl
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> pl;
typedef vector<pl> vpl;
ll n;
vl topo;
int seen[6000];
vvl adj(6000);
vvl adjr(6000);
vvl adjR(6000);
void toposort(int u){
if(seen[u]) return;
seen[u] = 1;
for(auto &v : adjr[u]){
toposort(v);
}
topo.pb(u);
}
int c = 0;
int dfs(int u){
if(seen[u]) return 0;
seen[u] = c;
ll ans = 1;
for(auto &v : adj[u]){
ans+=dfs(v);
}
return ans;
}
vector<int> res;
void dfs2(int u){
if(res[u]) return;
res[u] = 1;
for(auto &v : adjR[u]){
dfs2(v);
}
}
std::vector<int> who_wins(std::vector<int> a, std::vector<int> r, std::vector<int> u, std::vector<int> v) {
n = a.size();
ll m = u.size();
vl selfEdge(n+1, 0);
fo(i, m){
if(u[i] == v[i]) selfEdge[u[i]] = 1;
else{
adj[u[i]].pb(v[i]);
adjr[v[i]].pb(u[i]);
adjR[v[i]].pb(u[i]);
}
}
memset(seen, 0, sizeof(seen));
fo(i, n){
if(seen[i]) continue;
toposort(i);
}
reverse(all(topo));
memset(seen, 0, sizeof(seen));
vl good;
fo(i, n){
if(seen[topo[i]]) continue;
c++;
ll siz = dfs(topo[i]);
// deb2(topo[i], siz);
if(siz>1 || selfEdge[topo[i]]) good.pb(topo[i]);
}
vector<int> bla(n+1, 0);
fo(i, r.size()){
if(r[i]){
// deb(seen[i]);
bla[seen[i]] = 1;
}
}
// fo(i, n+1) deb(bla[i]);
res.assign(n, 0);
fo(i, good.size()){
// deb(good[i]);
if(bla[seen[good[i]]]){
// deb(seen[good[i]]);
dfs2(good[i]);
}
}
return res;
}
Compilation message (stderr)
train.cpp: In function 'std::vector<int> who_wins(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
train.cpp:5:33: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
5 | #define fo(i, n) for(ll i = 0; i<(n); i++)
| ^
train.cpp:89:5: note: in expansion of macro 'fo'
89 | fo(i, r.size()){
| ^~
train.cpp:5:33: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
5 | #define fo(i, n) for(ll i = 0; i<(n); i++)
| ^
train.cpp:98:5: note: in expansion of macro 'fo'
98 | fo(i, good.size()){
| ^~
# | 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... |