結果

問題 No.2293 無向辺 2-SAT
ユーザー 👑 kakel-sankakel-san
提出日時 2024-02-01 00:21:20
言語 C#
(.NET 8.0.203)
結果
WA  
実行時間 -
コード長 4,544 bytes
コンパイル時間 15,878 ms
コンパイル使用メモリ 158,424 KB
実行使用メモリ 240,860 KB
最終ジャッジ日時 2024-02-01 00:22:34
合計ジャッジ時間 70,019 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
33,444 KB
testcase_01 AC 94 ms
33,572 KB
testcase_02 AC 92 ms
33,572 KB
testcase_03 AC 974 ms
129,856 KB
testcase_04 AC 993 ms
79,668 KB
testcase_05 AC 1,009 ms
79,440 KB
testcase_06 AC 888 ms
79,924 KB
testcase_07 AC 262 ms
77,092 KB
testcase_08 AC 817 ms
78,772 KB
testcase_09 AC 639 ms
79,524 KB
testcase_10 AC 615 ms
81,184 KB
testcase_11 AC 1,051 ms
82,436 KB
testcase_12 AC 847 ms
78,580 KB
testcase_13 AC 838 ms
78,652 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 1,049 ms
84,352 KB
testcase_17 AC 1,193 ms
87,340 KB
testcase_18 AC 926 ms
77,224 KB
testcase_19 AC 538 ms
69,868 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 AC 442 ms
77,064 KB
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (106 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 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, q) = (c[0], c[1]);
        var map = NArr(q);
        var mlist = new List<int[]>();
        var ans = new List<long>();
        var all = Exp(2, n, mod);
        foreach (var li in map)
        {
            if (li[0] == 3)
            {
                ans.AddRange(Sub(n, mlist, all));
                ans.Add(all);
                mlist = new List<int[]>();
            }
            else mlist.Add(li);
        }
        ans.AddRange(Sub(n, mlist, all));
        WriteLine(string.Join("\n", ans));
    }
    static long Exp(long n, long p, int mod)
    {
        long _n = n % mod;
        var _p = p;
        var result = 1L;
        if ((_p & 1) == 1) result *= _n;
        while (_p > 0)
        {
            _n = _n * _n % mod;
            _p >>= 1;
            if ((_p & 1) == 1) result = result * _n % mod;
        }
        return result;
    }
    static int mod = 998_244_353;
    static int half = 499_122_177;
    static List<long> Sub(int n, List<int[]> map, long all)
    {
        if (map.Count == 0) return new List<long>();
        var set = new HashSet<int>();
        foreach (var li in map)
        {
            set.Add(li[1]);
            set.Add(li[2]);
        }
        var list = new List<int>(set);
        list.Sort();
        var dic = new Dictionary<int, int>();
        for (var i = 0; i < list.Count; ++i) dic[list[i]] = i;
        var zeros = new HashSet<int>[list.Count];
        var ones = new HashSet<int>[list.Count];
        for (var i = 0; i < list.Count; ++i)
        {
            zeros[i] = new HashSet<int>{ i };
            ones[i] = new HashSet<int>();
        }
        var uf = new UnionFindTree(list.Count);
        var ans = new List<long>(map.Count);
        var tmp = all;
        foreach (var li in map)
        {
            var a = dic[li[1]];
            var b = dic[li[2]];
            if (uf.IsSameTree(a, b))
            {
                var r = uf.GetRoot(a);
                if (li[0] == 1)
                {
                    if (zeros[r].Contains(a) != zeros[r].Contains(b)) tmp = 0;
                }
                else
                {
                    if (zeros[r].Contains(b) == zeros[r].Contains(a)) tmp = 0;
                }
            }
            else
            {
                var asize = uf.GetSize(a);
                var bsize = uf.GetSize(b);
                uf.Unite(a, b);
                var nr = uf.GetRoot(a);
                var v = asize > bsize ? a : b;
                if (li[0] == 1)
                {
                    foreach (var zi in zeros[v ^ a ^ b]) zeros[v].Add(zi);
                    foreach (var oi in ones[v ^ a ^ b]) ones[v].Add(oi);
                }
                else
                {
                    foreach (var zi in zeros[v ^ a ^ b]) ones[v].Add(zi);
                    foreach (var oi in ones[v ^ a ^ b]) zeros[v].Add(oi);
                }
                zeros[nr] = zeros[v];
                ones[nr] = ones[v];
                tmp = tmp * half % mod;
            }
            ans.Add(tmp);
        }
        return ans;
    }
    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