Submission #482047

#TimeUsernameProblemLanguageResultExecution timeMemory
482047PoPularPlusPlusRigged Roads (NOI19_riggedroads)C++17
0 / 100
265 ms62984 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define ull unsigned long long #define pb(e) push_back(e) #define sv(a) sort(a.begin(),a.end()) #define sa(a,n) sort(a,a+n) #define mp(a,b) make_pair(a,b) #define vf first #define vs second #define ar array #define all(x) x.begin(),x.end() const int inf = 0x3f3f3f3f; const int mod = 1000000007; const double PI=3.14159265358979323846264338327950288419716939937510582097494459230; bool remender(ll a , ll b){return a%b;} const int N = 300003; struct UFDS { int p[N],siz[N]; void init(int n){ for(int i = 0; i <= n; i++){ p[i] = i; siz[i] = 1; } } int get(int x){ return p[x]=(x==p[x] ? x : get(p[x])); } void unio(int u , int v){ int a = get(u); int b=get(v); if(a==b)return; //if(siz[a]>siz[b])swap(a,b); p[b]=a; siz[a]+=siz[b]; } }; const int l = 25; vector<pair<int,int>> adj[N]; vector<pair<int,int>> vp(N); int idx[N]; int timer; int tin[N], tout[N]; int up[N][l + 5]; int ans[N]; int cur; int there[N]; UFDS d; void dfs(int v, int p){ tin[v] = ++timer; up[v][0] = p; for (int i = 1; i <= l; ++i) up[v][i] = up[up[v][i-1]][i-1]; for (auto &[u , id] : adj[v]) { if (u != p){ dfs(u, v); idx[u] = id; } } tout[v] = ++timer; } bool is_ancestor(int u, int v){ return tin[u] <= tin[v] && tout[u] >= tout[v]; } int lca(int u, int v){ if (is_ancestor(u, v)) return u; if (is_ancestor(v, u)) return v; for (int i = l; i >= 0; --i) { if (!is_ancestor(up[u][i], v)) u = up[u][i]; } return up[u][0]; } void help(int x , int y){ int lc = lca(x,y); int u = d.get(x) , v = d.get(y); vector<int> ed; while(u != lc && is_ancestor(lc , u) == lc){ ed.pb(idx[u]); d.unio(d.get(up[u][0]) , u); u = d.get(u); } while(v != lc && is_ancestor(lc , v) == lc){ ed.pb(idx[v]); d.unio(d.get(up[v][0]) , v); v = d.get(v); } sv(ed); for(int i : ed){ if(ans[i] == 0)ans[i] = cur++; } } void solve(){ int n , m; cin >> n >> m; for(int i = 0; i < m; i++){ int a,b; cin >> a >> b; a--; b--; vp[i] = mp(a,b); } d.init(n + 2); memset(there , 0, sizeof there); for(int i = 1; i < n; i++){ int j; cin >> j; j--; there[j] = 1; adj[vp[j].vf].pb(mp(vp[j].vs , j)); adj[vp[j].vs].pb(mp(vp[j].vf , j)); } timer = 0; dfs(0,0); memset(ans,0,sizeof ans); cur = 1; for(int i = 0; i < m; i++){ if(ans[i] != 0)continue; if(there[i] == 1){ ans[i] = cur++; } else { help(vp[i].vf , vp[i].vs); ans[i] = cur++; } } for(int i = 0; i < m; i++){ cout << ans[i] << ' '; } cout << '\n'; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); //int t;cin >> t;while(t--) solve(); 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...