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 "keys.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct dsu {
int n;
vector<int> p, s;
dsu(int n) : n(n), p(n), s(n, 1) {
iota(p.begin(), p.end(), 0);
}
int find(int x) {
return x == p[x] ? x : p[x] = find(p[x]);
}
int size(int x) {
return s[find(x)];
}
bool same(int x, int y) {
return find(x) == find(y);
}
bool unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y) {
return 0;
}
if (s[x] > s[y]) {
swap(x, y);
}
p[x] = y;
s[y] += s[x];
return 1;
}
};
vector<int> find_reachable(vector<int> R, vector<int> U, vector<int> V, vector<int> C) {
int N = R.size(), M = C.size();
if (C == vector(M, 0)) {
dsu uf(N);
for (int i = 0; i < M; i++) {
uf.unite(U[i], V[i]);
}
vector<int> p(N);
for (int i = 0; i < N; i++) {
if (R[i] == 0) {
p[i] = uf.size(i);
} else {
p[i] = 1;
}
}
int mn = *min_element(p.begin(), p.end());
vector<int> ans(N);
for (int i = 0; i < N; i++) {
if (p[i] == mn) {
ans[i] = 1;
}
}
return ans;
}
vector<vector<pair<int, int>>> adj(N);
for (int i = 0; i < M; i++) {
adj[U[i]].emplace_back(V[i], C[i]);
adj[V[i]].emplace_back(U[i], C[i]);
}
vector<int> p(N, N);
auto bfs = [&](int s) {
vector<bool> have(N), vis(N);
vector<vector<int>> w(N);
vector<int> q;
vis[s] = 1;
q.push_back(s);
for (int i = 0; i < q.size(); i++) {
int x = q[i];
have[R[x]] = 1;
for (int y : w[R[x]]) {
if (!vis[y]) {
vis[y] = 1;
q.push_back(y);
}
}
w[R[x]].clear();
for (auto [y, z] : adj[x]) {
if (vis[y]) {
continue;
}
if (have[z]) {
vis[y] = 1;
q.push_back(y);
} else {
w[z].push_back(y);
}
}
}
for (int x : q) {
p[x] = q.size();
}
};
vector<vector<int>> f(N), g(N);
for (int i = 0; i < N; i++) {
for (auto [j, k] : adj[i]) {
if (R[i] == k) {
f[i].push_back(j);
g[j].push_back(i);
}
}
}
vector<int> v;
vector<bool> vis(N);
auto dfs = [&](auto dfs, int x) -> void {
vis[x] = 1;
for (int y : f[x]) {
if (!vis[y]) {
dfs(dfs, y);
}
}
v.push_back(x);
};
for (int i = 0; i < N; i++) {
if (!vis[i]) {
dfs(dfs, i);
}
}
fill(vis.begin(), vis.end(), 0);
vector<int> b(N), t(N);
auto sfd = [&](auto sfd, int x, int z) -> void {
vis[x] = 1;
b[x] = z;
for (int y : g[x]) {
if (!vis[y]) {
sfd(sfd, y, z);
} else if (b[x] != b[y]) {
t[b[y]] = 1;
}
}
};
for (int i = N - 1; i >= 0; i--) {
if (!vis[v[i]]) {
sfd(sfd, v[i], v[i]);
}
}
for (int i = 0; i < N; i++) {
if (b[i] == i && !t[i]) {
bfs(i);
}
}
int mn = *min_element(p.begin(), p.end());
vector<int> ans(N);
for (int i = 0; i < N; i++) {
if (p[i] == mn) {
ans[i] = 1;
}
}
return ans;
}
Compilation message (stderr)
keys.cpp: In lambda function:
keys.cpp:70:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for (int i = 0; i < q.size(); i++) {
| ~~^~~~~~~~~~
# | 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... |