結果
問題 | No.2536 同値性と充足可能性 |
ユーザー | srjywrdnprkt |
提出日時 | 2023-11-10 22:40:44 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,221 bytes |
コンパイル時間 | 2,653 ms |
コンパイル使用メモリ | 220,296 KB |
実行使用メモリ | 10,020 KB |
最終ジャッジ日時 | 2024-09-26 01:57:29 |
合計ジャッジ時間 | 7,888 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | WA | - |
testcase_01 | WA | - |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | AC | 2 ms
5,376 KB |
testcase_20 | AC | 8 ms
5,376 KB |
testcase_21 | AC | 8 ms
5,376 KB |
testcase_22 | AC | 8 ms
5,376 KB |
testcase_23 | AC | 75 ms
5,636 KB |
testcase_24 | AC | 74 ms
5,600 KB |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | WA | - |
testcase_30 | WA | - |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; struct UnionFind { int ngroup, N; vector<int> par, siz, mn; UnionFind(int _N) : N(_N), par(_N), siz(_N), mn(_N) { ngroup = _N; for(int i = 0; i < N; i++) par[i] = i, siz[i] = 1, mn[i] = i; } int root(int x) { if (par[x] == x) return x; return par[x] = root(par[x]); } int unite(int x, int y) { int rx = root(x), ry = root(y); if (rx == ry) return rx; ngroup--; if (siz[rx] > siz[ry]) swap(rx, ry); par[rx] = ry; siz[ry] += siz[rx]; mn[ry] = min(mn[rx], mn[ry]); return ry; } bool same(int x, int y) { int rx = root(x), ry = root(y); return rx == ry; } int size(int x) {return siz[root(x)];} int mindex (int x) {return mn[root(x)];} //xを含む連結成分の最小の頂点番号 int group_count() {return ngroup;} vector<vector<int>> groups(){ vector<int> rev(N); int nrt=0; for (int i=0; i<N; i++) if (root(i) == i) rev[i] = nrt, nrt++; vector<vector<int>> res(nrt); for (int i=0; i<N; i++) res[rev[root(i)]].push_back(i); return res; } }; int main(){ int N, M, x, y, cnt=0; cin >> N >> M; vector<vector<int>> E(N); UnionFind tree(N); vector<int> g(N), color(N, -1); string S; vector<pair<int, int>> a, b; for (int i=0; i<M; i++){ cin >> x >> S >> y; x--; y--; if (S == "<==>") a.push_back({x, y}); else b.push_back({x, y}); } for (auto [x, y] : b){ E[x].push_back(y); E[y].push_back(x); } auto dfs=[&](auto self, int from, int c)->void{ color[from] = c; g[from] = cnt; for (auto to : E[from]){ if (color[to] != -1){ if (color[from] == color[to]){ cout << "No" << endl; exit(0); } else continue; } self(self, to, 1-c); } }; for (int i=0; i<N; i++){ if (color[i] == -1 && E[i].size() > 1){ dfs(dfs, i, 0); cnt++; } } for (auto [x, y] : a){ if (color[tree.root(x)] == -1 && color[tree.root(y)] == -1) tree.unite(x, y); else if (color[tree.root(x)] != -1 && color[tree.root(y)] != -1){ if (color[tree.root(x)] != color[tree.root(y)]){ cout << "No" << endl; return 0; } } else if (color[tree.root(x)] == -1 && color[tree.root(y)] != -1){ color[tree.root(x)] = color[tree.root(y)]; } else{ color[tree.root(y)] = color[tree.root(x)]; } } vector<int> ans, aa, bb; for (int i=0; i<N; i++){ if (color[tree.root(i)] == -1) ans.push_back(i); else if (color[tree.root(i)] == 0) aa.push_back(i); else bb.push_back(i); } if (aa.size() < bb.size()) swap(aa, bb); for (auto x : aa) ans.push_back(x); cout << "Yes" << endl; sort(ans.begin(), ans.end()); cout << ans.size() << endl; for (auto x : ans) cout << x+1 << " "; cout << endl; return 0; }