/* Ideas: Lessons dsu on tree, bottom-up merger Each leaf node assigned to the initial value of 1, each node in turn merge each son In order to prevent conflicts, sub-tree when it is incorporated into the current node, all nodes must be renumbered Finally, the current node tree to go into the child because they inserted into a node, so once again re-numbered Each maximum of n times are renumbered, the complexity O (n ^ 2) */ #include<bits/stdc++.h> #include<vector> using namespace std; #define N 2005 int n,p[N],c[N],id[N]; vector<int>G[N]; vector<int> subtree[N]; int root; void dfs(int u,int pre){ if(G[u].size()==1 && pre==0){ subtree[u].push_back(u); id[u]=1;return; } for (auto i G [u]) if (v! = to) dfs (v, u); int tot=0; for(auto v:G[u])if(v!=pre){ for(auto x:subtree[v]){ subtree[u].push_back(x); id [x] + = all; } tot+=subtree[v].size(); subtree[v].clear(); } stack<int>stk; while(stk.size())stk.pop(); while(subtree[u].size()>=c[u]){ stk.push(subtree[u].back()); subtree[u].pop_back(); } subtree[u].push_back(u); id[u]=c[u]; while(stk.size()){ int now=stk.top();stk.pop(); id[now]++; subtree[u].push_back(now); } } int size[N]; void getsize(int u,int pre){ size[u]=1; for(auto v:G[u]){ if(v==pre)continue; getsize(v,u);size[u]+=size[v]; } } int main () { cin>>n; for(int i=1;i<=n;i++){ cin>>p[i]>>c[i]; c[i]++; G[i].push_back(p[i]); G[p[i]].push_back(i); if(p[i]==0)root=i; } getsize(root,0); for(int i=1;i<=n;i++) if(size[i]<c[i]){ puts("NO");return 0; } dfs(root,0); puts("YES"); for(int i=1;i<=n;i++)cout<<id[i]<<" "; }
cf1286B-- construction, heuristic merge
Guess you like
Origin www.cnblogs.com/zsben991126/p/12159094.html
Ranking