結果

問題 No.2277 Honest or Dishonest ?
ユーザー lddlinanlddlinan
提出日時 2023-04-21 22:08:25
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 59 ms / 2,000 ms
コード長 2,051 bytes
コンパイル時間 745 ms
コンパイル使用メモリ 91,336 KB
実行使用メモリ 15,940 KB
最終ジャッジ日時 2024-04-25 18:58:22
合計ジャッジ時間 3,791 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
8,896 KB
testcase_01 AC 3 ms
9,920 KB
testcase_02 AC 3 ms
9,924 KB
testcase_03 AC 38 ms
11,620 KB
testcase_04 AC 46 ms
14,788 KB
testcase_05 AC 12 ms
11,588 KB
testcase_06 AC 27 ms
10,952 KB
testcase_07 AC 26 ms
10,820 KB
testcase_08 AC 11 ms
10,568 KB
testcase_09 AC 25 ms
13,124 KB
testcase_10 AC 18 ms
10,564 KB
testcase_11 AC 11 ms
9,476 KB
testcase_12 AC 16 ms
9,412 KB
testcase_13 AC 33 ms
10,692 KB
testcase_14 AC 20 ms
9,372 KB
testcase_15 AC 12 ms
11,464 KB
testcase_16 AC 25 ms
10,568 KB
testcase_17 AC 13 ms
10,436 KB
testcase_18 AC 41 ms
14,528 KB
testcase_19 AC 41 ms
14,276 KB
testcase_20 AC 34 ms
13,508 KB
testcase_21 AC 22 ms
10,436 KB
testcase_22 AC 30 ms
12,868 KB
testcase_23 AC 30 ms
9,836 KB
testcase_24 AC 26 ms
12,612 KB
testcase_25 AC 28 ms
13,376 KB
testcase_26 AC 7 ms
9,796 KB
testcase_27 AC 16 ms
9,284 KB
testcase_28 AC 13 ms
11,336 KB
testcase_29 AC 19 ms
12,488 KB
testcase_30 AC 16 ms
11,076 KB
testcase_31 AC 28 ms
11,336 KB
testcase_32 AC 18 ms
11,032 KB
testcase_33 AC 43 ms
14,408 KB
testcase_34 AC 40 ms
12,868 KB
testcase_35 AC 6 ms
9,216 KB
testcase_36 AC 25 ms
10,820 KB
testcase_37 AC 33 ms
10,308 KB
testcase_38 AC 9 ms
8,744 KB
testcase_39 AC 12 ms
10,176 KB
testcase_40 AC 7 ms
9,316 KB
testcase_41 AC 42 ms
12,484 KB
testcase_42 AC 32 ms
11,588 KB
testcase_43 AC 52 ms
15,556 KB
testcase_44 AC 55 ms
15,940 KB
testcase_45 AC 58 ms
15,304 KB
testcase_46 AC 57 ms
15,332 KB
testcase_47 AC 56 ms
15,684 KB
testcase_48 AC 55 ms
15,304 KB
testcase_49 AC 53 ms
15,940 KB
testcase_50 AC 50 ms
15,296 KB
testcase_51 AC 50 ms
15,680 KB
testcase_52 AC 59 ms
15,276 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <map>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <stack>
#include <algorithm>
#include <array>
#include <unordered_set>
#include <unordered_map>
#include <string>
using namespace std;

bool rcmp(int a, int b) { return a>b; }
typedef long long LL;
class mypcmp {
public:
    bool operator()(const int& a, const int& b) {
        return a<b;
    }
};


int gg[100004];
int gx[100004];
int sk[100004];
int findg(int g) {
    int st=0;
    while(g!=gg[g]) {
        sk[st++]=g;
        g=gg[g];
    }
    while(st) gg[sk[--st]]=g;
    return g;
}
void join(int a, int b) {
    if (gx[a]==gx[b]) gx[a]++;
    if (gx[a]>gx[b]) {
        gg[b]=a;
    } else gg[a]=b;
}
set<int> nei[100004];
char mk[100004];
int que[100004];
int as[100004], bs[100004], cs[100004];
#define MOD 998244353
int main() {
    int n, i, q, a, b, c, j;
    int h, t;
    LL r;
    scanf("%d %d", &n, &q);
    for (i=1; i<=n; i++) { gg[i]=i; gx[i]=0; }
    for (i=1; i<=q; i++) {
        scanf("%d %d %d", &a, &b, &c);
        as[i]=a; bs[i]=b; cs[i]=c;
        if (c==0) {
            a=findg(a); b=findg(b);
            if (a!=b) join(a,b);
        }
    }
    for (i=1; i<=q; i++) {
        a=as[i]; b=bs[i]; c=cs[i];
        if (c==1) {
            a=findg(a); b=findg(b);
            nei[a].insert(b);
            nei[b].insert(a);
        }
    }
    r=1;
    for (i=1; i<=n; i++) mk[i]=0;
    for (i=1; i<=n; i++) {
        a=findg(i);
        if (mk[a]) continue;
        r*=2; r%=MOD;
        h=t=0;
        mk[a]=1;
        que[h++]=a;
        while(t<h) {
            a=que[t++];
            for (auto b: nei[a]) {
                if (mk[b]) {
                    if (mk[a]==mk[b]) { r=-1; break; }
                }  else {
                    if (mk[a]==1) mk[b]=2; else mk[b]=1;
                    que[h++]=b;
                }
            }
            if (r<0) break;
        }
        if (r<0) break;
    }
    if (r<0) r=0;
    printf("%lld\n", r);

    return 0;
}
0