Esempio n. 1
0
        public static void Solve()
        {
            var N    = Scanner.Scan <int>();
            var M    = 400000;
            var dsu  = new DisjointSetUnion(M);
            var edge = new int[M];

            for (var i = 0; i < N; i++)
            {
                var(a, b) = Scanner.Scan <int, int>();
                var u = dsu.LeaderOf(a - 1);
                var v = dsu.LeaderOf(b - 1);
                var w = u;
                if (!dsu.IsSame(u, v))
                {
                    w        = dsu.Merge(u, v);
                    edge[w] += edge[w == u ? v : u];
                }
                edge[w]++;
            }

            var answer = 0;

            for (var i = 0; i < M; i++)
            {
                if (dsu.LeaderOf(i) == i)
                {
                    answer += Math.Min(edge[i], dsu.SizeOf(i));
                }
            }

            Console.WriteLine(answer);
        }
Esempio n. 2
0
        public static void Solve()
        {
            var(N, M) = Scanner.Scan <int, int>();
            var dsu   = new DisjointSetUnion(N);
            var count = new int[N];

            Array.Fill(count, 1);
            var check = new bool[N];

            check[0] = true;
            for (var i = 0; i < M; i++)
            {
                var(x, y) = Scanner.Scan <int, int>();
                x--; y--;
                count[x]--;
                count[y]++;
                check[y] |= check[x];
                if (count[x] == 0)
                {
                    check[x] = false;
                }
                dsu.Merge(x, y);
            }

            var answer = 0;

            for (var i = 0; i < N; i++)
            {
                if (dsu.IsSame(0, i) && check[i])
                {
                    answer++;
                }
            }
            Console.WriteLine(answer);
        }
Esempio n. 3
0
        public static void Solve()
        {
            var(N, M) = Scanner.Scan <int, int>();
            var G = new List <(int, int, long)>(N + M);

            for (var i = 1; i <= N; i++)
            {
                var c = Scanner.Scan <int>();
                G.Add((0, i, c));
            }
            for (var i = 0; i < M; i++)
            {
                var(a, b, r) = Scanner.Scan <int, int, long>();
                G.Add((a, b, r));
            }

            G.Sort((x, y) => x.Item3.CompareTo(y.Item3));
            var answer = 0L;
            var dsu    = new DisjointSetUnion(N + 1);

            foreach (var(u, v, r) in G)
            {
                if (dsu.IsSame(u, v))
                {
                    continue;
                }
                dsu.Merge(u, v);
                answer += r;
            }

            Console.WriteLine(answer);
        }
Esempio n. 4
0
        public static void Solve()
        {
            var(N, M) = Scanner.Scan <int, int>();
            var E   = new List <(int U, int V, long C)>();
            var dsu = new DisjointSetUnion(N);

            for (var i = 0; i < M; i++)
            {
                var(a, b, c) = Scanner.Scan <int, int, long>();
                if (c >= 0)
                {
                    E.Add((a - 1, b - 1, c));
                }
                else
                {
                    dsu.Merge(a - 1, b - 1);
                }
            }

            E.Sort((x, y) => x.C.CompareTo(y.C));
            var answer = 0L;

            foreach (var(u, v, c) in E)
            {
                if (dsu.IsSame(u, v))
                {
                    answer += c;
                    continue;
                }

                dsu.Merge(u, v);
            }

            Console.WriteLine(answer);
        }
Esempio n. 5
0
        public static void Solve()
        {
            var N = Scanner.Scan <int>();
            var X = new int[N];
            var Y = new int[N];

            for (var i = 0; i < N; i++)
            {
                var(x, y) = Scanner.Scan <int, int>();
                X[i]      = x - 1;
                Y[i]      = y - 1;
            }

            var YByX = new int[N];

            for (var i = 0; i < N; i++)
            {
                YByX[X[i]] = Y[i];
            }
            var indexes = new int[N];

            for (var i = 0; i < N; i++)
            {
                indexes[X[i]] = i;
            }
            var comp = Comparer <(int y, int leader)> .Create((l, r) => Comparer <int> .Default.Compare(l.y, r.y));

            var queue = new PriorityQueue <(int y, int leader)>(comp);
            var dsu   = new DisjointSetUnion(N);

            for (var i = 0; i < N; i++)
            {
                var y      = YByX[i];
                var leader = i;
                while (queue.Any() && queue.Peek().y < YByX[i])
                {
                    var tmp = queue.Dequeue();
                    dsu.Merge(i, tmp.leader);
                    leader = dsu.LeaderOf(i);
                    y      = Math.Min(y, tmp.y);
                }
                queue.Enqueue((y, leader));
            }

            var answer = new int[N];

            for (var i = 0; i < N; i++)
            {
                answer[indexes[i]] = dsu.SizeOf(i);
            }

            for (var k = 0; k < N; k++)
            {
                Console.WriteLine(answer[k]);
            }
        }
