Submission #477009

#TimeUsernameProblemLanguageResultExecution timeMemory
477009JvThunderKeys (IOI21_keys)C++17
100 / 100
1018 ms51692 KiB
#include <bits/stdc++.h> #include "keys.h" using namespace std; #define fir first #define sec second const int maxn = 3e5 + 5; int n,m; int p[maxn],r[maxn]; // DSU int par[maxn]; int sz[maxn]; int find(int x) { if (par[x]==x) return x; return par[x]=find(par[x]); } bool have[maxn],vis[maxn],mk[maxn]; int hd,tl,q[maxn]; vector<pair<int,int>> conn[maxn]; // connections and the key value that is needed vector<int> vec[maxn]; vector<int> ans; int bfs(int s, bool flag = false) { hd = 1; tl = 0; int ret = 0; q[++tl] = s; vis[s] = 1; while (hd<=tl) { int x=q[hd]; hd++; if (!have[r[x]]) { while (!vec[r[x]].empty()) { int y=vec[r[x]].back(); if (find(y)!=s) ret = find(y); if (!vis[y]) { q[++tl] = y; vis[y] = 1; } vec[r[x]].pop_back(); } have[r[x]]=1; } for (pair<int,int> A : conn[x]) { int y = A.fir; if (have[A.sec]) { if (find(y)!=s) ret = find(y); if (!vis[y]) { q[++tl] = y; vis[y] = 1; } } else vec[A.sec].push_back(y); } if (ret) break; } for (int i=1;i<=tl;i++) vis[q[i]] = 0; for (int i=1;i<hd;i++) { int x = q[i]; have[r[x]] = 0; for (pair<int,int> A : conn[x]) vec[A.sec].clear(); } if (flag) { for (int i=1;i<=tl;i++) ans[q[i]-1] = tl; } return ret; } vector<int> find_reachable(vector<int> R,vector<int> U,vector<int> V, vector<int> C) { n = R.size(); m = U.size(); // initializing the DSU for (int i=1;i<=n;i++) { par[i] = i; sz[i] = 1; } // storing the room keys (1-based) for (int i=0;i<n;i++) r[i+1] = R[i] + 1; //initializing the answer array ans = vector<int> (n,n+1); // converting to the connection list (1-based) for (int i=0;i<m;i++) { int x,y,z; x=U[i]+1; y=V[i]+1; z=C[i]+1; conn[x].push_back({y,z}); conn[y].push_back({x,z}); } // maximum log(n) times while (true) { bool flag = false; for (int i=1;i<=n;i++) mk[i] = false; for (int i=1;i<=n;i++) { if (find(i)==i && !mk[i]) { int t = bfs(i); if (t>0) { par[i] = t; mk[t] = true; flag = true; } else mk[i] = true; } } if (!flag) break; } for (int i=1;i<=n;i++) { // only bfs to the roots if (find(i)==i) bfs(i,1); } // formatting the answer: ans[i] is the number of rooms vis int mn = n+1; for (int i=1;i<=n;i++) mn = min(mn,ans[i-1]); for (int i=1;i<=n;i++) { if (mn==ans[i-1]) ans[i-1] = 1; else ans[i-1] = 0; } return ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...