# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1058745 | qwusha | Keys (IOI21_keys) | C++17 | 0 ms | 0 KiB |
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;
typedef long long ll;
#define fi first
#define se second
typedef long double ld;
const ld eps = 1e-8;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
const ll inf = 1e18;
const ll mod = 1e9 + 7;
#include "keys.h"
vector<set<long long>> g;
vector<vector<pair<long long, long long>>> gr;
vector<long long> num;
long long cnt = 0;
void dfs(long long v, long long number) {
num[v] = number;
cnt++;
for (auto u : g[v]) {
if (num[u] == -1) {
dfs(u, number);
}
}
}
vector<long long> find_reachable(vector<long long> r, vector<long long> fir, vector<long long>sec, vector<long long> c) {
bool first = 1;
ll n = r.size();
long long m = fir.size();
for (long long i = 0; i < m; i++) {
if (c[i] != 0)
first = 0;
}
if (!first) {
gr.resize(n);
for (long long i = 0; i < m; i++) {
gr[fir[i]].push_back({sec[i], c[i]});
gr[sec[i]].push_back({fir[i], c[i]});
}
vector<long long> p(n);
for (long long i = 0; i < n; i++) {
set<long long> keys = {r[i]};
set<long long> pos = {i};
vector<long long> used(n, 0);
vector<set<long long>> reach(n);
while (!pos.empty()) {
auto v = *pos.begin();
pos.erase(pos.begin());
p[i]++;
used[v] = 1;
if (keys.find(r[v]) == keys.end()) {
keys.insert(r[v]);
for (auto nod : reach[r[v]]) {
if (!used[nod])
pos.insert(nod);
}
}
for (auto [u, w] : gr[v]) {
if (!used[u]) {
if (keys.find(w) != keys.end()) {
pos.insert(u);
} else {
reach[w].insert(u);
}
}
}
}
}
long long mini = n + 1;
for (long long i = 0; i < n; i++) {
mini = min(mini, p[i]);
}
vector<long long> res(n);
for (long long i = 0; i < n; i++) {
if (p[i] == mini)
res[i] = 1;
cout << p[i] << ' ';
}
cout << endl;
return res;
}
g.resize(n);
for (long long i = 0; i < m; i++) {
g[fir[i]].insert(sec[i]);
g[sec[i]].insert(fir[i]);
}
vector<long long> res(n);
bool nonzer = 0;
for (long long i = 0; i < n; i++) {
if (r[i] != 0)
nonzer = 1;
}
num.resize(n, -1);
vector<long long> sz;
long long mini = n + 1;
for (long long i = 0; i < n; i++) {
if (num[i] == -1) {
cnt = 0;
dfs(i, sz.size());
sz.push_back(cnt);
mini = min(mini, cnt);
}
}
for (long long i = 0; i < n; i++) {
if (nonzer) {
if (r[i] != 0 || sz[num[i]] == 1) {
res[i] = 1;
}
} else {
if (sz[num[i]] == mini) {
res[i] = 1;
}
}
}
return res;
}
/*
long long main() {
vector<long long> r = {0, 1, 1, 2, 2, 1, 2},
u = {0, 0, 1, 1, 2, 3, 3, 4, 4, 5},
v = {1, 2, 2, 3, 3, 4, 5, 5, 6, 6},
c = {0, 0, 1, 0, 0, 1, 2, 0, 2, 1};
auto res = find_reachable(r,u,v,c);
for (long long i = 0; i < r.size(); i++) {
cout << res[i] << ' ';
}
}
*/