This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;
#ifndef WAIMAI
#include "keys.h"
#endif
#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout(T t, U...u) {cout << t << (sizeof...(u) ? ", " : ""), dout(u...);}
#else
#define debug(...) 7122
#endif
#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second
const int SIZE = 3e5 + 5;
int n, m;
vector<pair<int, int>> adj[SIZE];
vector<int> cand, done;
vector<int> all[SIZE];
unordered_set<int> us[SIZE];
int to[SIZE], g[SIZE], vs[SIZE];
void deal() {
fill(to, to + n, -1);
for (int x : cand) {
int tag = 0;
for (int pos : all[x]) if (tag == 0) for (auto [np, c] : adj[pos]) if (us[x].count(c)) {
if (g[np] == -1) {
tag = 1;
to[x] = -1;
break;
}
if (g[np] != x) {
tag = 2;
to[x] = g[np];
break;
}
}
if (tag <= 1) {
if (tag == 0) done.pb(x);
for (int pos : all[x]) g[pos] = -1;
all[x].clear();
unordered_set<int> foo;
swap(us[x], foo);
}
}
fill(vs, vs + n, 0);
vector<int> tmp;
for (int x : cand) if (to[x] != -1 && !vs[x]) {
vector<int> chain, cyc;
chain.pb(x), vs[x] = 1, x = to[x];
while (!vs[x]) {
chain.pb(x);
vs[x] = 1;
x = to[x];
}
if (vs[x] == 2) {
for (int x : chain) {
vs[x] = 2;
for (int pos : all[x]) g[pos] = -1;
all[x].clear();
unordered_set<int> foo;
swap(us[x], foo);
}
continue;
}
for (int x : chain) vs[x] = 2;
FOR (i, 0, chain.size() - 1) if (chain[i] == x) {
FOR (j, 0, i - 1) {
int x = chain[j];
for (int pos : all[x]) g[pos] = -1;
all[x].clear();
unordered_set<int> foo;
swap(us[x], foo);
}
FOR (j, i + 1, chain.size() - 1) {
int y = chain[j];
for (int pos : all[y]) {
g[pos] = x;
all[x].pb(pos);
}
all[y].clear();
for (int c : us[y]) us[x].insert(c);
unordered_set<int> foo;
swap(us[y], foo);
}
tmp.pb(x);
break;
}
}
cand = tmp;
}
vector<int> reach(int s, vector<int> &r) {
unordered_set<int> us;
vector<int> all;
unordered_map<int, vector<int>> wait;
queue<int> q;
q.push(s), us.insert(r[s]), vs[s] = 1;
while (q.size()) {
int pos = q.front();
q.pop();
all.pb(pos);
for (auto [np, c] : adj[pos]) if (!vs[np]) {
if (us.count(c)) {
vs[np] = 1;
q.push(np);
if (!us.count(r[np])) {
us.insert(r[np]);
for (int x : wait[r[np]]) if (!vs[x]) {
vs[x] = 1;
q.push(x);
}
wait[r[np]].clear();
}
} else {
wait[c].pb(np);
}
}
}
return all;
}
vector<int> find_reachable(vector<int> r, vector<int> u, vector<int> v, vector<int> c) {
n = r.size(), m = u.size();
FOR (i, 0, m - 1) {
adj[u[i]].pb(v[i], c[i]);
adj[v[i]].pb(u[i], c[i]);
}
cand.resize(n);
iota(cand.begin(), cand.end(), 0);
FOR (i, 0, n - 1) {
g[i] = i;
all[i].pb(i);
us[i].insert(r[i]);
}
while (cand.size()) deal();
int mn = n + 1;
vector<int> ans(n), ansp;
fill(vs, vs + n, 0);
for (int s : done) {
auto v = reach(s, r);
if (v.size() < mn) {
mn = v.size();
ansp.clear();
}
if (v.size() == mn) ansp.insert(ansp.end(), v.begin(), v.end());
}
for (int x : ansp) ans[x] = 1;
return ans;
}
#ifdef WAIMAI
int main() {
int n, m;
assert(2 == scanf("%d%d", &n, &m));
vector<int> r(n), u(m), v(m), c(m);
for(int i=0; i<n; i++) {
assert(1 == scanf("%d", &r[i]));
}
for(int i=0; i<m; i++) {
assert(3 == scanf("%d %d %d", &u[i], &v[i], &c[i]));
}
fclose(stdin);
vector<int> ans = find_reachable(r, u, v, c);
for (int i = 0; i < (int) ans.size(); ++i) {
if(i) putchar(' ');
putchar(ans[i]+'0');
}
printf("\n");
return 0;
}
#endif
Compilation message (stderr)
keys.cpp: In function 'std::vector<int> find_reachable(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
keys.cpp:156:22: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
156 | if (v.size() < mn) {
| ~~~~~~~~~^~~~
keys.cpp:160:22: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
160 | if (v.size() == mn) ansp.insert(ansp.end(), v.begin(), v.end());
| ~~~~~~~~~^~~~~
# | 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... |