HDU 5156-harry and Christmas tree (Dfs sequence + offline trees array)

Source: Internet
Author: User

http://acm.hdu.edu.cn/showproblem.php?pid=5156

Bc#25 's C question.

Test instructions is: give a tree of size n, take 1 as the root, and then give the M-time staining, each time the node U plus a color (a node can have more than one color).

Finally, each node on the query tree corresponds to the number of different colors contained on the subtree.

At that time this game did not do, come back to see the topic, did not look at the standard solution to try to knock over, so the problem-solving ideas from the beginning of the road to the road.

The solution is O (n+m), the main idea is to turn the problem into: a single stain indicates that the path of the U to the root node is infected with this color.

But this needs to be heavy, because if both U and V are dyed in a certain color, their LCA and their parent chain will be dyed one more time.

So the offline Tarjan is used, one time for all 22 of the same color node of the LCA de-weight (minus 1 times staining).

The sort of places are sorted by cardinality, and the complexity is linear.

This time did not think of the above-mentioned standard solution linear algorithm,

The idea is to do the DFS sequence directly to the tree, all the nodes are replaced with the color sequence that is dyed on that node, and then you can find the interval in the DFS sequence of the subtree corresponding to each node (the nodes on the subtree must be contiguous in the DFS sequence).

So the problem was completely transformed into a query of different numbers of intervals, see spoj-dquery:http://www.spoj.com/problems/dquery/.

About this problem there are two ways, one is the offline tree array, one is on this basis to the online chairman of the tree, not much to unfold, there are many solutions can be consulted.

The method of the Chairman tree will be properly mle here, so the offline tree array method is used here.

Since this sequence is up to M in length, a MLOGM algorithm is implemented (visually timed out = =).

Finally with a quick read in, and the repetition of each node color to go to the weight of the problem, 2400ms, dangerous drifting.

Looking back at the code to find the record when the location of the number is not required to use a map, directly with the array can be, so optimized a bit, the last 900ms.

1#include <iostream>2#include <sstream>3#include <ios>4#include <iomanip>5#include <functional>6#include <algorithm>7#include <vector>8#include <string>9#include <list>Ten#include <queue> One#include <deque> A#include <stack> -#include <Set> -#include <map> the#include <cstdio> -#include <cstdlib> -#include <cmath> -#include <cstring> +#include <climits> -#include <cctype> + using namespacestd; A #defineXinf Int_max at #defineINF 0X3FFFFFFF - #defineMP (x, y) make_pair (x, y) - #definePB (x) push_back (x) - #defineREP (x,n) for (int x=0; x<n; X + +) - #defineREP2 (X,L,R) for (int x=l; x<=r; X + +) - #defineDEP (x,r,l) for (int x=r; x>=l; x--) in #defineCLR (a,x) memset (a,x,sizeof (A)) - #defineIT iterator to #defineRIT Reverse_iterator +typedefLong Longll; -typedef unsignedLong Longull; thetypedef pair<int,int>PII; *typedef vector<pii>VII; $typedef vector<int>VI;Panax Notoginseng #defineX First - #defineY Second the #defineLson (x) ((x) <<1) + #defineRson (x) ((x) <<1|1) A  the intScan () + { -     intRes, ch=0; $      while(! (ch>='0'&&ch<='9')) ch=GetChar (); $res=ch-'0'; -      while((Ch=getchar ()) >='0'&&ch<='9') -Res=res*Ten+ch-'0'; the     returnRes; - }Wuyi  the voidOut (inta) - { Wu     if(a>9) -Out (A/Ten); AboutPutchar (a%Ten+'0'); $ } -  -  - intc[500010]; A intN; +  the voidInitintN) { -N =N; $REP (i,n+1) c[i]=0; the } the  the voidAddintIintx) { the      while(i<=N) { -c[i]+=x; ini+=i&-i; the     } the } About  the intSuminti) { the     intR=0; the      while(i) { +r+=C[i]; -i-=i&-i; the     }Bayi     returnR; the } the  - inta[500010]; -  the intans[50000]; theVII query[500010]; the  theVI map[50000]; - intvis[50000]; the ints[50000], e[50000]; theVI vec[50000]; the 94 inttot=0; the voidDfsintu) { thevis[u]=1; thes[u]=tot;98REP (I,vec[u].size ()) a[tot++]=Vec[u][i]; About REP (I,map[u].size ()) { -         intv=Map[u][i];101         if(!Vis[v]) Dfs (v);102     }103e[u]=tot-1;104 } the 106 intmp[100001];//This is a lot slower if you switch to map here .107 108 intMain () {109     intn,m,l,r,u,v; the      while(~SCANF ("%d%d",&n,&m)) {111REP (I,50000) map[i].clear (); theREP (I,50000) vec[i].clear ();113REP (i,n-1) { theU=scan (); v=Scan (); theu--; v--; the Map[u]. PB (v);117 Map[v]. PB (u);118         }119 REP (i,m) { -U=scan (); v=Scan ();121u--;122 Vec[u]. PB (v);123         }124         /*To the color on a node to go heavy the REP (i,n) {126 sort (Vec[i].begin (), Vec[i].end ());127 vec[i].erase (Unique (Vec[i].begin (), Vec[i].end ()), Vec[i].end ()); -         }129         */ thetot=1;131CLR (Vis,0); theDfs0);133         134 REP (I,tot) query[i].clear ();135 REP (I,n) query[s[i]]. PB (MP (e[i],i));136         //map<int,int> MP;137CLR (mp,-1);138 init (tot);139DEP (I,tot,1) { $Add (I,1);141             if(mp[a[i]]!=-1) Add (mp[a[i]],-1);142 REP (J,query[i].size ()) {143ANS[QUERY[I][J]. Y] =sum (query[i][j]. X);144             }145mp[a[i]]=i;146         }147 REP (i,n) {148             if(i) Putchar (' ');149 out (Ans[i]); Max         }151Putchar ('\ n'); the     }153     return 0;154}

HDU 5156-harry and Christmas tree (Dfs sequence + offline trees array)

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.