2019 (CCPC-Final 2019) K. Russian Dolls on the Christmas(dsu one tree)
Posted issue是fw
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2019 (CCPC-Final 2019) K. Russian Dolls on the Christmas(dsu one tree)相关的知识,希望对你有一定的参考价值。
子树内的问题,每次把子树内的点扔进数组里暴力维护
保留重儿子启发式合并即可
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e6+10;
int t,n;
struct edge{
int to,nxt;
}d[maxn]; int heaed[maxn],cnt=1;
vector<int>vec[maxn];
int dfn[maxn],low[maxn],ID[maxn],id;
int siz[maxn],son[maxn],vis[maxn];
void dfs(int u,int fa)
{
son[u] = 0; siz[u] = 1; dfn[u] = ++id; ID[id] = u;
for( auto v:vec[u] )
{
if( v==fa ) continue;
dfs(v,u);
siz[u] += siz[v];
if( siz[v] > siz[son[u]] ) son[u] = v;
}
low[u] = id;
}
int SON,res,ans[maxn];
void insert(int w,int keep)
{
if( keep==1 )
{
vis[w] = 1;
if( vis[w-1] && vis[w+1] ) res--;
else if( vis[w-1] || vis[w+1] ) return;
else res++;
}
else
{
vis[w] = 0;
if( vis[w-1] && vis[w+1] ) res++;
else if( vis[w-1] || vis[w+1] ) return;
else res--;
}
}
void calc(int u,int fa,int keep)
{
insert( u,keep );
for(auto v:vec[u] )
{
if( v==SON || v==fa ) continue;
for(int j=dfn[v];j<=low[v];j++) insert( ID[j],keep);
}
}
void dsu(int u,int fa,int keep)
{
for( auto v:vec[u] )
{
if( v==fa || v==son[u] ) continue;
dsu(v,u,0);
}
if( son[u] ) dsu(son[u],u,1),SON = son[u];
calc( u,fa,1 );
ans[u] = res; SON = 0;
if( !keep ) calc(u,fa,0);
}
int main()
{
int casenum=0; cin >> t;
while( t-- )
{
cin >> n ;
for(int i=1;i<n;i++)
{
int l,r; scanf("%d%d",&l,&r);
vec[l].push_back( r ), vec[r].push_back( l );
}
dfs(1,1);
dsu(1,1,0);
printf("Case #%d: ",++casenum);
for(int i=1;i<=n;i++) printf("%d%c",ans[i],i==n?'\\n':' ');
for(int i=1;i<=n;i++) vec[i].clear();
id = res = 0;
}
}
以上是关于2019 (CCPC-Final 2019) K. Russian Dolls on the Christmas(dsu one tree)的主要内容,如果未能解决你的问题,请参考以下文章
The 2019 China Collegiate Programming Contest Harbin Site K. Keeping Rabbits
2019 China Collegiate K. MUV LUV UNLIMITED(思维,博弈)
2019 ICPC 南昌 K. Tree(树上启发式合并,平衡树 treap)