結果
問題 | No.2277 Honest or Dishonest ? |
ユーザー | tnakao0123 |
提出日時 | 2023-05-19 10:55:34 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
RE
|
実行時間 | - |
コード長 | 3,383 bytes |
コンパイル時間 | 921 ms |
コンパイル使用メモリ | 70,636 KB |
実行使用メモリ | 13,472 KB |
最終ジャッジ日時 | 2024-05-10 00:16:30 |
合計ジャッジ時間 | 8,212 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 3 ms
10,880 KB |
testcase_01 | AC | 3 ms
5,504 KB |
testcase_02 | AC | 3 ms
5,632 KB |
testcase_03 | RE | - |
testcase_04 | RE | - |
testcase_05 | AC | 38 ms
11,912 KB |
testcase_06 | RE | - |
testcase_07 | RE | - |
testcase_08 | AC | 40 ms
12,640 KB |
testcase_09 | AC | 55 ms
12,048 KB |
testcase_10 | RE | - |
testcase_11 | RE | - |
testcase_12 | TLE | - |
testcase_13 | -- | - |
testcase_14 | -- | - |
testcase_15 | -- | - |
testcase_16 | -- | - |
testcase_17 | -- | - |
testcase_18 | -- | - |
testcase_19 | -- | - |
testcase_20 | -- | - |
testcase_21 | -- | - |
testcase_22 | -- | - |
testcase_23 | -- | - |
testcase_24 | -- | - |
testcase_25 | -- | - |
testcase_26 | -- | - |
testcase_27 | -- | - |
testcase_28 | -- | - |
testcase_29 | -- | - |
testcase_30 | -- | - |
testcase_31 | -- | - |
testcase_32 | -- | - |
testcase_33 | -- | - |
testcase_34 | -- | - |
testcase_35 | -- | - |
testcase_36 | -- | - |
testcase_37 | -- | - |
testcase_38 | -- | - |
testcase_39 | -- | - |
testcase_40 | -- | - |
testcase_41 | -- | - |
testcase_42 | -- | - |
testcase_43 | -- | - |
testcase_44 | -- | - |
testcase_45 | -- | - |
testcase_46 | -- | - |
testcase_47 | -- | - |
testcase_48 | -- | - |
testcase_49 | -- | - |
testcase_50 | -- | - |
testcase_51 | -- | - |
testcase_52 | -- | - |
コンパイルメッセージ
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]) { | ^
ソースコード
/* -*- 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 u = 0; u < n; u++) if (u == uft.root(u)) ans = (ll)ans * 2 % MOD; printf("%d\n", ans); return 0; }