結果

問題 No.2277 Honest or Dishonest ?
ユーザー magstamagsta
提出日時 2023-04-20 19:32:48
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 273 ms / 2,000 ms
コード長 5,741 bytes
コンパイル時間 7,298 ms
コンパイル使用メモリ 230,768 KB
実行使用メモリ 34,032 KB
最終ジャッジ日時 2024-04-25 18:48:26
合計ジャッジ時間 16,144 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 3 ms
6,816 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 211 ms
26,636 KB
testcase_04 AC 212 ms
28,264 KB
testcase_05 AC 55 ms
15,464 KB
testcase_06 AC 165 ms
21,828 KB
testcase_07 AC 130 ms
18,612 KB
testcase_08 AC 40 ms
17,104 KB
testcase_09 AC 114 ms
19,684 KB
testcase_10 AC 88 ms
15,352 KB
testcase_11 AC 39 ms
8,784 KB
testcase_12 AC 71 ms
11,948 KB
testcase_13 AC 192 ms
23,620 KB
testcase_14 AC 87 ms
11,804 KB
testcase_15 AC 43 ms
13,952 KB
testcase_16 AC 127 ms
17,868 KB
testcase_17 AC 52 ms
10,548 KB
testcase_18 AC 187 ms
25,444 KB
testcase_19 AC 197 ms
28,096 KB
testcase_20 AC 148 ms
21,080 KB
testcase_21 AC 121 ms
15,484 KB
testcase_22 AC 121 ms
18,536 KB
testcase_23 AC 172 ms
20,292 KB
testcase_24 AC 107 ms
19,312 KB
testcase_25 AC 129 ms
21,284 KB
testcase_26 AC 23 ms
13,564 KB
testcase_27 AC 77 ms
13,868 KB
testcase_28 AC 47 ms
13,172 KB
testcase_29 AC 80 ms
17,164 KB
testcase_30 AC 74 ms
12,804 KB
testcase_31 AC 144 ms
19,676 KB
testcase_32 AC 72 ms
16,088 KB
testcase_33 AC 198 ms
28,716 KB
testcase_34 AC 229 ms
28,272 KB
testcase_35 AC 14 ms
9,484 KB
testcase_36 AC 124 ms
19,468 KB
testcase_37 AC 197 ms
22,688 KB
testcase_38 AC 35 ms
7,632 KB
testcase_39 AC 50 ms
9,184 KB
testcase_40 AC 15 ms
8,420 KB
testcase_41 AC 239 ms
27,500 KB
testcase_42 AC 151 ms
20,540 KB
testcase_43 AC 263 ms
34,032 KB
testcase_44 AC 264 ms
32,392 KB
testcase_45 AC 262 ms
32,972 KB
testcase_46 AC 273 ms
31,520 KB
testcase_47 AC 267 ms
33,448 KB
testcase_48 AC 255 ms
32,352 KB
testcase_49 AC 262 ms
32,056 KB
testcase_50 AC 256 ms
31,900 KB
testcase_51 AC 264 ms
32,736 KB
testcase_52 AC 269 ms
31,316 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <set>
#include "testlib.h"
using namespace std;
using Graph = vector<vector<int>>;