Esempio n. 6
0
        public static void Solve()
        {
            var(N, M) = Scanner.Scan <int, int>();
            var dsu = new DisjointSetUnion(N);

            for (var i = 0; i < M; i++)
            {
                var(X, Y, Z) = Scanner.Scan <int, int, int>();
                dsu.Merge(X - 1, Y - 1);
            }

            Console.WriteLine(dsu.GetGroups().Count());
        }
Esempio n. 7
0
        public static void Solve()
        {
            var(N, M) = Scanner.Scan <int, int>();
            var G   = new List <int> [N].Select(x => new List <int>()).ToArray();
            var dsu = new DisjointSetUnion(N);

            for (var i = 0; i < M; i++)
            {
                var(u, v) = Scanner.Scan <int, int>();
                u--; v--;
                G[u].Add(v);
                G[v].Add(u);
                dsu.Merge(u, v);
            }

            var answer = 0;

            foreach (var group in dsu.GetGroups())
            {
                var x     = group.First();
                var used  = new bool[N];
                var queue = new Queue <(int, int)>();
                queue.Enqueue((x, -1));
                var ok = true;
                while (queue.Any())
                {
                    var(u, p) = queue.Dequeue();
                    if (used[u])
                    {
                        ok = false;
                        break;
                    }
                    used[u] = true;
                    foreach (var v in G[u])
                    {
                        if (v == p)
                        {
                            continue;
                        }
                        queue.Enqueue((v, u));
                    }
                }

                if (ok)
                {
                    answer++;
                }
            }

            Console.WriteLine(answer);
        }
Esempio n. 8
0
        public static void Solve()
        {
            var N   = Scanner.Scan <int>();
            var P   = Scanner.ScanEnumerable <int>().Select(x => x - 1).ToArray();
            var Q   = Scanner.ScanEnumerable <int>().Select(x => x - 1).ToArray();
            var dsu = new DisjointSetUnion(N);

            foreach (var(p, q) in P.Zip(Q))
            {
                dsu.Merge(p, q);
            }

            var f = new Dictionary <int, mint>();

            f[1] = 2;
            f[2] = 3;

            mint F(int n)
            {
                if (f.ContainsKey(n))
                {
                    return(f[n]);
                }
                return(f[n] = F(n - 1) + F(n - 2));
            }

            var g = new Dictionary <int, mint>();

            g[1] = 1;
            g[2] = 3;
            g[3] = 4;

            mint G(int n)
            {
                if (g.ContainsKey(n))
                {
                    return(g[n]);
                }
                return(g[n] = F(n - 1) + F(n - 3));
            }

            mint answer = 1;

            foreach (var group in dsu.GetGroups())
            {
                answer *= G(group.Count);
            }

            Console.WriteLine(answer);
        }
Esempio n. 9
0
        public static void Solve()
        {
            var(N, Q) = Scanner.Scan <int, int>();
            var dsu = new DisjointSetUnion(N + 1);

            for (var i = 0; i < Q; i++)
            {
                var(l, r) = Scanner.Scan <int, int>();
                l--;
                dsu.Merge(l, r);
            }
            var answer = dsu.IsSame(0, N);

            Console.WriteLine(answer ? "Yes" : "No");
        }
Esempio n. 10
0
        public static void Solve()
        {
            var N   = Scanner.Scan <int>();
            var F   = Scanner.ScanEnumerable <int>().ToArray();
            var dsu = new DisjointSetUnion(N);

            for (var i = 0; i < N; i++)
            {
                dsu.Merge(i, F[i] - 1);
            }

            var answer = mint.Power(2, dsu.GetGroups().Count()) - 1;

            Console.WriteLine(answer);
        }
