static void Main() { var n = int.Parse(Console.ReadLine()); var edges = Enumerable.Repeat(0, n - 1).Select(_ => Console.ReadLine().Split().Select(x => int.Parse(x) - 1).ToArray()).ToArray(); int k = int.Parse(Console.ReadLine()); Node[] nodes = new Node[n]; for (int i = 0; i < k; i++) { var vp = Console.ReadLine().Split().Select(int.Parse).ToArray(); nodes[vp[0] - 1] = new Node(vp[1] % 2 == 0 ? Parity.Even : Parity.Odd, vp[1], vp[1]); } var IdentityElement = new Node(Parity.Both, int.MinValue / 2, int.MaxValue / 2); ReRooting <Node> rerooting = new ReRooting <Node>(n, edges, IdentityElement, Node.Merge, Node.Next, (x, index) => Node.Merge(x, nodes[index] ?? IdentityElement)); var res = new Node[n]; for (int i = 0; i < n; i++) { var query = rerooting.Query(i); if (query.Parity == Parity.None || query.LowerBound > query.UpperBound || (query.LowerBound == query.UpperBound && ((query.Parity == Parity.Even) ^ query.LowerBound % 2 == 0))) { goto invalid; } res[i] = query; } Console.WriteLine("Yes"); Console.WriteLine(string.Join("\n", res.Select(x => x.UpperBound))); return; invalid :; Console.WriteLine("No"); }
public static void Main() { var n = int.Parse(Console.ReadLine()); var edges = Enumerable.Repeat(0, n - 1).Select(_ => Console.ReadLine().Split().Select(x => int.Parse(x) - 1).ToArray()).ToArray(); ReRooting <Data> res = new ReRooting <Data>( n, edges, new Data() { Combination = 1, Count = 0 }, (x, y) => { var newCount = x.Count + y.Count; return(new Data() { Combination = x.Combination * y.Combination * Factorial(newCount) / Factorial(x.Count) / Factorial(y.Count), Count = newCount }); }, (x, _) => new Data() { Combination = x.Combination, Count = x.Count + 1 }); for (int i = 0; i < n; i++) { Console.WriteLine(res.Query(i).Combination); } }
public static void Main() { var nk = Console.ReadLine().Split().Select(int.Parse).ToArray(); var n = nk[0]; var k = nk[1]; ReRooting <Tuple <long, int[]> > hoge = new ReRooting <Tuple <long, int[]> >( n, Enumerable.Repeat(0, n - 1).Select(_ => Console.ReadLine().Split().Select(x => int.Parse(x) - 1).ToArray()).ToArray(), new Tuple <long, int[]>(0, new int[k]), (x, y) => { return(new Tuple <long, int[]>(x.Item1 + y.Item1, (x.Item2.Zip(y.Item2, (X, Y) => X + Y).ToArray()) )); }, (x, y) => { var res = new int[k]; for (int i = 0; i < k - 1; i++) { res[i + 1] = x.Item2[i]; } res[0] = x.Item2[k - 1] + 1; return(new Tuple <long, int[]>(x.Item1 + res[0], res)); } ); Console.WriteLine((Enumerable.Range(0, n).Sum(x => hoge.Query(x).Item1 - hoge.Query(x).Item2[0])) / 2); }
public static void Main() { var n = int.Parse(Console.ReadLine()); ReRooting <Data> rerooting = new ReRooting <Data>( n, Enumerable.Repeat(0, n - 1).Select(_ => Console.ReadLine().Split().Select(x => int.Parse(x) - 1).ToArray()).ToArray(), new Data() { Available = 0, Unavailable = 0 }, Data.Merge, (node, _) => node, (node, _) => Data.AddEdge(node) ); int res = 0; for (int i = 0; i < n; i++) { var query = rerooting.Query(i); if (query.Available == query.Unavailable) { res++; } } Console.WriteLine(res); }
public static void Main() { int n = int.Parse(Console.ReadLine()); var edges = new int[n - 1][]; List <int>[] tree = Enumerable.Repeat(0, n).Select(_ => new List <int>()).ToArray(); for (int i = 0; i < n - 1; i++) { var st = Console.ReadLine().Split().Select(x => int.Parse(x) - 1).ToArray(); edges[i] = st; tree[st[0]].Add(st[1]); tree[st[1]].Add(st[0]); } ReRooting <int> subTreeSizes = new ReRooting <int>(n, edges, 0, (x, y) => x + y, x => x + 1); ModInt prob = 0; for (int i = 0; i < n; i++) { //白の時、他の部分木の2つ以上に着色があればいい //一つ、ゼロの余事象 ModInt zeroProb = 1 / Power(2, n - 1); ModInt oneProb = 0; foreach (var subtreeSize in subTreeSizes.dp[i]) { var any = 1 - 1 / Power(2, subtreeSize); oneProb += 1 / Power(2, n - subtreeSize - 1) * any; } prob += 1 - zeroProb - oneProb; } Console.WriteLine(prob / 2); }
static void Main() { var n = int.Parse(Console.ReadLine()); var edges = Enumerable.Repeat(0, n - 1).Select(_ => Console.ReadLine().Split().Select(x => int.Parse(x) - 1).ToArray()).ToArray(); ReRooting <int> getDiameter = new ReRooting <int>(n, edges, 0, Max, x => x + 1); var diameter = Enumerable.Range(0, n).Select(x => getDiameter.Query(x)).Max() - 1; Console.WriteLine(diameter % 3 == 1 ? "Second" : "First"); }
static void Main() { int n = int.Parse(Console.ReadLine()); List <int>[] neighbours = Enumerable.Repeat(0, n).Select(_ => new List <int>()).ToArray(); int[][] edges = Enumerable.Repeat(0, n - 1).Select(_ => Console.ReadLine().Split().Select(x => int.Parse(x) - 1).ToArray()).ToArray(); ReRooting treeDP = new ReRooting(n, edges); Console.WriteLine(string.Join("\n", Enumerable.Range(0, n).Select(x => treeDP.Query(x)))); }
static void Main() { var nm = Console.ReadLine().Split().Select(int.Parse).ToArray(); var n = nm[0]; Mod = nm[1]; List <int>[] neighbours = Enumerable.Repeat(0, n).Select(_ => new List <int>()).ToArray(); int[][] edges = Enumerable.Repeat(0, n - 1).Select(_ => Console.ReadLine().Split().Select(x => int.Parse(x) - 1).ToArray()).ToArray(); ReRooting <long> treeDP = new ReRooting <long>(n, edges, 1, (x, y) => (x * y) % nm[1], x => x + 1); Console.WriteLine(string.Join("\n", Enumerable.Range(0, n).Select(x => treeDP.Query(x) - 1))); }
public static void Main() { int n = int.Parse(Console.ReadLine()); List <int[]> edges = new List <int[]>(); for (int i = 1; i < n; i++) { edges.Add(new int[] { i, int.Parse(Console.ReadLine()) }); } var reRooting = new ReRooting <int>(n, edges.ToArray(), 0, (x, y) => x + y, (x, _) => x + 1); for (int i = 0; i < reRooting.NodeCount; i++) { Console.WriteLine(reRooting.DP[i].Max()); } }
public static void Main() { int n = NextInt; int[][] edges = new int[n - 1][]; for (int i = 1; i < n; i++) { edges[i - 1] = new int[] { i, NextInt }; } ReRooting <int> rerooting = new ReRooting <int>(n, edges, 0, (x, y) => x + y, (x, _) => x + 1); for (int i = 0; i < n; i++) { Console.WriteLine(rerooting.DP[i].Max()); } }
static void Main() { int n = int.Parse(Console.ReadLine()); List <int>[] neighbours = Enumerable.Repeat(0, n).Select(_ => new List <int>()).ToArray(); int[][] edges = Enumerable.Repeat(0, n - 1).Select(_ => Console.ReadLine().Split().Select(x => int.Parse(x) - 1).ToArray()).ToArray(); ReRooting treeDP = new ReRooting(n, edges); var dicts = treeDP.Neighbours.Select(x => x.Select((y, z) => new Tuple <int, int>(y, z)).ToDictionary(y => y.Item1, y => y.Item2)).ToArray(); var sums = Console.ReadLine().Split().Select(long.Parse).ToArray(); int eqIndex = -1; long[] res = new long[edges.Length]; BigInteger sum = new BigInteger(0); for (int i = 0; i < sums.Length; i++) { sum += sums[i]; } for (int i = 0; i < edges.Length; i++) { var edge = edges[i]; long atob = treeDP.Query(edge[0], dicts[edge[0]][edge[1]]); long btoa = treeDP.Query(edge[1], dicts[edge[1]][edge[0]]); if (atob == btoa) { res[i] = 0; eqIndex = i; } else { res[i] = (Abs(Abs(sums[edge[0]] - sums[edge[1]]) / (Abs(atob - btoa)))); sum -= (2 * res[i] * new BigInteger(atob) * btoa); } } if (eqIndex >= 0) { BigInteger zeroatob = treeDP.Query(edges[eqIndex][0], dicts[edges[eqIndex][0]][edges[eqIndex][1]]); BigInteger zerobtoa = treeDP.Query(edges[eqIndex][1], dicts[edges[eqIndex][1]][edges[eqIndex][0]]); res[eqIndex] = (long)(sum / (zeroatob * zerobtoa * 2)); } Console.WriteLine(string.Join("\n", res)); }
static void Solve() { var nabd = Console.ReadLine().Split().Select(int.Parse).ToArray(); var n = nabd[0]; var a = nabd[1] - 1; var b = nabd[2] - 1; var da = nabd[3]; var db = nabd[4]; var edges = Enumerable.Repeat(0, n - 1).Select(_ => Console.ReadLine().Split().Select(x => int.Parse(x) - 1).ToArray()).ToArray(); ReRooting <int> rerooting = new ReRooting <int>(n, edges, 0, Max, (x, _) => x + 1); int[] distance = Enumerable.Repeat(-1, n).ToArray(); Stack <int> stack = new Stack <int>(); stack.Push(a); distance[a] = 0; while (stack.Count != 0) { var parent = stack.Pop(); foreach (var adj in rerooting.Adjacents[parent]) { if (distance[adj] != -1) { continue; } distance[adj] = distance[parent] + 1; stack.Push(adj); } } var d = Enumerable.Range(0, n).Select(rerooting.Query).Max() - 1; if (db <= da * 2 || d <= da * 2 || distance[b] <= da) { Console.WriteLine("Alice"); } else { Console.WriteLine("Bob"); } }
public static void Main() { int n = int.Parse(Console.ReadLine()); var edges = Enumerable.Repeat(0, n - 1).Select(_ => Console.ReadLine().Split().Select(x => int.Parse(x) - 1).ToArray()).ToArray(); List <int>[] graph = Enumerable.Repeat(0, n).Select(_ => new List <int>()).ToArray(); for (int i = 0; i < n - 1; i++) { var st = edges[0]; graph[st[0]].Add(st[1]); graph[st[1]].Add(st[0]); } long[] sums = new long[n]; Dictionary <int, long>[] dics = Enumerable.Repeat(0, n).Select(_ => new Dictionary <int, long>()).ToArray(); int q = int.Parse(Console.ReadLine()); for (int i = 0; i < q; i++) { var tex = Console.ReadLine().Split().Select(int.Parse).ToArray(); var ind = tex[1] - 1; var(node, par) = tex[0] == 1 ? (edges[ind][0], edges[ind][1]) : (edges[ind][1], edges[ind][0]); var x = tex[2]; if (!dics[node].ContainsKey(par)) { dics[node][par] = 0; } sums[node] += x; dics[node][par] += x; } ReRooting <long> rerooting = new ReRooting <long>(n, edges, 0, (x, y) => x + y, (val, node, par) => val + sums[node] - (dics[node].ContainsKey(par) ? dics[node][par] : 0)); for (int i = 0; i < n; i++) { Console.WriteLine(rerooting.Query(i)); } }
public static void Main() { int n = int.Parse(Console.ReadLine()); List <int>[] graph = Enumerable.Repeat(0, n).Select(_ => new List <int>()).ToArray(); int[][] edges = new int[n - 1][]; for (int i = 0; i < n - 1; i++) { var st = Console.ReadLine().Split().Select(x => int.Parse(x) - 1).ToArray(); edges[i] = st; graph[st[0]].Add(st[1]); graph[st[1]].Add(st[0]); } ReRooting <int> diamator = new ReRooting <int>(n, edges, 0, Max, (x, _) => x + 1); var max = Enumerable.Range(0, n).Max(x => diamator.Query(x)); var firstInd = Enumerable.Range(0, n).TakeWhile(x => diamator.Query(x) != max).Count(); Stack <int> stack = new Stack <int>(); stack.Push(firstInd); int[] distances = new int[n]; while (stack.Count > 0) { var elem = stack.Pop(); foreach (var item in graph[elem]) { if (item == firstInd || distances[item] != 0) { continue; } distances[item] = distances[elem] + 1; stack.Push(item); } } var secondInd = distances.TakeWhile(x => x != max - 1).Count(); Console.WriteLine($"{firstInd + 1} {secondInd + 1}"); }
public static void Main() { int n = int.Parse(Console.ReadLine()); var edges = Enumerable.Repeat(0, n - 1).Select(_ => Console.ReadLine().Split().Select(x => int.Parse(x) - 1).ToArray()).ToArray(); ReRooting <int> rerooting = new ReRooting <int>(n, edges, 0, Max, (x, _) => x + 1); var rerootingRes = Enumerable.Range(0, n).Select(rerooting.Query).ToArray(); var max = rerootingRes.Max(); var maxInd = Array.IndexOf(rerootingRes, max); List <int>[] graph = Enumerable.Repeat(0, n).Select(_ => new List <int>()).ToArray(); foreach (var st in edges) { graph[st[0]].Add(st[1]); graph[st[1]].Add(st[0]); } Stack <(ImmutableStack <int>, int)> stack = new Stack <(ImmutableStack <int>, int)>(); stack.Push((new ImmutableStack <int>().Push(maxInd), -1)); var maxSeq = new ImmutableStack <int>(); while (stack.Count != 0) { var(seq, prev) = stack.Pop(); if (maxSeq.Count < seq.Count) { maxSeq = seq; } var node = seq.Top; foreach (var adj in graph[node]) { if (adj == prev) { continue; } stack.Push((seq.Push(adj), node)); } } List <int> stem = new List <int>(); while (maxSeq.Count != 0) { stem.Add(maxSeq.Top); maxSeq = maxSeq.Pop(); } var dic = stem.ToDictionary(x => x, x => 0); for (int i = 0; i < n; i++) { if (dic.ContainsKey(i)) { continue; } var parent = graph[i].First(); if (graph[i].Count != 1 || !dic.ContainsKey(parent)) { Console.WriteLine(-1); return; } dic[parent]++; } for (int i = 0; i < stem.Count; i++) { if (dic[stem[i]] < dic[stem[^ (i + 1)]])