結果

問題 No.1983 [Cherry 4th Tune C] 南の島のマーメイド
ユーザー 👑 kakel-sankakel-san
提出日時 2023-10-10 21:26:47
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 875 ms / 4,000 ms
コード長 5,755 bytes
コンパイル時間 6,996 ms
コンパイル使用メモリ 113,656 KB
実行使用メモリ 130,152 KB
最終ジャッジ日時 2023-10-10 21:27:20
合計ジャッジ時間 28,570 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
23,944 KB
testcase_01 AC 68 ms
24,004 KB
testcase_02 AC 68 ms
22,028 KB
testcase_03 AC 68 ms
24,040 KB
testcase_04 AC 69 ms
21,916 KB
testcase_05 AC 67 ms
21,908 KB
testcase_06 AC 68 ms
22,000 KB
testcase_07 AC 68 ms
22,008 KB
testcase_08 AC 78 ms
24,052 KB
testcase_09 AC 86 ms
29,092 KB
testcase_10 AC 94 ms
30,240 KB
testcase_11 AC 94 ms
29,828 KB
testcase_12 AC 84 ms
29,364 KB
testcase_13 AC 463 ms
63,292 KB
testcase_14 AC 531 ms
68,328 KB
testcase_15 AC 583 ms
77,472 KB
testcase_16 AC 365 ms
69,468 KB
testcase_17 AC 506 ms
69,876 KB
testcase_18 AC 515 ms
75,160 KB
testcase_19 AC 674 ms
77,628 KB
testcase_20 AC 509 ms
74,380 KB
testcase_21 AC 549 ms
73,268 KB
testcase_22 AC 625 ms
76,420 KB
testcase_23 AC 835 ms
98,556 KB
testcase_24 AC 850 ms
98,400 KB
testcase_25 AC 875 ms
98,504 KB
testcase_26 AC 826 ms
96,456 KB
testcase_27 AC 837 ms
96,384 KB
testcase_28 AC 847 ms
96,328 KB
testcase_29 AC 848 ms
98,604 KB
testcase_30 AC 832 ms
98,388 KB
testcase_31 AC 835 ms
96,448 KB
testcase_32 AC 867 ms
98,456 KB
testcase_33 AC 67 ms
24,016 KB
testcase_34 AC 408 ms
71,700 KB
testcase_35 AC 684 ms
118,852 KB
testcase_36 AC 624 ms
86,268 KB
testcase_37 AC 68 ms
22,036 KB
testcase_38 AC 281 ms
38,264 KB
testcase_39 AC 729 ms
130,152 KB
testcase_40 AC 684 ms
88,396 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static int NN => int.Parse(ReadLine());
    static int[] NList => ReadLine().Split().Select(int.Parse).ToArray();
    static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var c = NList;
        var (n, m, q) = (c[0], c[1], c[2]);
        var map = NArr(m);
        var query = NArr(q);

        var tree = new List<int>[n];
        for (var i = 0; i < tree.Length; ++i) tree[i] = new List<int>();
        foreach (var edge in map)
        {
            tree[edge[0] - 1].Add(edge[1] - 1);
            tree[edge[1] - 1].Add(edge[0] - 1);
        }

        var g = new SCCGraph(n);
        var visited = new bool[n];
        for (var i = 0; i < n; ++i) if (!visited[i]) DFS(-1, i, tree, g, visited);
        var sccList = g.SCC();
        var groups = new int[n];
        for (var i = 0; i < sccList.Length; ++i)
        {
            foreach (var e in sccList[i]) groups[e] = i;
        }

        var uf = new UnionFindTree(n);
        foreach (var edge in map)
        {
            if (groups[edge[0] - 1] != groups[edge[1] - 1]) uf.Unite(edge[0] - 1, edge[1] - 1);
        }
        var ans = new bool[q];
        for (var i = 0; i < q; ++i)
        {
            ans[i] = uf.IsSameTree(query[i][0] - 1, query[i][1] - 1);
        }
        WriteLine(string.Join("\n", ans.Select(f => f ? "Yes" : "No")));
    }
    static void DFS(int prev, int cur, List<int>[] tree, SCCGraph g, bool[] visited)
    {
        if (visited[cur]) return;
        visited[cur] = true;
        foreach (var next in tree[cur])
        {
            if (prev == next) continue;
            g.AddEdge(cur, next);
            DFS(cur, next, tree, g, visited);
        }
    }
    class SCCGraph
    {
        private int _n;
        private List<(int from, int to)> edges;
        public SCCGraph(int n)
        {
            _n = n;
            edges = new List<(int, int)>();
        }
        public int NumVertices() { return _n; }
        public void AddEdge(int from, int to) { edges.Add((from, to )); }
        public (int, int[]) SCCIds()
        {
            var g = new CSR<int>(_n, edges);
            var nowOrd = 0;
            var groupNum = 0;
            var visited = new List<int>(_n);
            var low = new int[_n];
            var ord = Enumerable.Repeat(-1, _n).ToArray();
            var ids = new int[_n];
            void DFS(int v)
            {
                low[v] = ord[v] = nowOrd++;
                visited.Add(v);
                for (var i = g.Start[v]; i < g.Start[v + 1]; ++i)
                {
                    var to = g.EList[i];
                    if (ord[to] == -1)
                    {
                        DFS(to);
                        low[v] = Math.Min(low[v], low[to]);
                    }
                    else low[v] = Math.Min(low[v], ord[to]);
                }
                if (low[v] == ord[v])
                {
                    while (true)
                    {
                        var u = visited.Last();
                        visited.RemoveAt(visited.Count - 1);
                        ord[u] = _n;
                        ids[u] = groupNum;
                        if (u == v) break;
                    }
                    ++groupNum;
                }
            }
            for (var i = 0; i < _n; ++i) if (ord[i] == -1) DFS(i);
            for (var i = 0; i < ids.Length; ++i) ids[i] = groupNum - 1 - ids[i];
            return (groupNum, ids);
        }
        public List<int>[] SCC()
        {
            var (groupNum, list) = SCCIds();
            var counts = new int[groupNum];
            foreach (var x in list) ++counts[x];
            var groups = new List<int>[groupNum];
            for (var i = 0; i < groups.Length; ++i) groups[i] = new List<int>(counts[i]);
            for (var i = 0; i < _n; ++i) groups[list[i]].Add(i);
            return groups;
        }
    }
    // 有向辺のリスト
    class CSR<E>
    {
        public int[] Start { get; private set; }
        public E[] EList { get; private set; }
        public CSR(int n, List<(int, E)> edges)
        {
            Start = new int[n + 1];
            EList = new E[edges.Count];
            foreach (var e in edges) ++Start[e.Item1 + 1];
            for (var i = 1; i <= n; ++i) Start[i] += Start[i - 1];
            var counter = (int[])Start.Clone();
            foreach (var e in edges)
            {
                EList[counter[e.Item1]++] = e.Item2;
            }
        }
    }
    class UnionFindTree
    {
        int[] roots;
        public UnionFindTree(int size)
        {
            roots = new int[size];
            for (var i = 0; i < size; ++i) roots[i] = -1;
        }
        public int GetRoot(int a)
        {
            if (roots[a] < 0) return a;
            return roots[a] = GetRoot(roots[a]);
        }
        public bool IsSameTree(int a, int b)
        {
            return GetRoot(a) == GetRoot(b);
        }
        public bool Unite(int a, int b)
        {
            var x = GetRoot(a);
            var y = GetRoot(b);
            if (x == y) return false;
            if (-roots[x] < -roots[y]) { var tmp = x; x = y; y = tmp; }
            roots[x] += roots[y];
            roots[y] = x;
            return true;
        }
        public int GetSize(int a)
        {
            return -roots[GetRoot(a)];
        }
    }
}
0