Esempio n. 11
0
        public static void Solve()
        {
            var(N, Q) = Scanner.Scan <int, int>();
            var C     = Scanner.ScanEnumerable <int>().Select(x => x - 1).ToArray();
            var dsu   = new DisjointSetUnion(N);
            var count = new Dictionary <int, int> [N].Select(_ => new Dictionary <int, int>()).ToArray();

            for (var i = 0; i < N; i++)
            {
                if (!count[i].ContainsKey(C[i]))
                {
                    count[i][C[i]] = 0;
                }
                count[i][C[i]]++;
            }
            while (Q-- > 0)
            {
                var(q, a, b) = Scanner.Scan <int, int, int>();
                a--; b--;
                if (q == 1)
                {
                    if (dsu.IsSame(a, b))
                    {
                        continue;
                    }
                    var la = dsu.LeaderOf(a);
                    var lb = dsu.LeaderOf(b);
                    dsu.Merge(a, b);
                    var na = dsu.LeaderOf(a);
                    var nb = na == la ? lb : la;
                    foreach (var(key, value) in count[nb])
                    {
                        if (!count[na].ContainsKey(key))
                        {
                            count[na][key] = 0;
                        }
                        count[na][key] += value;
                    }
                }
                else
                {
                    var l = dsu.LeaderOf(a);
                    Console.WriteLine(count[l].ContainsKey(b) ? count[l][b] : 0);
                }
            }
        }
Esempio n. 12
0
        public static void Solve()
        {
            var(N, K) = Scanner.Scan <int, int>();
            var G = new int[N][];

            for (var i = 0; i < N; i++)
            {
                G[i] = Scanner.ScanEnumerable <int>().ToArray();
            }

            mint answer = 1;
            var  dsu1   = new DisjointSetUnion(N);
            var  dsu2   = new DisjointSetUnion(N);

            for (var i = 0; i < N; i++)
            {
                for (var j = i + 1; j < N; j++)
                {
                    var(ok1, ok2) = (true, true);
                    for (var k = 0; k < N; k++)
                    {
                        ok1 &= G[k][i] + G[k][j] <= K;
                        ok2 &= G[i][k] + G[j][k] <= K;
                    }
                    if (ok1)
                    {
                        dsu1.Merge(i, j);
                    }
                    if (ok2)
                    {
                        dsu2.Merge(i, j);
                    }
                }
            }

            foreach (var group in dsu1.GetGroups())
            {
                answer *= EnumerationModulo.Factorial(group.Count());
            }
            foreach (var group in dsu2.GetGroups())
            {
                answer *= EnumerationModulo.Factorial(group.Count());
            }

            Console.WriteLine(answer);
        }
Esempio n. 13
0
        public static void Solve()
        {
            var(N, M) = Scanner.Scan <int, int>();
            var dsu = new DisjointSetUnion(N);

            for (var i = 0; i < M; i++)
            {
                var(u, v) = Scanner.Scan <int, int>();
                u--; v--;
                dsu.Merge(u, v);
            }

            var(g1, g2, g3) = (0L, 0L, 0L);
            foreach (var g in dsu.GetGroups())
            {
                if (g.Contains(0))
                {
                    g1 = g.Count();
                }
                if (g.Contains(1))
                {
                    g2 = g.Count();
                }

                if (g1 > 0 && g2 > 0)
                {
                    break;
                }
            }

            g3 = N - g1 - g2;

            if (g1 > g2)
            {
                g1 += g3;
            }
            else
            {
                g2 += g3;
            }

            var answer = g1 * (g1 - 1) / 2 + g2 * (g2 - 1) / 2 - M;

            Console.WriteLine(answer);
        }
Esempio n. 14
0
        public static void Solve()
        {
            var(N, Q) = Scanner.Scan <int, int>();
            var dsu = new DisjointSetUnion(N);

            while (Q-- > 0)
            {
                var(p, a, b) = Scanner.Scan <int, int, int>();
                if (p == 0)
                {
                    dsu.Merge(a, b);
                }
                else
                {
                    Console.WriteLine(dsu.IsSame(a, b) ? "Yes" : "No");
                }
            }
        }
Esempio n. 15
0
        public static void Solve()
        {
            var(N, M, S) = Scanner.Scan <int, int, int>();
            var G = new List <int> [N].Select(x => new List <int>()).ToArray();

            for (var i = 0; i < M; i++)
            {
                var(a, b) = Scanner.Scan <int, int>();
                a--; b--;
                if (a > b)
                {
                    (a, b) = (b, a);
                }
                G[a].Add(b);
            }

            var dsu = new DisjointSetUnion(N);

            for (var u = N - 1; u >= S; u--)
            {
                foreach (var v in G[u])
                {
                    dsu.Merge(u, v);
                }
            }

            var answer = new List <int>();

            answer.Add(S);
            S--;
            for (var u = S - 1; u >= 0; u--)
            {
                foreach (var v in G[u])
                {
                    dsu.Merge(u, v);
                }
                if (dsu.IsSame(S, u))
                {
                    answer.Add(u + 1);
                }
            }
            answer.Reverse();
            Printer.Print1D(answer, "\n");
        }
