HDU 5379 Mahjong Tree (detailed, structured + thought)

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5379


Surface:

Mahjong Tree Time limit:6000/3000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 1148 Accepted Submission (s): 351


Problem Descriptionlittle Sun is an artist. Today He is playing mahjong alone. He suddenly feels that the tree in the yard doesn ' t look good. So he wants to decorate the tree (the tree had n Vertexs, indexed from 1 to n.)
Thought for a long time, and finally he decides the mahjong to decorate the tree.
His mahjong are strange because all of the mahjong tiles had a distinct index. (Little Sun have only n mahjong tiles, and the Mahjong tiles indexed from 1 to n.)
He put the Mahjong tiles on the vertexs of the tree.
As is known to all, Little sun was an artist. So he want to decorate the tree as beautiful as possible.
His decoration rules is as follows:

(1) Place exact one mahjong tiles on each vertex.
(2) The Mahjong Tiles ' index must be continues which is placed on the son Vertexs of a vertex.
(3) The Mahjong Tiles ' index must be continues which is placed on the vertexs of any subtrees.

Now he want to know so he can obtain how many different beautiful mahjong tree using these rules, because of the answer Can is very large, you need output the answer modulo 1e9 + 7.
Inputthe first line of the input was a single integer T, indicates the number of test cases.
For each test case, the first line contains an integers n. (1 <= n <= 100000)
And the next n-1 lines, each line contains the integers UI and VI, which describes a edge of the tree, and Vertex 1 is The root of the tree.
Outputfor each test case, output one line. The output format is ' case #x: ans ' (without quotes), X is the case number, starting from 1.
Sample Input
292 13 14 35 36 27 48 79 382 13 14 35 16 47 58 4

Sample Output
Case #1:32Case #2:16

Source2015 multi-university Training Contest 7 Problem Solving:

You can analyze the situation when each node has several leaves and a few children continue to descend, all of them are good. You can see the code in detail.

Summarize:

This problem has made two fatal mistakes.

1. The naïve thought that the given edge is the son even to the father, originally also felt inappropriate, the topic did not say, but still naïve to write down ... Don't be out of context!!

2. Analysis of the point there are three can continue down the node, then this situation is not legal, incredibly, regardless, at that time really dizzy head, should output 0 is right Ah!

In a comprehensive way, I am really naïve ....


Code:

#include <iostream> #include <cstdio> #include <queue> #include <vector> #include <cstring > #define MOD 1000000007using namespace Std;long long ans;struct node{int id;//real kids vector <int> child;//    The adjacent edge vectors <int> Edge are saved; int num;} Store[100010];queue <int> qe;bool flag;bool vis[100010];void bfs (int x) {while (!qe.empty ()) Qe.pop ();//    The first to visit is the Father Memset (vis,0,sizeof (VIS));    int Sz,yezi,qujian;    Qe.push (x);    int tmp;        while (!qe.empty ()) {//leaf and interval number yezi=qujian=0;        Tmp=qe.front ();        Qe.pop ();//marked for access over vis[tmp]=1; sz=store[tmp].num;for (int i=0;i<sz;i++) {//if not visited, description is child if (!vis[store[tmp].edge[i]]) {vis[store[tmp].edge[i]]=1 ; Store[tmp].child.push_back (Store[tmp].edge[i]);}}        Sz=store[tmp].child.size (); for (int i=0;i<sz;i++) {//is not a leaf node, it is also able to descend the node//leaf node only if its father is connected to its edge, while the root node is also, but does not appear here if (store[store[tmp].child[                i]].num>1) {qujian++; Qe.push (Store[tmp].child[i]);            } else {yezi++; }}//started here without a special sentence, too idealistic, a node with more than three points can still continue down, then the answer is 0if (qujian>=3) {Flag=true;return;}        All are leaf nodes, all arranged if (Yezi==sz) {for (int i=1;i<=sz;i++) Ans=ans*i%mod;            }//an interval, the other whole leaves, only leaves one side, the interval side//Both sides interchange, leaves the entire arrangement else if (yezi==sz-1) {ans=ans*2%mod;        for (int i=1;i<sz;i++) Ans=ans*i%mod;            }//two intervals, the other whole leaves, only leaves in the middle, the interval on both sides//leaves the whole arrangement, the left and right interval interchange else if (yezi==sz-2) {ans=ans*2%mod;        for (int i=1;i<sz-1;i++) Ans=ans*i%mod;    }}}int Main () {int t,n,fa,son;    scanf ("%d", &t);        for (int i=1;i<=100000;i++) {store[i].id=i;        } for (int i=1;i<=t;i++) {//Initialize scanf ("%d", &n);            for (int j=1;j<=n;j++) {store[j].child.clear (); Store[j].edge.clear ();        store[j].num=0;     } for (int j=1;j<n;j++) {       scanf ("%d%d", &AMP;SON,&AMP;FA);//double-sided, search to determine who is the father, who is the son store[fa].edge.push_back; store[son].edge.pus        H_back (FA);//store[fa].num++;store[son].num++ of the number of connected edges;        }//a node-specific award if (n==1) Ans=1;          else{Flag=false;          ans=2;  BFS (1);        if (flag) ans=0;}    printf ("Case #%d:%lld\n", I,ans); } return 0;}


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

HDU 5379 Mahjong Tree (detailed, structured + thought)

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.