結果
問題 | No.1983 [Cherry 4th Tune C] 南の島のマーメイド |
ユーザー | kakel-san |
提出日時 | 2023-10-10 21:26:47 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 895 ms / 4,000 ms |
コード長 | 5,755 bytes |
コンパイル時間 | 3,710 ms |
コンパイル使用メモリ | 116,028 KB |
実行使用メモリ | 134,964 KB |
最終ジャッジ日時 | 2024-09-13 00:19:22 |
合計ジャッジ時間 | 27,107 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 39 ms
27,488 KB |
testcase_01 | AC | 38 ms
25,324 KB |
testcase_02 | AC | 38 ms
27,372 KB |
testcase_03 | AC | 38 ms
27,236 KB |
testcase_04 | AC | 37 ms
25,520 KB |
testcase_05 | AC | 37 ms
25,408 KB |
testcase_06 | AC | 37 ms
25,400 KB |
testcase_07 | AC | 37 ms
27,236 KB |
testcase_08 | AC | 48 ms
27,568 KB |
testcase_09 | AC | 56 ms
32,036 KB |
testcase_10 | AC | 64 ms
31,212 KB |
testcase_11 | AC | 65 ms
31,008 KB |
testcase_12 | AC | 53 ms
30,612 KB |
testcase_13 | AC | 453 ms
68,484 KB |
testcase_14 | AC | 541 ms
75,448 KB |
testcase_15 | AC | 625 ms
78,700 KB |
testcase_16 | AC | 356 ms
72,528 KB |
testcase_17 | AC | 531 ms
75,068 KB |
testcase_18 | AC | 553 ms
80,328 KB |
testcase_19 | AC | 678 ms
85,036 KB |
testcase_20 | AC | 546 ms
79,672 KB |
testcase_21 | AC | 551 ms
80,352 KB |
testcase_22 | AC | 638 ms
81,316 KB |
testcase_23 | AC | 884 ms
101,412 KB |
testcase_24 | AC | 853 ms
101,672 KB |
testcase_25 | AC | 867 ms
99,624 KB |
testcase_26 | AC | 862 ms
99,380 KB |
testcase_27 | AC | 895 ms
101,820 KB |
testcase_28 | AC | 886 ms
101,280 KB |
testcase_29 | AC | 879 ms
99,752 KB |
testcase_30 | AC | 868 ms
101,300 KB |
testcase_31 | AC | 848 ms
99,492 KB |
testcase_32 | AC | 883 ms
99,364 KB |
testcase_33 | AC | 37 ms
25,280 KB |
testcase_34 | AC | 415 ms
76,836 KB |
testcase_35 | AC | 656 ms
123,828 KB |
testcase_36 | AC | 649 ms
89,392 KB |
testcase_37 | AC | 37 ms
25,264 KB |
testcase_38 | AC | 239 ms
41,180 KB |
testcase_39 | AC | 738 ms
134,964 KB |
testcase_40 | AC | 699 ms
92,924 KB |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
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)]; } } }