[Bestcoder Round #25 1003] Harry and Christmas Tree

Source: Internet
Author: User

Harry and Christmas Tree
Time limit:5000/2500 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): Accepted submission (s): 3


Problem description
On Christmas night, Harry got a Christmas tree. This tree is made up of n nodes, with the number 1th node as the root. Each node in the tree had some different colors of presents, so Harry began to count the gifts on the tree. Every time Harry counted a gift, he would record a (a, b), a for the node where the gift was, and b for the color of the gift. After the statistics, Harry wanted to know that there were several different colors of gifts under the subtree contained in each node.
Enter a description
Multiple sets of input data the first row of each group of data consists of n m representing the size of the tree, and the number of gifts
   
    
        1≤n≤50000,1≤m  ≤500000    
 

    
   Next n-1 line, two number a B per line indicates that there is an edge between the node (a, B)
   
    
        1≤a,b≤n,a ≠ b  
 

    
   Next m line, two number a B for each line indicates a gift with a color B on the A node
   
    
        1≤a≤n,1≤b ≤ 100000  
 

    
   
Output description
Each set of data outputs a row of n integers, and the number of I indicates that there are several different color gifts under the subtree contained in the I node.
Input sample
5 33 14 12 35 11 94 64 6
Output sample
2 0 0 1 0 Tip: Make your program run faster as much as possible

Read the question when there is no train of thought, violence to see, in the merger of the subtree information when the thought of each color to be weighed. Then, I was overwhelmed. After the game, I saw the puzzle and found it was very divine.

How does ① weigh? Not a heavy sentence! For each color alone processing: We have processed the I node, then we in the processing of the i+1 node, we only need to mark the first node on the I+1 1, in the first node i+1 and the first I node of the LCA of the Mark 1, and finally add the tag!

②lca How to Beg? Find out the LCA of the I pair of nodes? No!

The Magical DFS sequence!

DFS has a good feature that, when dealing with a subtrees tree, it will not process the other subtrees until the subtrees tree is processed, which will be the lemma we have shown below.

Theorem: The LCA of the node I,j is (I,J), and the depth of the node x is dx; if the n nodes of color 1 are sorted in Dfs order to {a1,a2,a3,..., an}, then any I must have D (A1,ai) <=d (A2,ai) <=d (A3,ai) <=...<=d (Ai-1,ai) (i.e. (Ai-1,ai) is an LCA of AI and the former I-1 nodes, so we only need to mark 1 on (Ai-1,ai).

Prove:

Apparently, I∈[2,n]∩n.

①i=2, there is only one (A1,A2), theorem is established.

②i>2, if present j∈[1,i-2]∩n, makes D (Aj,ai) >d (Aj+1,ai).


Then (Aj,ai) in a subtree (Aj+1,ai), that is, AJ is in a subtree (Aj+1,ai) that is the root;

and (Aj+1,ai) is not in a subtree rooted in (Aj,ai),

Because if (Aj+1,ai) is in a subtree rooted in (Aj,ai), then D (Aj+1,ai) >=d (Aj,ai).

That is, aj+1 is not in a subtree rooted in (Aj,ai).

The subtree with the root (Ai,aj) is T,

The DFS access T must have visited aj+1, or did not have access to aj+1.

and ∵aj∈t,ai∈t.

If you set the Dfs order for node x to P (x),

Then P (aj+1) >p (AI) >p (AJ) or P (aj+1) <p (AJ) <p (AI)

However, the P (AJ) <p (aj+1) <p (AI), as set forth in the question, are contradictory to the above two situations.


Therefore, there is no j∈[1,i-2]∩n, making D (Aj,ai) >d (Aj+1,ai).

The original proposition is established.

#include <iostream>using namespace std #include <cstdio> #include <cstring> #include <algorithm > #include <cmath> #include <vector>vector<int> nodegift[50001],sorted[100001];int fa[50001],n[    100001],p[50001],s[100001],ans[50001];inline void Sort (int x,int ftr) {int i;    For (I=nodegift[x].size (); i--;) Sorted[nodegift[x][i]].push_back (x); for (I=p[x];i;i=n[i]) if (S[I]!=FTR) Sort (s[i],x);} inline int find (int x) {return fa[x]!=fa[fa[x]]?fa[x]=find (Fa[x]): Fa[x];}        vector<int> query[50001];inline void Tarjan (int x,int ftr) {int i;        Fa[x]=x;        for (I=p[x];i;i=n[i]) if (S[I]!=FTR) Tarjan (s[i],x);        For (I=query[x].size (); i--;)--ans[find (query[x][i]);    FA[X]=FTR; ANS[FTR]+=ANS[X];}    int main () {int i,n,m,j,a,b;        while (~SCANF ("%d%d", &n,&m)) {/*----Initialize----*/memset (p,0,sizeof (int) * (n+1));        memset (ans,0,sizeof (int) * (n+1)); for (i=n;i;--i) {vector<int> (). Swap (nodegift[i]);        Vector<int> (). Swap (query[i]);            }/*------Input-------*/for (i=n;--i;) {scanf ("%d%d", &a,&b);            N[i]=p[a],p[a]=i,s[i]=b;        N[i+n]=p[b],p[b]=i+n,s[i+n]=a;            } while (m--) {scanf ("%d%d", &a,&b);        Nodegift[a].push_back (b);        } for (I=100000;i;--i) vector<int> (). Swap (sorted[i]);        /*-sort for a order-*/Sort (1,0);            for (I=100000;i;--i) {for (J=sorted[i].size (); j--;) ++ans[sorted[i][j]];        For (J=sorted[i].size () -1;j>0;--j) Query[sorted[i][j]].push_back (sorted[i][j-1]);        }/*-tarjan for anses-*/Tarjan (1,0);        /*-------Output-----*/For (i=1;i<n;++i) printf ("%d", ans[i]);    printf ("%d\n", Ans[n]); }}

After the code:

One

This problem because first asked for the DFS sequence, so whether it is Tarjan particularly good writing, but I still write the first time is disabled:

③ I in clear sorted array of time forget the meaning of sorted, accidentally put it and the rest of the array from the 1~n clear, this is obviously not correct, later write code must pay attention to this point: the mind sober, distinguish oneself to variable set meaning.

Even more wrong is that I even did not make up the data on the film did not write on the hand, this is really a big bogey, it must be remembered!!! After writing the code, no matter how simple the problem is-the first thing must be a compilation of data; If you find that you can write a violent thing, write a pat.

Second, by this problem by the way to learn the DFS sequence, found still very God, here are some study notes:

The ④dfs sequence can be used to maintain a two-point distance in the tree, which can be used to maintain subtree rights and:

Because in the DFS sequence, a subtree must be connected in one piece, so the sub-tree summation becomes the interval summation problem;

If the node weight is written down when the node is entered and the inverse number of the node node is written down when the node is exited, then the distance between the node and the node in the subtree in which it is rooted can be expressed by the interval between them, because the points not on the path must be exited after entering. And the points on the path will not be exited after entering, which is in fact similar to the LCA turn RMQ.

⑤ for the diameter of the tree

A mighty Greedy: the farthest point from any node in the tree must be a point at one end of the tree's diameter;

A reliable DP: the diameter of a tree must be a short-circuit and a lower-shorted connection to a point down.

[Bestcoder Round #25 1003] Harry and Christmas Tree

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.