Submission #936130

#TimeUsernameProblemLanguageResultExecution timeMemory
936130vjudge1Rigged Roads (NOI19_riggedroads)C++17
100 / 100
440 ms96160 KiB
#include<bits/stdc++.h> #define pb push_back #define pli pair<int,int> #define fi first #define se second #define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); using namespace std; using ll=long long; const ll maxn=3e5+10; const ll inf=1e18; const ll mod=1e9+7; bool ins[maxn]; ll lab[maxn],top[maxn],P[maxn][20]; ll findset(ll x) { return lab[x]<0?x:lab[x]=findset(lab[x]); } vector<pli>g[maxn]; ll h[maxn]; void unite(ll u,ll v) { u=findset(u); v=findset(v); if(u==v) return; if(lab[u]>lab[v]) swap(u,v); lab[u]+=lab[v]; lab[v]=u; if(h[top[u]]>h[top[v]]) top[u]=top[v]; } ll idx[maxn]; void dfs(ll u,ll p) { P[u][0]=p; for(int i=1;i<=18;i++) P[u][i]=P[P[u][i-1]][i-1]; for(auto [v,id]:g[u]) { if(v!=p) { h[v]=h[u]+1; idx[v]=id; dfs(v,u); } } } void update(ll u) { ins[u]=true; top[u]=u; for(auto [v,id]:g[u]) { if(ins[v]==true) unite(u,v); } } ll par[maxn]; vector<pli>get(ll u,ll p) { vector<pli>ans; while(h[u]>h[p]) { if(ins[u]==true) { u=top[findset(u)]; u=P[u][0]; } else { ans.pb({u,idx[u]}); u=P[u][0]; } } return ans; } ll n,e,x[maxn],y[maxn]; bool in[maxn]; ll ans[maxn]; ll lca(ll u,ll v) { if(h[u]<h[v]) swap(u,v); for(int i=18;i>=0;i--) if(h[u]-(1<<i)>=h[v]) u=P[u][i]; if(u==v) return u; for(int i=18;i>=0;i--) if(P[u][i]!=P[v][i]) u=P[u][i],v=P[v][i]; return P[u][0]; } bool cmp(pli x,pli y) { return x.se<y.se; } void solve() { cin >> n >> e; for(int i=1;i<=n;i++) lab[i]=-1; for(int i=1;i<=e;i++) { cin >> x[i] >> y[i]; in[i]=false; } for(int i=1;i<n;i++) { ll id; cin >> id; in[id]=true; g[x[id]].pb({y[id],id}); g[y[id]].pb({x[id],id}); } dfs(1,0); ll dien=0; for(int i=1;i<=e;i++) { if(ans[i]>0) continue; if(in[i]==false) { ll u=x[i]; ll v=y[i]; ll c=lca(u,v); vector<pli>cc=get(u,c); vector<pli>vc=get(v,c); for(auto zz:vc) cc.pb(zz); sort(cc.begin(),cc.end(),cmp); for(auto zz:cc) { update(zz.fi); ans[zz.se]=++dien; } ans[i]=++dien; } else { ll u=x[i]; ll v=y[i]; if(h[u]<h[v]) swap(u,v); update(u); ans[i]=++dien; } } for(int i=1;i<=e;i++) { cout << ans[i]<<' '; } } int main() { fastio //freopen(TASKNAME".INP","r",stdin); //freopen(TASKNAME".OUT","w",stdout); solve(); }
#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...