Mahjong tree
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 455 Accepted Submission(s): 143
Problem Description
Little 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 has n vertexs, indexed from 1 to n.)
Thought for a long time, finally he decides to use the mahjong to decorate the tree.
His mahjong is strange because all of the mahjong tiles had a distinct index.(Little sun has 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 is an artist. So he want to decorate the tree as beautiful as possible.
His decoration rules are as follows:
(1)Place exact one mahjong tile on each vertex.
(2)The mahjong tiles' index must be continues which are placed on the son vertexs of a vertex.
(3)The mahjong tiles' index must be continues which are placed on the vertexs of any subtrees.
Now he want to know that he can obtain how many different beautiful mahjong tree using these rules, because of the answer can be very large, you need output the answer modulo 1e9 + 7.
Thought for a long time, finally he decides to use the mahjong to decorate the tree.
His mahjong is strange because all of the mahjong tiles had a distinct index.(Little sun has 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 is an artist. So he want to decorate the tree as beautiful as possible.
His decoration rules are as follows:
(1)Place exact one mahjong tile on each vertex.
(2)The mahjong tiles' index must be continues which are placed on the son vertexs of a vertex.
(3)The mahjong tiles' index must be continues which are placed on the vertexs of any subtrees.
Now he want to know that he can obtain how many different beautiful mahjong tree using these rules, because of the answer can be very large, you need output the answer modulo 1e9 + 7.
Input
The first line of the input is 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 two integers ui and vi, which describes an edge of the tree, and vertex 1 is the root of the tree.
For each test case, the first line contains an integers n. (1 <= n <= 100000)
And the next n - 1 lines, each line contains two integers ui and vi, which describes an edge of the tree, and vertex 1 is the root of the tree.
Output
For 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
2 9 2 1 3 1 4 3 5 3 6 2 7 4 8 7 9 3 8 2 1 3 1 4 3 5 1 6 4 7 5 8 4
Sample Output
Case #1: 32 Case #2: 16
题意:给定一棵树,排序号,使得每个节点的儿子们是连续的,每颗子树是连续的,求方案数
从根节点开始,,每个节点如果有儿子有节点,就会有两种选择,一个是当前最大的数,一个是当前存留最小的数,其他没有儿子的节点由于是按顺序排列的,所以会对结果造成n!的影响,dfs一遍,另外如果有一个节点有多于两个节点有儿子,那是不合法的,只有一个节点是1
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
#define Max 100010
const int MOD = 1e9+7;
vector <int > q[Max];
int vis[Max];
long long res=1;
int flag=1;
int n;
void dfs(int a)
{
int k=q[a].size();
int cnt=0;
vis[a]=1;
for(int i=0;i<k;i++)
{
if(vis[q[a][i]]) continue;
if(q[q[a][i]].size()==1&&vis[q[a][i]]==0) cnt++;
else dfs(q[a][i]);
}
if(a!=1)k--;
if(k-cnt>2){flag=0;return ;}
if(cnt!=k)
{
res*=2;
res%=MOD;
}
//cout<<k<<" "<<cnt<<" ";
while(cnt>0)
{
res*=cnt;res%=MOD;cnt--;
}
//cout<<a<<" "<<res<<endl;
//cout<<res<<endl;
}
int main()
{
int T;
cin>>T;
int _case=0;
while(T--)
{
cin>>n;
flag=1;
for(int i=1;i<=n;i++) q[i].clear();
memset(vis,0,sizeof(vis));
int a,b;
for(int i=1;i<n;i++)
{
scanf("%d %d",&a,&b);
q[b].push_back(a);
q[a].push_back(b);
}
res=2;
if(n!=1)
dfs(1);
else res=1;
if(!flag) res=0;
cout<<"Case #"<<++_case<<": "<<res%MOD<<endl;
}
return 0;
}

本文深入探讨了某一技术领域的最新发展与实践应用,包括关键技术和解决方案的介绍,以及如何将这些技术应用于实际场景中以提升效率和解决具体问题。通过案例分析,展示了技术在不同行业中的创新应用,强调了其对未来发展的潜在影响。

642

被折叠的 条评论
为什么被折叠?



