# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|
435016 | | grt | Keys (IOI21_keys) | C++17 | | 2603 ms | 203368 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>
#define ST first
#define ND second
#define PB push_back
using namespace std;
using ll = long long;
using pi = pair<int,int>;
using vi = vector<int>;
const int nax = 300 * 1000 + 10, INF = 1e9;
int n, m, nr;
map<int, vi>V[nax];
bool vis[2 * nax];
vi seen;
vi G[2 * nax];
vi Grev[2 * nax];
int topo[2 * nax];
int SCC[2 * nax];
bool ok[2 * nax];
int color;
int cc[nax];
int cnt[2 * nax], rep[2 * nax];
bool visited[nax];
bool done[nax];
vi verToAC[nax];
vector<pi>V2[nax];
void dfs(int x, int col) {
vis[x] = true;
seen.PB(x);
for(auto ed : V[col][x]) if(!vis[ed]) {
dfs(ed, col);
}
}
void dfs2(int x) {
vis[x] = true;
for(int nbh : G[x]) {
if(!vis[nbh]) {
dfs2(nbh);
}
}
topo[nr++] = x;
}
void dfs3(int x) {
vis[x] = true;
SCC[x] = color;
if(x < n) rep[color] = x;
if(x < n) cnt[color]++;
for(int nbh : Grev[x]) if(!vis[nbh]) {
dfs3(nbh);
}
}
queue<int>Q;
vi saw, sawC;
void dfss(int x) {
visited[x] = true;
saw.PB(x);
//cout << "VER: " << x << "\n";
Q.push(cc[x]);
for(auto nbh : V2[x]) if(!visited[nbh.ST]) {
if(done[nbh.ND]) {
dfss(nbh.ST);
} else {
verToAC[nbh.ND].PB(nbh.ST);
sawC.PB(nbh.ND);
}
}
}
vi alg(int x) {
saw = {};
sawC = {};
//for(int i = 0; i < n; ++i) {
//done[i] = visited[i] = false;
//}
visited[x] = true;
Q.push(cc[x]);
saw.PB(x);
for(auto nbh : V2[x]) {
verToAC[nbh.ND].PB(nbh.ST);
sawC.PB(nbh.ND);
}
vi rem = {};
while(!Q.empty()) {
x = Q.front();
Q.pop();
//cout << "COL: " << x << "\n";
if(done[x]) continue;
rem.PB(x);
done[x] = true;
for(auto y : verToAC[x]) {
if(!visited[y]) dfss(y);
}
}
for(int xx : rem) {
done[xx] = false;
}
for(int xx : saw) {
visited[xx] = false;
}
for(int xx : sawC) {
verToAC[xx].clear();
}
return saw;
}
vi find_reachable(vi r, vi u, vi v, vi c) {
n = (int)r.size();
m = (int)u.size();
for(int i = 0; i < m; ++i) {
V[c[i]][u[i]].PB(v[i]);
V[c[i]][v[i]].PB(u[i]);
V2[u[i]].emplace_back(v[i], c[i]);
V2[v[i]].emplace_back(u[i], c[i]);
}
for(int i = 0; i < n; ++i) {
cc[i] = r[i];
}
int ver = n-1;
for(int i = 0; i < n; ++i) {
for(auto it : V[i]) {
if(!vis[it.ST]) {
seen.clear();
dfs(it.ST, i);
bool any = false;
for(auto x : seen) {
if(r[x] == i) any = true;
}
if(!any) continue;
ver++;
for(auto x : seen) {
G[ver].PB(x);
Grev[x].PB(ver);
if(r[x] == i) {
G[x].PB(ver);
Grev[ver].PB(x);
}
}
}
}
for(auto it : V[i]) vis[it.ST] = false;
}
for(int i = 0; i <= ver; ++i) {
if(!vis[i]) {
dfs2(i);
}
}
for(int i = 0; i <= ver; ++i) {
vis[i] = false;
}
for(int i = ver; i >= 0; --i) {
if(!vis[topo[i]]) {
color++;
dfs3(topo[i]);
}
}
for(int i = 0; i <= ver; ++i) ok[i] = false;
for(int i = 0; i < n; ++i) ok[SCC[i]] = true;
for(int i = 0; i <= ver; ++i) {
for(int nbh : G[i]) {
if(SCC[nbh] != SCC[i]) {
ok[SCC[i]] = false;
}
}
}
vector<vi>com;
int mi = INF;
for(int i = 1; i <= color; ++i) {
if(!ok[i]) continue;
com.PB(alg(rep[i]));
//cout << rep[i] << "\n";
//for(int x : com.back()) {
//cout << x << " ";
//}
//cout << "\n";
mi = min(mi, (int)com.back().size());
}
//cout << "\n";
vector<int>ans(n);
for(int i = 0; i < n; ++i) ans[i] = false;
for(auto vc : com) {
if((int)vc.size() == mi) {
for(int x : vc) {
ans[x] = true;
}
}
}
//for(int i = 0; i < n; ++i) {
//if(ok[SCC[i]] && cnt[SCC[i]] == mi) {
//ans[i] = true;
//} else {
//ans[i] = false;
//}
//}
return ans;
}
//int main() {
//ios_base::sync_with_stdio(0);
//cin.tie(0);
//int N, M;
//cin >> N >> M;
//vi r(N), u(M), v(M), c(M);
//for(int i = 0; i < N; ++i) {
//cin >> r[i];
//}
//for(int i = 0; i < M; ++i) {
//cin >> u[i] >> v[i] >> c[i];
//}
//vi w = find_reachable(r,u,v,c);
//vi w = find_reachable({0, 1, 1, 2},{0, 0, 1, 1, 3}, {1, 2, 2, 3, 1}, {0, 0, 1, 0, 2});
//vi w = find_reachable({0, 1, 1, 2, 2, 1, 2},{0, 0, 1, 1, 2, 3, 3, 4, 4, 5},
//{1, 2, 2, 3, 3, 4, 5, 5, 6, 6},
//{0, 0, 1, 0, 0, 1, 2, 0, 2, 1});
//vi w = find_reachable({0, 0, 0}, {0}, {1}, {0});
//for(int x : w) {
//cout << x << " ";
//}
//}
# | 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... |