結果

問題 No.2536 同値性と充足可能性
ユーザー 👑 kakel-sankakel-san
提出日時 2023-11-10 23:07:23
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 358 ms / 2,000 ms
コード長 2,461 bytes
コンパイル時間 9,146 ms
コンパイル使用メモリ 156,944 KB
実行使用メモリ 222,252 KB
最終ジャッジ日時 2023-11-10 23:07:39
合計ジャッジ時間 14,514 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 92 ms
33,316 KB
testcase_01 AC 89 ms
33,060 KB
testcase_02 AC 94 ms
33,316 KB
testcase_03 AC 88 ms
33,060 KB
testcase_04 AC 92 ms
33,316 KB
testcase_05 AC 95 ms
33,316 KB
testcase_06 AC 93 ms
33,316 KB
testcase_07 AC 92 ms
33,316 KB
testcase_08 AC 94 ms
33,316 KB
testcase_09 AC 94 ms
33,316 KB
testcase_10 AC 94 ms
33,316 KB
testcase_11 AC 92 ms
33,316 KB
testcase_12 AC 93 ms
33,316 KB
testcase_13 AC 93 ms
33,316 KB
testcase_14 AC 93 ms
33,316 KB
testcase_15 AC 92 ms
33,316 KB
testcase_16 AC 96 ms
33,316 KB
testcase_17 AC 94 ms
33,572 KB
testcase_18 AC 94 ms
33,572 KB
testcase_19 AC 90 ms
33,188 KB
testcase_20 AC 111 ms
36,644 KB
testcase_21 AC 102 ms
36,772 KB
testcase_22 AC 104 ms
36,772 KB
testcase_23 AC 185 ms
62,820 KB
testcase_24 AC 180 ms
62,820 KB
testcase_25 AC 353 ms
75,852 KB
testcase_26 AC 358 ms
74,264 KB
testcase_27 AC 351 ms
76,440 KB
testcase_28 AC 321 ms
73,420 KB
testcase_29 AC 340 ms
75,136 KB
testcase_30 AC 315 ms
222,252 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (102 ms)。
MSBuild のバージョン 17.7.3+8ec440e68 (.NET)
  main -> /home/judge/data/code/bin/Release/net7.0/main.dll
  main -> /home/judge/data/code/bin/Release/net7.0/publish/

ソースコード

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 string[] SList(long n) => Enumerable.Repeat(0, (int)n).Select(_ => ReadLine()).ToArray();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var c = NList;
        var (n, m) = (c[0], c[1]);
        var order = SList(m);
        var tree = new List<(int to, int xor)>[n];
        for (var i = 0; i < tree.Length; ++i) tree[i] = new List<(int to, int xor)>();
        foreach (var o in order)
        {
            var s = o.Split();
            var i = int.Parse(s[0]) - 1;
            var j = int.Parse(s[2]) - 1;
            var xor = s[1] == "<==>" ? 0 : 1;
            tree[i].Add((j, xor));
            tree[j].Add((i, xor));
        }
        var color = Enumerable.Repeat(-1, n).ToArray();
        for (var i = 0; i < n; ++i)
        {
            if (color[i] < 0)
            {
                var dic = new Dictionary<int, int>();
                dic[i] = 0;
                if (!DFS(i, tree, dic))
                {
                    WriteLine("No");
                    return;
                }
                var zeros = dic.Count(kv => kv.Value == 0);
                if (zeros * 2 < dic.Count())
                {
                    foreach (var kv in dic) color[kv.Key] = kv.Value ^ 1;
                }
                else
                {
                    foreach (var kv in dic) color[kv.Key] = kv.Value;
                }
            }
        }
        var list = new List<int>();
        for (var i = 0; i < n; ++i) if (color[i] == 0) list.Add(i + 1);
        WriteLine("Yes");
        WriteLine(list.Count);
        WriteLine(string.Join(" ", list));
    }
    static bool DFS(int cur, List<(int to, int xor)>[] tree, Dictionary<int, int> dic)
    {
        var color = dic[cur];
        foreach (var next in tree[cur])
        {
            if (dic.ContainsKey(next.to))
            {
                if (dic[next.to] != (color ^ next.xor)) return false;
            }
            else
            {
                dic[next.to] = color ^ next.xor;
                if (!DFS(next.to, tree, dic)) return false;
            }
        }
        return true;
    }
}
0