Esempio n. 16
0
        public static void Solve()
        {
            var(N, M) = Scanner.Scan <int, int>();
            var dsu = new DisjointSetUnion(N);
            var deg = new int[N];

            for (var i = 0; i < M; i++)
            {
                var(u, v) = Scanner.Scan <int, int>();
                u--; v--;
                dsu.Merge(u, v);
                deg[u]++;
                deg[v]++;
            }

            var ok = 0;

            foreach (var group in dsu.GetGroups())
            {
                var v = 0;
                var e = 0;
                foreach (var u in group)
                {
                    v++;
                    e += deg[u];
                }

                if (e == v * 2)
                {
                    ok++;
                }
                else
                {
                    Console.WriteLine(0);
                    return;
                }
            }

            var answer = mint.Power(2, ok);

            Console.WriteLine(answer);
        }
Esempio n. 17
0
        public static void Solve()
        {
            var N      = Scanner.Scan <int>();
            var A      = Scanner.ScanEnumerable <int>().ToArray();
            var dsu    = new DisjointSetUnion((int)2e5 + 1);
            var answer = 0;

            for (var i = 0; i < N / 2; i++)
            {
                var(a, b) = (A[i], A[N - 1 - i]);
                if (dsu.IsSame(a, b))
                {
                    continue;
                }
                dsu.Merge(a, b);
                answer++;
            }

            Console.WriteLine(answer);
        }
Esempio n. 18
0
        public static void Solve()
        {
            var(N, M, Q) = Scanner.Scan <int, int, int>();
            var E1 = new Edge[M];
            var E2 = new Edge[Q];

            for (var i = 0; i < M; i++)
            {
                var(a, b, c) = Scanner.Scan <int, int, int>();
                a--; b--;
                E1[i] = new Edge(-1, a, b, c);
            }

            for (var i = 0; i < Q; i++)
            {
                var(a, b, c) = Scanner.Scan <int, int, int>();
                a--; b--;
                E2[i] = new Edge(i, a, b, c);
            }

            var answer = new bool[Q];
            var dsu    = new DisjointSetUnion(N);

            foreach (var e in E1.Concat(E2).OrderBy(x => x.Cost))
            {
                if (dsu.IsSame(e.U, e.V))
                {
                    continue;
                }
                if (e.ID == -1)
                {
                    dsu.Merge(e.U, e.V);
                }
                else
                {
                    answer[e.ID] = true;
                }
            }

            Console.WriteLine(string.Join("\n", answer.Select(x => x ? "Yes" : "No")));
        }
Esempio n. 19
0
        public static void Solve()
        {
            var(N, M) = Scanner.Scan <int, int>();
            var dsu  = new DisjointSetUnion(N + M);
            var used = new bool[M];

            for (var i = 0; i < N; i++)
            {
                var L = Scanner.ScanEnumerable <int>().Skip(1).Select(x => x - 1).ToArray();
                foreach (var l in L)
                {
                    dsu.Merge(i, N + l);
                    used[l] = true;
                }
            }

            var no     = used.Count(x => !x);
            var answer = dsu.SizeOf(0) == N + M - no;

            Console.WriteLine(answer ? "YES" : "NO");
        }
Esempio n. 20
0
        public static void Solve()
        {
            var N   = Scanner.Scan <int>();
            var dsu = new DisjointSetUnion(N);

            for (var i = 0; i < N; i++)
            {
                var a = Scanner.Scan <int>() - 1;
                dsu.Merge(i, a);
            }

            foreach (var group in dsu.GetGroups())
            {
                if (group.Count() % 2 == 1)
                {
                    Console.WriteLine(-1);
                    return;
                }
            }

            Console.WriteLine(N / 2);
        }
