#include <bits/stdc++.h>
#define ll long long
#define sz(x) int(x.size())
#define forn(i, n) for (i = 0; i < n; i++)
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define fr first
#define se second
using namespace std;
const int MAXN = 1e5 + 1;
vector<ll> grafo[MAXN], grafoINV[MAXN], topo;
set<ll> gComps[MAXN];
bool vis[MAXN], act[MAXN];
ll comp[MAXN], actC, ap[MAXN], tam[MAXN];
vector<int> ans, A, R;
void dfs(ll nod)
{
vis[nod] = 1;
for (auto k : grafo[nod])
{
if (vis[k])
continue;
dfs(k);
}
topo.pb(nod);
}
void dfs2(ll nod)
{
tam[actC]++;
comp[nod] = actC;
if (R[nod])
act[actC] = 1;
for (auto k : grafoINV[nod])
{
if (k == nod)
tam[actC]++;
if (comp[k])
continue;
dfs2(k);
}
}
bool dfs3(ll nod)
{
vis[nod] = 1;
for (auto k : gComps[nod])
{
if (!vis[k])
act[nod] = act[nod] | dfs3(k);
else
act[nod] = act[nod] | act[k];
}
return act[nod];
}
std::vector<int> who_wins(std::vector<int> a, std::vector<int> r, std::vector<int> u, std::vector<int> v)
{
ll i, n = sz(a), m = sz(u);
A = a;
R.resize(n, 1);
ans.resize(n, 0);
for (i = 0; i < m; i++)
{
if (r[v[i]] || r[u[i]])
continue;
grafo[u[i]].pb(v[i]);
grafoINV[v[i]].pb(u[i]);
}
auto f = [&]()
{
for (i = 0; i < n; i++)
if (vis[i] == 0)
dfs(i);
reverse(all(topo));
for (i = 0; i < sz(topo); i++)
if (!comp[topo[i]])
{
actC++;
dfs2(topo[i]);
if (tam[actC] <= 1)
act[actC] = 0;
}
for (i = 0; i < n; i++)
for (auto k : grafo[i])
if (comp[i] != comp[k])
{
gComps[comp[i]].insert(comp[k]);
ap[comp[k]];
}
memset(vis, 0, sizeof(vis));
for (i = 1; i <= actC; i++)
if (!ap[i])
dfs3(i);
};
f();
R.resize(0);
R.resize(n, 0);
for (i = 0; i < n; i++)
R[i] = act[comp[i]];
topo.resize(0);
memset(vis, 0, sizeof(vis));
memset(tam, 0, sizeof(tam));
memset(ap, 0, sizeof(ap));
memset(act, 0, sizeof(act));
memset(comp, 0, sizeof(comp));
actC = 0;
for (i = 0; i < n; i++)
{
grafo[i].resize(0);
grafoINV[i].resize(0);
gComps[i].clear();
}
for (i = 0; i < m; i++)
{
grafo[u[i]].pb(v[i]);
grafoINV[v[i]].pb(u[i]);
}
f();
for (i = 0; i < n; i++)
ans[i] = !act[comp[i]];
return ans;
}
| # | 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... |