Submission #242846

#TimeUsernameProblemLanguageResultExecution timeMemory
242846moonrabbit2Rigged Roads (NOI19_riggedroads)C++17
100 / 100
548 ms71432 KiB
#include <bits/stdc++.h> #define fi first #define se second using namespace std; typedef long long ll; typedef long double db; typedef pair<int,int> pii; typedef pair<ll,ll> pll; typedef pair<db,db> pdb; typedef tuple<int,int,int> tii; typedef tuple<ll,ll,ll> tll; typedef tuple<int,int,int,int> ti4; typedef vector<vector<ll>> mat; const ll mod=1e9+7,inf=1e18; const int N=3e5+5,M=2e5+5,K=1e7+5; int n,m,u[N],v[N],r[N],ok[N],cnt,ans[N]; vector<pll> g[N]; int in[N],pe[N],h[N],par[19][N],curp[N]; vector<int> V; int clr(int u,int v){ if(!u||h[u]<=h[v]) return u; if(!in[pe[u]]) V.emplace_back(pe[u]); in[pe[u]]=1; return curp[u]=clr(curp[u],v); } void dfs(int u){ for(auto [v,d] : g[u]) if(v!=par[0][u]){ h[v]=h[u]+1; par[0][v]=u; curp[v]=u; pe[v]=d; dfs(v); } } int lca(int u,int v){ if(h[u]>h[v]) swap(u,v); for(int bit=18;bit>=0;bit--) if((h[v]-h[u])&(1<<bit)){ v=par[bit][v]; } if(u==v) return u; for(int bit=18;bit>=0;bit--) if(par[bit][u]!=par[bit][v]){ u=par[bit][u]; v=par[bit][v]; } return par[0][u]; } int main(){ ios::sync_with_stdio(false); cin.tie(0); cin>>n>>m; for(int i=1;i<=m;i++) cin>>u[i]>>v[i]; for(int i=1;i<n;i++){ cin>>r[i]; ok[r[i]]=1; g[u[r[i]]].emplace_back(v[r[i]],r[i]); g[v[r[i]]].emplace_back(u[r[i]],r[i]); } h[1]=1; dfs(1); in[0]=1; for(int bit=1;bit<19;bit++) for(int i=1;i<=n;i++){ par[bit][i]=par[bit-1][par[bit-1][i]]; } for(int i=1;i<=m;i++){ if(ans[i]){ cout<<ans[i]<<" "; continue; } if(ok[i]){ in[i]=1; cout<<++cnt<<" "; continue; } int uv=lca(u[i],v[i]); clr(u[i],uv); clr(v[i],uv); sort(V.begin(),V.end()); for(auto j : V) ans[j]=++cnt; V.clear(); cout<<++cnt<<" "; } return 0; }
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...