結果
問題 | No.1023 Cyclic Tour |
ユーザー |
![]() |
提出日時 | 2020-04-10 22:58:48 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,214 bytes |
コンパイル時間 | 1,417 ms |
コンパイル使用メモリ | 125,724 KB |
実行使用メモリ | 21,504 KB |
最終ジャッジ日時 | 2024-09-15 23:32:37 |
合計ジャッジ時間 | 7,958 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 35 WA * 14 |
ソースコード
#include <iostream> #include <vector> #include <map> #include <set> #include <unordered_set> #include <unordered_map> #include <stack> #include <queue> #include <algorithm> #include <cassert> #include <random> #include <iomanip> #include <bitset> #define FOR(i, n, m) for(ll i = n; i < (int)m; i++) #define REP(i, n) FOR(i, 0, n) #define ALL(v) v.begin(), v.end() #define pb push_back using namespace std; using ll = long long; using P = pair<ll, ll>; constexpr ll inf = 1000000000; constexpr ll mod = 998244353; constexpr long double eps = 1e-9; struct Edge { int to; int ty; }; vector<vector<Edge>> g; vector<vector<int>> ng; void dfs(int v, vector<int>& gr, int c) { gr[v] = c; for(auto elm: g[v]) { if(gr[elm.to] != -1 || elm.ty == 2) continue; dfs(elm.to, gr, c); } return; } bool dfs2(int v, vector<bool>& used) { used[v] = true; bool ret = false; for(auto elm: ng[v]) { if(used[elm]) ret = true; else ret = ret | dfs2(elm, used); } return ret; } int main() { cin.tie(0); ios::sync_with_stdio(false); int n, m; cin >> n >> m; g.resize(n); REP(i, m) { int a, b, c; cin >> a >> b >> c; a--; b--; if(c == 1) { g[a].pb({b, c}); g[b].pb({a, c}); } else { g[a].pb({b, c}); } } vector<int> gr(n, -1), grc, grec; int c = 0; REP(i, n) { if(gr[i] != -1) continue; dfs(i, gr, c); c++; } grc.assign(c, 0); grec.assign(c, 0); ng.resize(c); REP(i, n) { grc[gr[i]]++; for(auto elm: g[i]) { if(gr[i] != gr[elm.to]) { ng[gr[i]].pb(gr[elm.to]); } else { if(elm.ty == 2 || i < elm.to) grec[gr[i]]++; } } } REP(i, c) { if(grc[i] <= grec[i]) { cout << "Yes" << endl; return 0; } } REP(i, c) { sort(ng[i].begin(), ng[i].end()); ng[i].erase(unique(ng[i].begin(), ng[i].end()), ng[i].end()); } vector<bool> used(c, false); if(dfs2(0, used)) cout << "Yes" << endl; else cout << "No" << endl; return 0; }