Submission #487916

# Submission time Handle Problem Language Result Execution time Memory
487916 2021-11-17T04:05:02 Z niloyroot Rigged Roads (NOI19_riggedroads) C++14
100 / 100
389 ms 61124 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<ll>;
using pl = pair<ll,ll>;
#define pb push_back
#define form(m,it) for(auto it=m.begin(); it!=m.end(); it++)
#define forp(i,a,b) for(ll i=a; i<=b; i++)
#define forn(i,a,b) for(ll i=a; i>=b; i--)
#define newl '\n'
const ll mod = 1000000007;

void solve(){
    ll n,e; cin>>n>>e;

    vector<pl> edg;
    ll a,b;
    forp(i,1,e){
        cin>>a>>b;
        edg.pb({a,b});
    }

    vector<pl> adj[n+1];
    bool mst[e+1]={0};
    forp(i,1,n-1){
        ll t;
        cin>>t; t--;
        mst[t]=1;
        a=edg[t].first; b=edg[t].second;
        adj[a].pb({b,t});
        adj[b].pb({a,t});
    }

    ll len[n+1];
    pl par[n+1];
    function<void(ll,ll)> dfs = [&](ll i, ll pa){
        for(auto u:adj[i]){
            if(u.first!=pa){
                len[u.first]=len[i]+1;
                par[u.first].first=i; 
                par[u.first].second=u.second;
                dfs(u.first,i);
            }
        }
    };
    len[1]=0;
    par[1].first=-1; par[1].second=-1;
    dfs(1,-1);

    ll cnt=1;
    pl shortest_dep[n+1];
    ll link[n+1];
    ll sz[n+1];
    forp(i,1,n){
        link[i]=i;
        sz[i]=1;
        shortest_dep[i].first=len[i];
        shortest_dep[i].second=i;
    }
    ll ans[e]={0};
    ll u,v;
    forp(i,0,e-1){
        if(mst[i]){
            if(!ans[i]){
                ans[i]=cnt;
                cnt++;
                u=edg[i].first; v=edg[i].second;
                while(u!=link[u]){u=link[u];}
                while(v!=link[v]){v=link[v];}

                if(u!=v){
                    if(sz[u]<sz[v]){swap(u,v);}
                    sz[u]+=sz[v];
                    link[v]=u;
                    if(shortest_dep[u].first<shortest_dep[v].first){
						shortest_dep[v]=shortest_dep[u]; 
					} else {
						shortest_dep[u]=shortest_dep[v];
					}
                }
            }
        } else {
            vi cyc;

            u=edg[i].first; v=edg[i].second;
            while(u!=link[u]){u=link[u];}
            while(v!=link[v]){v=link[v];}
            u=shortest_dep[u].second;
            v=shortest_dep[v].second;

			
            while(u!=v){
                if(len[u]<len[v]){swap(u,v);}
                cyc.pb(par[u].second);
                b=par[u].first;
                while(u!=link[u]){u=link[u];}
                while(b!=link[b]){b=link[b];}
                if(u!=b){
                    if(sz[u]<sz[b]){
                        sz[b]+=sz[u];
                        link[u]=b;
                    } else {
                        sz[u]+=sz[b];
                        link[b]=u;
                    }
                    if(shortest_dep[u].first<shortest_dep[b].first){
						shortest_dep[b]=shortest_dep[u]; 
					} else {
						shortest_dep[u]=shortest_dep[b];
					}
                }
                u=shortest_dep[u].second;
            }

            sort(cyc.begin(), cyc.end());
            for(auto ed:cyc){

                ans[ed]=cnt;
                cnt++;
            }
            ans[i]=cnt; cnt++;
        }

        cout<<ans[i]<<" ";
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t=1; //cin>>t;
    while(t--)solve();
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 1 ms 296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 1 ms 296 KB Output is correct
4 Correct 1 ms 464 KB Output is correct
5 Correct 1 ms 464 KB Output is correct
6 Correct 1 ms 464 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 48 ms 17080 KB Output is correct
2 Correct 98 ms 24740 KB Output is correct
3 Correct 104 ms 14480 KB Output is correct
4 Correct 161 ms 50632 KB Output is correct
5 Correct 204 ms 52664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 74 ms 26448 KB Output is correct
2 Correct 46 ms 11588 KB Output is correct
3 Correct 22 ms 6084 KB Output is correct
4 Correct 75 ms 19708 KB Output is correct
5 Correct 25 ms 8012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 223 ms 49744 KB Output is correct
2 Correct 255 ms 58448 KB Output is correct
3 Correct 52 ms 16756 KB Output is correct
4 Correct 82 ms 24252 KB Output is correct
5 Correct 310 ms 61124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 173 ms 35628 KB Output is correct
2 Correct 107 ms 24200 KB Output is correct
3 Correct 385 ms 50536 KB Output is correct
4 Correct 330 ms 46148 KB Output is correct
5 Correct 15 ms 4936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 1 ms 296 KB Output is correct
4 Correct 1 ms 464 KB Output is correct
5 Correct 1 ms 464 KB Output is correct
6 Correct 1 ms 464 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 48 ms 17080 KB Output is correct
10 Correct 98 ms 24740 KB Output is correct
11 Correct 104 ms 14480 KB Output is correct
12 Correct 161 ms 50632 KB Output is correct
13 Correct 204 ms 52664 KB Output is correct
14 Correct 74 ms 26448 KB Output is correct
15 Correct 46 ms 11588 KB Output is correct
16 Correct 22 ms 6084 KB Output is correct
17 Correct 75 ms 19708 KB Output is correct
18 Correct 25 ms 8012 KB Output is correct
19 Correct 223 ms 49744 KB Output is correct
20 Correct 255 ms 58448 KB Output is correct
21 Correct 52 ms 16756 KB Output is correct
22 Correct 82 ms 24252 KB Output is correct
23 Correct 310 ms 61124 KB Output is correct
24 Correct 173 ms 35628 KB Output is correct
25 Correct 107 ms 24200 KB Output is correct
26 Correct 385 ms 50536 KB Output is correct
27 Correct 330 ms 46148 KB Output is correct
28 Correct 15 ms 4936 KB Output is correct
29 Correct 350 ms 46300 KB Output is correct
30 Correct 341 ms 49776 KB Output is correct
31 Correct 389 ms 49864 KB Output is correct
32 Correct 82 ms 13620 KB Output is correct
33 Correct 319 ms 49096 KB Output is correct