제출 #477000

#제출 시각아이디문제언어결과실행 시간메모리
477000JvThunder열쇠 (IOI21_keys)C++17
100 / 100
858 ms59564 KiB
#include <bits/stdc++.h> #include "keys.h" using namespace std; const int maxn = 3e5 + 5; int n,m,p[maxn],r[maxn],fa[maxn]; int sz[maxn]; int find(int x) { if (fa[x]==x) return x; return fa[x]=find(fa[x]); } bool have[maxn],vis[maxn],mk[maxn]; int hd,tl,q[maxn]; vector<pair<int,int>> g[maxn]; vector<int> vec[maxn]; vector<int> ans; int bfs(int s,bool flag=0) { hd=1,tl=0; int res=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) res = 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 : g[x]) { int y=A.first; if (have[A.second]) { if (find(y)!=s) res = find(y); if (!vis[y]) q[++tl] = y , vis[y] = 1; } else vec[A.second].push_back(y); } if (res) 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 : g[x]) vec[A.second].clear(); } if (flag) { for (int i=1;i<=tl;i++) ans[q[i]-1] = tl; } return res; } vector<int> find_reachable(vector<int> R,vector<int> U,vector<int> V, vector<int> C) { n = R.size(); m = U.size(); for (int i=0;i<n;i++) r[i+1] = R[i] + 1; int x,y,z; ans = vector<int> (n,n+1); for (int i=0;i<m;i++) { x=U[i]+1,y=V[i]+1,z=C[i]+1; g[x].push_back({y,z}),g[y].push_back({x,z}); } for (int i=1;i<=n;i++) fa[i] = i, sz[i] = 1; while (true) { bool flag = false; for (int i=1;i<=n;i++) mk[i] = 0; for (int i=1;i<=n;i++) { if (find(i)==i && !mk[i]) { int t=bfs(i); if (t) fa[i]=t,mk[t]=1,flag=1; else mk[i]=1; } } if (!flag) break; } int mn = n+1; for (int i=1;i<=n;i++) if (find(i)==i) bfs(i,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...