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 sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define uniq(x) x.resize(unique(all(x)) - x.begin());
#define dbg(x) cout << #x << " " << x << endl;
#define ll long long
#define ld long double
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<typename T>
void pr(vector<T>& a) {
for(auto i: a) {
cout << i << " ";
}
cout << endl;
}
void pr(vector<pair<int, int>>& a) {
for(auto i: a) {
cout << "{" << i.first << "," << i.second << "}" << " ";
}
cout << endl;
}
void pr(vector<pair<pair<int, int>, int>>& a) {
for(auto i: a) {
cout << "{ {" << i.first.first << "," << i.first.second << "}" << ", " << i.second << "} ";
}
cout << endl;
}
const int N = 4e5 + 10, inf = 1e9;
vector<pair<int, int>> adj[N];
vector<int> color[N];
bool finished[N], vis[N];
int n, m, answ[N], have_color[N];
int color_node[N];
int bfs(int node) {
int count = 1;
queue<int> q; vis[node] = true;
vector<int> path{node}, colors;
q.push(node);
while(!q.empty()) {
auto u = q.front();
q.pop();
if(!have_color[color_node[u]]) {
have_color[color_node[u]] = true;
colors.push_back(color_node[u]);
for(auto i: color[color_node[u]]) {
if(finished[i]) {
{
for(auto j: path) {
have_color[color_node[j]] = false;
vis[j] = false;
}
for(auto j: colors) {
color[j].clear();
}
}
return inf;
}
if(!vis[i]) {
vis[i] = true;
count++, path.push_back(i);
q.push(i);
}
} color[color_node[u]].clear();
}
for(auto i: adj[u]) {
if(vis[i.first]) continue;
if(!have_color[i.second]) {
colors.push_back(i.second);
color[i.second].push_back(i.first);
continue;
}
if(finished[i.first]) {
{
for(auto j: path) {
have_color[color_node[j]] = false;
vis[j] = false;
}
for(auto j: colors) {
color[j].clear();
}
}
return inf;
}
count++, path.push_back(i.first);
vis[i.first] = true;
q.push(i.first);
}
}
for(auto j: path) answ[j] = min(answ[j], count);
{
for(auto j: path) {
vis[j] = false;
}
for(auto j: colors) {
have_color[j] = false;
color[j].clear();
}
}
return count;
}
vector<int> find_reachable(vector<int> r, vector<int> u, vector<int> v, vector<int> c) {
n = sz(r), m = sz(u);
vector<int> seq;
for(int i = 0; i < m; i++) {
adj[u[i]].push_back({v[i], c[i]});
adj[v[i]].push_back({u[i], c[i]});
seq.push_back(u[i]);
seq.push_back(v[i]);
}
for(int i = 0; i < n; i++) {
color_node[i] = r[i];
answ[i] = inf;
}
shuffle(all(seq), rng);
// pr(seq);
for(auto i: seq) {
if(!finished[i]) {
int pat = bfs(i);
answ[i] = min(answ[i], pat);
finished[i] = true;
}
}
for(int i = 0; i < n; i++) {
if(!finished[i]) {
int pat = bfs(i);
answ[i] = min(answ[i], pat);
finished[i] = true;
}
}
int mini = inf;
for(int i = 0; i < n; i++) {
mini = min(mini, answ[i]);
}
vector<int> ans(n);
for(int i = 0; i < n; i++) {
if(answ[i] == mini) {
ans[i] = 1;
} else {
ans[i] = 0;
}
}
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... |