public static void Run() { using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput()))) using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()))) { int n = fs.NextInt(), m = fs.NextInt(); int[] p = new int[n]; DisjointSet dsu = new DisjointSet(); for (int i = 0; i < n; i++) { p[i] = fs.NextInt() - 1; dsu.MakeSet(i); } for (int i = 0; i < m; i++) { int u = fs.NextInt() - 1, v = fs.NextInt() - 1; dsu.Union(p[u], p[v]); } LinkedList<int>[] lists = new LinkedList<int>[n]; for (int i = n - 1; i >= 0; i--) { int set = dsu.FindSet(i); if (lists[set] == null) lists[set] = new LinkedList<int>(); lists[set].AddLast(i); } for (int i = 0; i < n; i++) { int set = dsu.FindSet(p[i]); writer.Write(lists[set].First() + 1 + " "); lists[set].RemoveFirst(); } } }
public static void Run() { using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput()))) using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()))) { int n = fs.NextInt(); DisjointSet dsu = new DisjointSet(); List<string> cycles = new List<string>(); for (int i = 1; i <= n; i++) { dsu.MakeSet(i); } for (int i = 0; i < n - 1; i++) { int a = fs.NextInt(), b = fs.NextInt(); if (dsu.FindSet(a) == dsu.FindSet(b)) cycles.Add(a + " " + b); else dsu.Union(a, b); } HashSet<int> hashSet = new HashSet<int>(); for (int i = 1; i <= n; i++) { hashSet.Add(dsu.FindSet(i)); } int[] leaders = hashSet.ToArray(); writer.WriteLine(leaders.Length - 1); int j = 0; for (int i = 0; i < leaders.Length - 1; i++) { writer.WriteLine(cycles[j++] + " " + leaders[i] + " " + leaders[i + 1]); } } }
public static void Run() { using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput()))) using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()))) { N = fs.NextInt(); M = fs.NextInt(); long k = fs.NextLong(); A = new Hay[N, M]; Answer = new int[N, M]; Visited = new bool[N, M]; List<Hay> hays = new List<Hay>(N * M); DisjointSet dsu = new DisjointSet(); for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { A[i, j] = new Hay { Id = M * i + j, Row = i, Column = j, Height = fs.NextLong() }; hays.Add(A[i, j]); } } hays = hays.OrderByDescending(h => h.Height).ToList(); foreach (Hay hay in hays) { dsu.MakeSet(hay.Id); if (hay.Row > 0 && hay.Height <= A[hay.Row - 1, hay.Column].Height) dsu.Union(hay.Id, A[hay.Row - 1, hay.Column].Id); if (hay.Column > 0 && hay.Height <= A[hay.Row, hay.Column - 1].Height) dsu.Union(hay.Id, A[hay.Row, hay.Column - 1].Id); if (hay.Row < N - 1 && hay.Height <= A[hay.Row + 1, hay.Column].Height) dsu.Union(hay.Id, A[hay.Row + 1, hay.Column].Id); if (hay.Column < M - 1 && hay.Height <= A[hay.Row, hay.Column + 1].Height) dsu.Union(hay.Id, A[hay.Row, hay.Column + 1].Id); int count = dsu.GetSetElementCount(hay.Id); if (k % hay.Height == 0) { long need = k / hay.Height; int temp = (int)need; if (need <= (long)count) { RunDFS(hay.Row, hay.Column, (int)hay.Height, ref temp); writer.WriteLine("YES"); for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { writer.Write(Answer[i, j] + " "); } writer.WriteLine(); } return; } } } writer.WriteLine("NO"); } }