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;
using ll = long long;
#define forn(i, n) for(int i = 0; i < n; ++i)
#define sz(a) (int)a.size()
#define pb push_back
#define all(a) a.begin(), a.end()
const int N = 5e5 + 10;
int p[N];
int get(int a) {return p[a] = (a == p[a] ? a : get(p[a]));}
void merge(int a, int b) {
a = get(a), b = get(b);
p[a] = b;
}
vector<pair<int, int>> adj[N];
int cnt[N], s;
vector<int> require[N];
bool vis[N], have[N];
vector<int> vis_moment, have_moment, key_moment;
int mn = INT_MAX;
vector<int> component;
bool paiu = false, return_trick = false;
vector<pair<int, int>> bruh;
void dfs(int u, vector<int>& r) {
if(return_trick) return;
component.pb(u);
//assert(vis[u] == false);
vis[u] = true;
vis_moment.pb(u);
++cnt[s];
if(get(u) != get(s) && !paiu) {
return_trick = true;
//merge(s, u);
bruh.pb({s, u});
return;
}
if(sz(require[r[u]])) {
for(auto x: require[r[u]]) {
if(!vis[x]) {
dfs(x, r);
if(return_trick) return;
}
}
require[r[u]].clear();
}
if(!have[r[u]]) {
have[r[u]] = true;
have_moment.pb(r[u]);
}
for(auto x: adj[u]) {
int v = x.first, c = x.second;
if(!vis[v]) {
if(have[c]) {
dfs(v, r);
if(return_trick) return;
} else {
require[c].pb(v);
key_moment.pb(c);
if(return_trick) return;
}
}
}
}
void solve(int start, vector<int>& r) {
for(auto x: key_moment) require[x].clear();
if(!paiu) for(auto x: vis_moment) vis[x] = false;
for(auto x: have_moment) have[x] = false;
vis_moment.clear();
key_moment.clear();
have_moment.clear();
component.clear();
return_trick = false;
s = start;
dfs(start, r);
}
bool solved[N];
vector<int> find_reachable(vector<int> r, vector<int> u, vector<int> v, vector<int> c) {
for(int i = 0; i < sz(u); ++i) {
adj[u[i]].pb({v[i], c[i]});
adj[v[i]].pb({u[i], c[i]});
}
int n = sz(r);
forn(i, n) p[i] = i;
vector<int> ans(n, 0);
for(int iter = 0; iter < 20; ++iter) {
for(int i = 0; i < n; ++i) {
if(get(i) == i) {
solve(i, r);
}
}
if(!sz(bruh)) break;
for(auto x: bruh) merge(x.first, x.second);
bruh.clear();
}
forn(i, n) cnt[i] = 0;
paiu = true;
for(auto x: vis_moment) vis[x] = false;
vis_moment.clear();
for(int i = 0; i < n; ++i) {
if(!solved[i] && get(i) == i) {
solve(i, r);
for(auto x: component) {
solved[x] = true;
cnt[x] = cnt[i];
}
mn = min(mn, cnt[i]);
}
}
for(int i = 0; i < n; ++i) {
if(solved[i] && cnt[i] == mn) ans[i] = 1;
}
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... |