이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ar array
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
using namespace std;
template<typename T> bool ckmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template<typename T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
struct dsu {
vector<int> p, size, got;
dsu(int n, const vector<int>& r) : p(n), size(n, 1), got(n) {
iota(all(p), 0);
for (int i = 0; i < n; i++) got[i] = 1 << r[i];
}
int get(int v) {
return v == p[v] ? v : p[v] = get(p[v]);
}
bool merge(int a, int b) {
a = get(a), b = get(b);
if (a == b) return 0;
if (size[a] < size[b]) swap(a, b);
size[a] += size[b], got[a] |= got[b], p[b] = a;
return 1;
}
int val(int v) {
return got[get(v)];
}
};
std::vector<int> find_reachable(std::vector<int> r, std::vector<int> u, std::vector<int> v, std::vector<int> c) {
int n = sz(r), m = sz(u);
vector<vector<ar<int, 2>>> adj(n);
vector<vector<ar<int, 2>>> edges(30);
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]});
edges[c[i]].push_back({u[i], v[i]});
}
set<int> vis;
queue<int> q;
for (int i = 0; i < 30; i++) {
vis.insert(1 << i);
q.push(1 << i);
}
int mn = 1e9;
vector<int> ans(n);
while (q.size()) {
int col = q.front(); q.pop();
dsu d(n, r);
for (int i = 0; i < 30; i++) if (col >> i & 1) {
for (auto [x, y] : edges[i]) {
d.merge(x, y);
}
}
for (int i = 0; i < n; i++) if (i == d.get(i) && !vis.count(d.val(i))) {
vis.insert(d.val(i));
q.push(d.val(i));
}
int o = mn;
for (int i = 0; i < n; i++) if (i == d.get(i) && (d.val(i) & col) == d.val(i)) ckmin(mn, d.size[i]);
if (mn < o) {
for (int i = 0; i < n; i++) {
ans[i] = (d.val(i) & col) == d.val(i) && d.size[d.get(i)] == mn;
}
}
else if (mn == o) {
for (int i = 0; i < n; i++) {
ans[i] |= (d.val(i) & col) == d.val(i) && d.size[d.get(i)] == mn;
}
}
}
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... |