Ejemplo n.º 1
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());
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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);
        }
Ejemplo 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(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);
        }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
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");
        }
Ejemplo n.º 10
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}");
            }
        }