#include "keys.h"
#include <bits/stdc++.h>
using namespace std;
struct DSU {
vector<int> e;
vector<unordered_set<int>> st;
DSU(int n) {
e = vector<int>(n, -1);
st = vector<unordered_set<int>>(n);
}
int get(int x) {
return e[x] < 0 ? x : e[x] = get(e[x]);
}
bool can_unite(int x, int r) {
x = get(x);
return st[x].count(r);
}
void unite(int x, int y) {
x = get(x);
y = get(y);
if (x == y) return;
if (e[x] > e[y]) swap(x, y);
if (st[x].size() > st[y].size()) {
swap(st[x], st[y]);
}
for (auto& u : st[y]) {
st[x].insert(u);
}
e[x] += e[y];
st[y].clear();
e[y] = x;
}
};
std::vector<int> find_reachable(std::vector<int> r, std::vector<int> _u, std::vector<int> _v, std::vector<int> _c) {
int N = r.size();
int M = _u.size();
vector<vector<array<int, 2>>> adj(N);
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]});
}
vector<int> cnt(N);
for (int i = 0; i < N; ++i) {
vector<int> seen(N);
vector<int> now{i};
vector<int> have(N);
have[r[i]] = 1;
while (true) {
bool added = false;
for (auto& v : now) {
for (auto& [u, c] : adj[v]) {
if (seen[u]) continue;
if (have[c]) {
seen[u] = 1;
now.push_back(u);
have[r[u]] = 1;
added = true;
}
}
}
if (!added) break;
}
for (int j = 0; j < N; ++j) {
cnt[i] += seen[j];
}
}
vector<int> res(N);
int mn = *min_element(begin(cnt), end(cnt));
for (int i = 0; i < N; ++i) {
if (mn == cnt[i]) {
res[i] = 1;
}
}
return res;
}
# | 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... |