AIM Tech Round 3 (Div. 2) E. centroids

Source: Internet
Author: User

Exercises

Tree-shaped DP

A very good topic.

Test instructions

For each point. Change an edge to make this point the center of the tree

Exercises

The so-called center of Gravity: refers to the maximum number of connected components after removing this point <=N/2

For each point, divided into downward analysis, upward analysis

Down analysis: Find the maximum node of the child node of the point U v. Then find the maximum child node of node V that is less than or equal to N/2, connected to U

Analyze up: Find the maximum node of the parent node of the point U v. If V==u so. Find the second largest node W. Then find the maximum child node of the child node of the point less than or equal to N/2, connected to U

Down analysis and up analysis only need to judge one, because the points greater than N/2 only one

Code reference: Http://blog.csdn.net/u014258433/article/details/52454105?locationNum=9

Code:

#include <bits/stdc++.h>#defineMAXN 400010#defineMoD 1000000007#definell Long Long#definePB Push_back#defineFS First#defineSe Secondusing namespacestd;Const intinf=1e9+7;intN;vector<int>G[MAXN];intFSON[MAXN],SSON[MAXN],UP[MAXN],DW[MAXN];intSIZ[MAXN],ANS[MAXN];voidDfsintUintFA) {Siz[u]=1; Fson[u]=sson[u]=0;  for(intI=0; I<g[u].size (); i++)    {        intv=G[u][i]; if(V==FA)Continue;        DFS (V,U); Siz[u]+=Siz[v]; if(Dw[v]>sson[u]) sson[u]=Dw[v]; if(fson[u]<Sson[u]) swap (fson[u],sson[u]); } Dw[u]=Fson[u]; if(siz[u]<=n/2) dw[u]=siz[u];}voidDFS1 (intUintFA) {    if(fa!=-1) {Up[u]=Max (UP[U],UP[FA]); if(Dw[u]!=fson[fa]) up[u]=Max (UP[U],FSON[FA]); Elseup[u]=Max (UP[U],SSON[FA]); if(n-siz[u]<=n/2) up[u]=n-Siz[u]; }    inttmp=0;  for(intI=0; I<g[u].size (); i++)    {        intv=G[u][i]; if(V==FA)Continue;        DFS1 (V,u); if(siz[v]>n/2) tmp=v; }    if(n-siz[u]>n/2) tmp=-1; if(!tmp) ans[u]=1; Else if(tmp>0)    {        if(siz[tmp]-dw[tmp]<=n/2) ans[u]=1; }     Else if(n-siz[u]-up[u]<=n/2) ans[u]=1;}intMain () {CIN>>N;  for(intI=1; i<n;i++)    {        intx, y; CIN>>x>>y;        G[X].PB (y);    G[Y].PB (x); } DFS (1,-1); DFS1 (1,-1);  for(intI=1; i<=n;i++) printf ("%d", Ans[i]); printf ("\ n"); return 0;}

AIM Tech Round 3 (Div. 2) E. centroids

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.