// 深さ優先探索
int seen[100010], seen2[100010];
bool dfs(const Graph& G, const Graph& G_, const Graph& G2, const Graph& G2_, int v, bool HonLia) {
    bool flag = false; //矛盾が生じたか
    if (HonLia == true) seen[v] = 2;
    else seen[v] = 1;
    for (auto next_v : G[v]) {
        if (seen[next_v] >= 1) {
            if (HonLia == true && seen[next_v] == 1) flag = true;
            if (HonLia == false && seen[next_v] == 2) flag = true;
            continue;
        }
        if (HonLia == true) { if (dfs(G, G_, G2, G2_, next_v, true) == true) flag = true; } // 再帰的に探索
        else { if (dfs(G, G_, G2, G2_, next_v, false) == true) flag = true; } // 再帰的に探索
    }
    for (auto next_v : G_[v]) {
        if (seen[next_v] >= 1) {
            if (HonLia == true && seen[next_v] == 2) flag = true;
            if (HonLia == false && seen[next_v] == 1) flag = true;
            continue;
        }
        if (HonLia == true) { if (dfs(G, G_, G2, G2_, next_v, false) == true) flag = true; } // 再帰的に探索
        else { if (dfs(G, G_, G2, G2_, next_v, true) == true) flag = true; } // 再帰的に探索
    }
    for (auto next_v : G2[v]) {
        if (seen[next_v] >= 1) {
            if (HonLia == true && seen[next_v] == 1) flag = true;
            if (HonLia == false && seen[next_v] == 2) flag = true;
            continue;
        }
        if (HonLia == true) { if (dfs(G, G_, G2, G2_, next_v, true) == true) flag = true; } // 再帰的に探索
        else { if (dfs(G, G_, G2, G2_, next_v, false) == true) flag = true; } // 再帰的に探索
    }
    for (auto next_v : G2_[v]) {
        if (seen[next_v] >= 1) {
            if (HonLia == true && seen[next_v] == 2) flag = true;
            if (HonLia == false && seen[next_v] == 1) flag = true;
            continue;
        }
        if (HonLia == true) { if (dfs(G, G_, G2, G2_, next_v, false) == true) flag = true; } // 再帰的に探索
        else { if (dfs(G, G_, G2, G2_, next_v, true) == true) flag = true; } // 再帰的に探索
    }
    return flag;
}
bool dfs2(const Graph& G, const Graph& G_, const Graph& G2, const Graph& G2_, int v, bool HonLia) {
    bool flag = false; //矛盾が生じたか
    if (HonLia == true) seen2[v] = 2;
    else seen2[v] = 1;
    for (auto next_v : G[v]) {
        if (seen2[next_v] >= 1) {
            if (HonLia == true && seen2[next_v] == 1) flag = true;
            if (HonLia == false && seen2[next_v] == 2) flag = true;
            continue;
        }
        if (HonLia == true) { if (dfs2(G, G_, G2, G2_, next_v, true) == true) flag = true; } // 再帰的に探索
        else { if (dfs2(G, G_, G2, G2_, next_v, false) == true) flag = true; } // 再帰的に探索
    }
    for (auto next_v : G_[v]) {
        if (seen2[next_v] >= 1) {
            if (HonLia == true && seen2[next_v] == 2) flag = true;
            if (HonLia == false && seen2[next_v] == 1) flag = true;
            continue;
        }
        if (HonLia == true) { if (dfs2(G, G_, G2, G2_, next_v, false) == true) flag = true; } // 再帰的に探索
        else { if (dfs2(G, G_, G2, G2_, next_v, true) == true) flag = true; } // 再帰的に探索
    }
    for (auto next_v : G2[v]) {
        if (seen2[next_v] >= 1) {
            if (HonLia == true && seen2[next_v] == 1) flag = true;
            if (HonLia == false && seen2[next_v] == 2) flag = true;
            continue;
        }
        if (HonLia == true) { if (dfs2(G, G_, G2, G2_, next_v, true) == true) flag = true; } // 再帰的に探索
        else { if (dfs2(G, G_, G2, G2_, next_v, false) == true) flag = true; } // 再帰的に探索
    }
    for (auto next_v : G2_[v]) {
        if (seen2[next_v] >= 1) {
            if (HonLia == true && seen2[next_v] == 2) flag = true;
            if (HonLia == false && seen2[next_v] == 1) flag = true;
            continue;
        }
        if (HonLia == true) { if (dfs2(G, G_, G2, G2_, next_v, false) == true) flag = true; } // 再帰的に探索
        else { if (dfs2(G, G_, G2, G2_, next_v, true) == true) flag = true; } // 再帰的に探索
    }
    return flag;
}

int main() {
    registerValidation();
    // 頂点数と辺数
    int N = inf.readInt(2, 100000, "N");
    inf.readSpace();
    int Q = inf.readInt(1, 100000, "Q");
    inf.readEoln();

    set<pair<int, int>> Set;

    // グラフ入力受取
    Graph G(N), G_(N), G2(N), G2_(N);
    for (int i = 0; i < Q; ++i) {
        int a = inf.readInt(1, N, "a");
        inf.readSpace();
        int b = inf.readInt(1, N, "b");
        inf.readSpace();
        int c = inf.readInt(0, 1, "c");
        if (a == b) cout << -1 << endl;
        if (c == 0) G[a - 1].push_back(b - 1);
        if (c == 1) G_[a - 1].push_back(b - 1);
        if (c == 0) G2[b - 1].push_back(a - 1);
        if (c == 1) G2_[b - 1].push_back(a - 1);
        if (*(Set.lower_bound(make_pair(a, b))) == make_pair(a, b)) cout << -1 << endl;
        Set.insert(make_pair(a, b));
        inf.readEoln();
    }
    inf.readEof();

    // 全頂点が訪問済みになるまで探索
    long long ans = 1;
    for (int i = 0; i < N; i++) seen[i] = seen2[i] = 0;
    for (int v = 0; v < N; ++v) {
        if (seen[v] >= 1) continue; // v が探索済みだったらスルー
        long long count = 0;
        if (dfs(G, G_, G2, G2_, v, true) == false) count++; // v が未探索なら v を始点とした DFS を行う
        if (dfs2(G, G_, G2, G2_, v, false) == false) count++; // v が未探索なら v を始点とした DFS を行う
        ans = (ans * count) % 998244353;
    }
    cout << ans << endl;
}
0