Bzoj 1086 [SCOI2005] Royal Federal Tree sub-block

Source: Internet
Author: User

Test Instructions:Link Method:Tree Tiles parsing:In order to brush the team on the tree so to learn the tree, I heard this is a naked question so run to do. What is the process of tree chunking? Recursively searching from the root of the tree, if the backtracking node exceeds the size of the block we want to divide into (it may be set to a), these nodes are used as a block, and the traced node is connected to any node in the block. This can obviously be handled with a stack. And it is important to note that if we do not limit it, what will happen? The elements in the sub-blocks are broken into slag, that is, the blind points. What limits? Is the limit for the relative stack base of each node. If it is on the current node, it has two sons. The number of nodes found in the subtree of the left son is A-1, when we go to search the right son, it may be assumed that the right son as the root of the subtree is a chain of 10000 long. So apparently the bottom leaf node is in the same piece as the subtree represented by the left son, which is obviously broken into slag. So every time we search for a node, we have to take the current stack pointer as the relative stack of the node, so we can avoid this problem. Please fill in your own brain for more information. Is that it? And not. There will be a lot of points left in the final stack, but not more than B, and we divide the size of the block will not exceed 2b (self-brain compensation), so the stack of all points thrown into the last piece, the size will not exceed 3b, just corresponding to the requirements of the problem. Code:
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define N 1010using namespace STD;intSta[n];intn,b,cnt;intHead[n];intRoot[n];intBelong[n];BOOLV[n];intTot,top;structnode{intFrom,to,next;} edge[n<<1];voidInit () {memset(head,-1,sizeof(head)); Cnt=1;}voidEdgeadd (intFromintTo) {edge[cnt].to=to;    Edge[cnt].next=head[from]; head[from]=cnt++;}voidDfsintNow) {v[now]=1;intBot=top; for(inti=head[now];i!=-1; i=edge[i].next) {intto=edge[i].to;if(V[to])Continue; DFS (to);if(top-bot>=b)            {tot++; Root[tot]=now; Do{Belong[sta[top]]=tot;            top--; } while(Top!=bot); }} Sta[++top]=now;}intMain () {init ();scanf("%d%d", &n,&b); for(intI=1; i<n;i++) {intx, y;scanf("%d%d", &x,&y);        Edgeadd (x, y);    Edgeadd (Y,X); } DFS (1); while(top)    {Belong[sta[top--]]=tot; }cout<<tot<<endl; for(intI=1; i<n;i++)cout<<belong[i]<<"';cout<<belong[n]<<endl; for(intI=1; i<tot;i++)cout<<root[i]<<"';cout<<root[tot]<<endl; }

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Bzoj 1086 [SCOI2005] Royal Federal Tree sub-block

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.