Esempio n. 21
0
        public static void Solve()
        {
            var(N, M) = Scanner.Scan <int, int>();
            var G = new List <int> [N].Select(x => new List <int>()).ToArray();

            for (var i = 0; i < M; i++)
            {
                var(a, b) = Scanner.Scan <int, int>();
                a--; b--;
                if (a > b)
                {
                    (a, b) = (b, a);
                }
                G[a].Add(b);
            }

            var answer = new List <int>(N);
            var dsu    = new DisjointSetUnion(N);
            var curr   = 0;

            for (var u = N - 1; u >= 0; u--)
            {
                answer.Add(curr);
                curr++;
                foreach (var v in G[u])
                {
                    if (!dsu.IsSame(u, v))
                    {
                        curr--;
                    }
                    dsu.Merge(u, v);
                }
            }

            answer.Reverse();
            Console.WriteLine(string.Join("\n", answer));
        }
Esempio n. 22
0
        public static void Solve()
        {
            var(N, M) = Scanner.Scan <int, int>();
            var P   = Scanner.ScanEnumerable <int>().Select(x => x - 1).ToArray();
            var dsu = new DisjointSetUnion(N);

            for (var i = 0; i < M; i++)
            {
                var(x, y) = Scanner.Scan <int, int>();
                dsu.Merge(x - 1, y - 1);
            }

            var answer = 0;

            for (var i = 0; i < N; i++)
            {
                if (dsu.IsSame(P[i], i))
                {
                    answer++;
                }
            }

            Console.WriteLine(answer);
        }
Esempio n. 23
0
        public static void Solve()
        {
            var(N, M) = Scanner.Scan <int, int>();
            var A = Scanner.ScanEnumerable <long>().ToArray();
            var B = Scanner.ScanEnumerable <long>().ToArray();
            // if (A.Sum() != B.Sum()) { Console.WriteLine("No"); return; }
            var answer = true;
            var dsu    = new DisjointSetUnion(N);

            for (var i = 0; i < M; i++)
            {
                var(c, d) = Scanner.Scan <int, int>();
                dsu.Merge(c - 1, d - 1);
            }

            foreach (var group in dsu.GetGroups())
            {
                var sumA = 0L;
                var sumB = 0L;
                foreach (var u in group)
                {
                    sumA += A[u];
                    sumB += B[u];
                }
                if (sumA != sumB)
                {
                    answer = false;
                }
                if (!answer)
                {
                    break;
                }
            }

            Console.WriteLine(answer ? "Yes" : "No");
        }
