結果

問題 No.2277 Honest or Dishonest ?
ユーザー tnakao0123tnakao0123
提出日時 2023-05-19 11:00:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,388 bytes
コンパイル時間 800 ms
コンパイル使用メモリ 70,444 KB
実行使用メモリ 14,896 KB
最終ジャッジ日時 2024-05-10 00:18:10
合計ジャッジ時間 5,004 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,504 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 3 ms
5,504 KB
testcase_03 AC 55 ms
10,848 KB
testcase_04 AC 71 ms
13,608 KB
testcase_05 AC 33 ms
11,912 KB
testcase_06 AC 39 ms
9,608 KB
testcase_07 AC 36 ms
9,260 KB
testcase_08 AC 34 ms
12,644 KB
testcase_09 AC 48 ms
12,240 KB
testcase_10 AC 25 ms
8,320 KB
testcase_11 AC 14 ms
6,760 KB
testcase_12 AC 18 ms
6,784 KB
testcase_13 AC 47 ms
10,076 KB
testcase_14 AC 21 ms
6,400 KB
testcase_15 AC 28 ms
10,780 KB
testcase_16 AC 36 ms
9,056 KB
testcase_17 AC 18 ms
7,168 KB
testcase_18 AC 62 ms
13,456 KB
testcase_19 AC 64 ms
13,420 KB
testcase_20 AC 50 ms
11,224 KB
testcase_21 AC 29 ms
8,832 KB
testcase_22 AC 41 ms
10,304 KB
testcase_23 AC 43 ms
9,472 KB
testcase_24 AC 38 ms
10,268 KB
testcase_25 AC 54 ms
13,168 KB
testcase_26 AC 24 ms
11,868 KB
testcase_27 AC 20 ms
7,552 KB
testcase_28 AC 27 ms
10,428 KB
testcase_29 AC 40 ms
12,036 KB
testcase_30 WA -
testcase_31 AC 33 ms
9,276 KB
testcase_32 AC 34 ms
11,648 KB
testcase_33 AC 64 ms
12,804 KB
testcase_34 WA -
testcase_35 AC 17 ms
9,080 KB
testcase_36 WA -
testcase_37 AC 43 ms
10,240 KB
testcase_38 AC 11 ms
6,272 KB
testcase_39 AC 15 ms
7,040 KB
testcase_40 AC 15 ms
8,292 KB
testcase_41 AC 58 ms
11,476 KB
testcase_42 AC 38 ms
10,160 KB
testcase_43 WA -
testcase_44 AC 84 ms
14,760 KB
testcase_45 AC 84 ms
14,752 KB
testcase_46 AC 91 ms
14,880 KB
testcase_47 AC 89 ms
14,896 KB
testcase_48 AC 94 ms
14,892 KB
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'void scc_visit(const vpii*, int, vvi&, si&, vb&, vi&, vi&, int&)':
main.cpp:90:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
   90 |   for (const auto [w, c]: nbrs[v]) {
      |                   ^
main.cpp: In function 'int main()':
main.cpp:151:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
  151 |         for (auto [v, c]: nbrs[u])
      |                   ^
main.cpp:166:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
  166 |     for (auto [v, c]: nbrs[u]) {
      |               ^

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2277.cc:  No.2277 Honest or Dishonest ? - yukicoder
 */

#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<algorithm>
#include<utility>
 
using namespace std;

/* constant */

const int MAX_N = 100000;
const int MAX_QN = 100000;
const int MOD = 998244353;

/* typedef */

typedef long long ll;
typedef vector<int> vi;
typedef queue<int> qi;
typedef pair<int,int> pii;
typedef vector<pii> vpii;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef stack<int> si;

struct UFT {
  vector<int> links, ranks, sizes;
  UFT() {}

  void init(int n) {
    links.resize(n);
    for (int i = 0; i < n; i++) links[i] = i;
    ranks.assign(n, 1);
    sizes.assign(n, 1);
  }

  int root(int i) {
    int i0 = i;
    while (links[i0] != i0) i0 = links[i0];
    return (links[i] = i0);
  }

  int rank(int i) { return ranks[root(i)]; }
  int size(int i) { return sizes[root(i)]; }
  bool same(int i, int j) { return root(i) == root(j); }

  int merge(int i0, int i1) {
    int r0 = root(i0), r1 = root(i1), mr;
    if (r0 == r1) return r0;
    if (ranks[r0] == ranks[r1]) {
      links[r1] = r0;
      sizes[r0] += sizes[r1];
      ranks[r0]++;
      mr = r0;
    }
    else if (ranks[r0] > ranks[r1]) {
      links[r1] = r0;
      sizes[r0] += sizes[r1];
      mr = r0;
    }
    else {
      links[r0] = r1;
      sizes[r1] += sizes[r0];
      mr = r1;
    }
    return mr;
  }
};

/* global variables */

vpii nbrs[MAX_N];
int gids[MAX_N], ds[MAX_N];
UFT uft;

/* subroutines */

void scc_visit(const vpii *nbrs, int v, vvi& scc,
	       si& S, vb &inS, vi& low, vi& num, int& time) {
  low[v] = num[v] = ++time;
  S.push(v);
  inS[v] = true;

  for (const auto [w, c]: nbrs[v]) {
    if (num[w] == 0) {
      scc_visit(nbrs, w, scc, S, inS, low, num, time);
      low[v] = min(low[v], low[w]);
    }
    else if (inS[w])
      low[v] = min(low[v], num[w]);
  }

  if (low[v] == num[v]) {
    scc.push_back(vi());
    for (;;) {
      int w = S.top(); S.pop();
      inS[w] = false;
      scc.back().push_back(w);
      if (v == w) break;
    }
  }
}

void calc_scc(const int n, const vpii nbrs[], vvi& scc) {
  vi num(n), low(n);
  si S;
  vb inS(n);
  int time = 0;

  for (int u = 0; u < n; u++)
    if (num[u] == 0)
      scc_visit(nbrs, u, scc, S, inS, low, num, time);
}

/* main */

int main() {
  int n, qn;
  scanf("%d%d", &n, &qn);

  for (int i = 0; i < qn; i++) {
    int ai, bi, ci;
    scanf("%d%d%d", &ai, &bi, &ci);
    ai--, bi--;
    nbrs[bi].push_back({ai, ci});
  }

  vvi scc;
  calc_scc(n, nbrs, scc);

  int m = scc.size();
  for (int i = 0; i < m; i++)
    for (auto u: scc[i]) gids[u] = i;

  fill(ds, ds + n, -1);
  for (int st = 0; st < n; st++)
    if (ds[st] < 0) {
      ds[st] = 0;
      qi q;
      q.push(st);

      while (! q.empty()) {
	int u = q.front(); q.pop();
	int gu = gids[u];
	for (auto [v, c]: nbrs[u])
	  if (gids[v] == gu) {
	    if (ds[v] < 0) {
	      ds[v] = ds[u] ^ c;
	      q.push(v);
	    }
	    else if (ds[v] != (ds[u] ^ c)) {
	      puts("0"); return 0;
	    }
	  }
      }
    }

  uft.init(m);
  for (int u = 0; u < n; u++)
    for (auto [v, c]: nbrs[u]) {
      int gu = gids[u], gv = gids[v];
      if (gu != gv) uft.merge(gu, gv);
    }

  int ans = 1;
  for (int gu = 0; gu < m; gu++)
    if (gu == uft.root(gu)) ans = (ll)ans * 2 % MOD;

  printf("%d\n", ans);
  return 0;
}
0