-
Notifications
You must be signed in to change notification settings - Fork 1
/
ByteLandian Tours.cs
117 lines (98 loc) · 2.53 KB
/
ByteLandian Tours.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System;
using System.Collections.Generic;
using System.Text;
class Solution
{
public const int MaxN = 10000;
public const int Modulo = 1000000007;
public static long[] Factorial;
public static void Init()
{
// Init Factorial
Factorial = new long[MaxN+2];
Factorial[0] = 1;
for (int i = 1; i <= MaxN; i++)
Factorial[i] = Factorial[i - 1] * i % Modulo;
}
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder();
Init();
int NumTests;
int.TryParse(Console.ReadLine(), out NumTests);
for (int t = 0; t < NumTests; t++)
{
Tree tree = new Tree();
sb.Append(tree.Solve()).AppendLine();
}
Console.WriteLine(sb.ToString());
}
}
class Tree
{
int _numNodes;
int _numLeaves;
Node[] _nodes;
long Result = 1;
bool _updateResult;
public Tree()
{
int.TryParse(Console.ReadLine(), out _numNodes);
_numLeaves = 0;
//Initializing Nodes
_nodes = new Node[_numNodes];
for (int n = 0; n < _numNodes; n++)
_nodes[n] = new Node(n);
//Reading Edges
string[] input;
int i, j;
for (int edge = 0; edge < _numNodes - 1; edge++)
{
input = Console.ReadLine().Split();
int.TryParse(input[0], out i);
int.TryParse(input[1], out j);
_nodes[i].Neighbors.Add(j);
_nodes[j].Neighbors.Add(i);
}
}
public long Solve()
{
MarkLeaves();
foreach (Node n in _nodes)
if (!n.IsLeaf && FindNextInCentralPath(n) > 2)
return 0;
if (_numLeaves == _numNodes - 1)
return Result;
return 2*Result % Solution.Modulo;
}
private void MarkLeaves()
{
foreach (Node n in _nodes)
if (n.Neighbors.Count == 1)
{
n.IsLeaf = true;
_numLeaves++;
}
}
private int FindNextInCentralPath(Node n)
{
int NumLeaves = 0;
int NumNext = 0;
foreach (int neighbor in n.Neighbors)
if (!_nodes[neighbor].IsLeaf)
NumNext++;
else
NumLeaves++;
Result = Result * Solution.Factorial[NumLeaves] % Solution.Modulo;
return NumNext;
}
}
class Node
{
public bool IsLeaf;
public List<int> Neighbors;
public Node(int idx)
{
Neighbors = new List<int>();
}
}