Esempio n. 24
0
 public static void Solve()
 {
     var(H, W) = Scanner.Scan <int, int>();
     var dsu = new DisjointSetUnion(H * W);
     var G   = new bool[H, W];
     var D4  = new[] { (1, 0), (-1, 0), (0, 1), (0, -1) };
Esempio n. 25
0
        public static void Solve()
        {
            var N       = Scanner.Scan <int>();
            var S1      = Scanner.Scan <string>();
            var S2      = Scanner.Scan <string>();
            var hashset = new HashSet <char>();

            foreach (var c in S1)
            {
                hashset.Add(c);
            }
            foreach (var c in S2)
            {
                hashset.Add(c);
            }
            var dsu = new DisjointSetUnion(char.MaxValue);

            for (var i = 0; i < N; i++)
            {
                dsu.Merge(S1[i], S2[i]);
            }

            var answer = 1L;
            var used   = new bool[char.MaxValue];
            var first  = false;

            for (var i = 0; i < 10; i++)
            {
                first |= dsu.IsSame(i + '0', S1[0]);
                first |= dsu.IsSame(i + '0', S2[0]);
            }
            if (!first)
            {
                answer *= 9;
            }
            used[S1[0]] = used[S2[0]] = true;
            for (var i = 1; i < N; i++)
            {
                if (used[S1[i]] || used[S2[i]])
                {
                    continue;
                }
                var ok = false;
                for (var j = 0; j < 10; j++)
                {
                    ok |= dsu.IsSame(j + '0', S1[i]);
                    ok |= dsu.IsSame(j + '0', S2[i]);
                }
                if (ok)
                {
                    continue;
                }
                used[S1[i]] = used[S2[i]] = true;
                if (first)
                {
                    answer *= 10;
                }
                else if (!dsu.IsSame(S1[0], S1[i]))
                {
                    answer *= 10;
                }
            }
            Console.WriteLine(answer);
        }
Esempio n. 26
0
        public static void Solve()
        {
            var(N, M) = Scanner.Scan <int, int>();
            var NM = N + M;
            var X  = new long[NM];
            var Y  = new long[NM];
            var C  = new long[NM];

            for (var i = 0; i < NM; i++)
            {
                var(x, y, c) = Scanner.Scan <long, long, long>();
                X[i]         = x;
                Y[i]         = y;
                C[i]         = c;
            }

            double GetCost(int u, int v)
            {
                var(dx, dy) = (X[u] - X[v], Y[u] - Y[v]);
                var cost = Math.Sqrt(dx * dx + dy * dy);

                if (C[u] != C[v])
                {
                    cost *= 10;
                }
                return(cost);
            }

            var big = new List <(int U, int V, double C)>();

            for (var i = 0; i < N; i++)
            {
                for (var j = i + 1; j < N; j++)
                {
                    big.Add((i, j, GetCost(i, j)));
                }
            }

            var answer = double.MaxValue;

            for (var s = 0; s < 1 << M; s++)
            {
                var costs = big.ToList();
                for (var i = 0; i < M; i++)
                {
                    if ((s >> i & 1) == 1)
                    {
                        for (var j = 0; j < N; j++)
                        {
                            costs.Add((j, N + i, GetCost(j, N + i)));
                        }

                        for (var j = i + 1; j < M; j++)
                        {
                            if ((s >> j & 1) == 1)
                            {
                                costs.Add((N + i, N + j, GetCost(N + i, N + j)));
                            }
                        }
                    }
                }

                costs.Sort((x, y) => x.C.CompareTo(y.C));
                var dsu = new DisjointSetUnion(NM);

                var cost = 0d;
                foreach (var(u, v, c) in costs)
                {
                    if (dsu.IsSame(u, v))
                    {
                        continue;
                    }
                    cost += c;
                    dsu.Merge(u, v);
                }

                answer = Math.Min(answer, cost);
            }

            Console.WriteLine(answer);
        }
Esempio n. 27
0
        public static void Solve()
        {
            var(N, M) = Scanner.Scan <int, int>();
            var D = Scanner.ScanEnumerable <long>().ToArray();

            if (D.Sum() != (N - 1) * 2)
            {
                Console.WriteLine(-1);
                return;
            }

            var dsu = new DisjointSetUnion(N);
            var G   = new List <int> [N].Select(x => new List <int>()).ToArray();

            for (var i = 0; i < M; i++)
            {
                var(a, b) = Scanner.Scan <int, int>();
                a--; b--;
                G[a].Add(b);
                G[b].Add(a);
                dsu.Merge(a, b);
                D[a]--;
                D[b]--;
            }

            var answers = new List <(int, int)>();
            var queue   = new PriorityQueue <(Queue <int> U, long M)>((x, y) => y.M.CompareTo(x.M));

            foreach (var group in dsu.GetGroups())
            {
                var q = new Queue <int>(group.Where(x => D[x] > 0));
                var s = group.Sum(x => D[x]);
                if (q.Count == 0)
                {
                    Console.WriteLine(-1);
                    return;
                }
                queue.Enqueue((q, s));
            }

            while (queue.Count >= 2)
            {
                var(uq, us) = queue.Dequeue();
                var(vq, vs) = queue.Dequeue();
                var u = uq.Dequeue();
                var v = vq.Dequeue();
                D[u]--;
                D[v]--;
                us--;
                vs--;
                dsu.Merge(u, v);
                answers.Add((u + 1, v + 1));
                if (D[u] > 0)
                {
                    uq.Enqueue(u);
                }
                if (D[v] > 0)
                {
                    vq.Enqueue(v);
                }

                if (us >= vs)
                {
                    while (vq.Count > 0)
                    {
                        uq.Enqueue(vq.Dequeue());
                    }
                    us += vs;
                    queue.Enqueue((uq, us));
                }
                else
                {
                    while (uq.Count > 0)
                    {
                        vq.Enqueue(uq.Dequeue());
                    }
                    vs += us;
                    queue.Enqueue((vq, vs));
                }
            }

            for (var i = 0; i < N; i++)
            {
                for (var j = i + 1; j < N; j++)
                {
                    if (D[i] == 0)
                    {
                        break;
                    }
                    if (D[j] == 0)
                    {
                        continue;
                    }
                    D[i]--;
                    D[j]--;
                    answers.Add((i + 1, j + 1));
                }
            }

            if (D.Any(x => x != 0))
            {
                Console.WriteLine(-1);
                return;
            }

            foreach (var(u, v) in answers)
            {
                Console.WriteLine($"{u} {v}");
            }
        }