結果
問題 | No.583 鉄道同好会 |
ユーザー | dnish |
提出日時 | 2018-06-22 15:10:08 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 50 ms / 2,000 ms |
コード長 | 2,137 bytes |
コンパイル時間 | 1,486 ms |
コンパイル使用メモリ | 168,912 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-06-30 17:57:42 |
合計ジャッジ時間 | 2,432 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 16 |
コンパイルメッセージ
In member function 'int UnionFind::find(int)', inlined from 'bool UnionFind::same(int, int)' at main.cpp:67:31, inlined from 'int main()' at main.cpp:88:20: main.cpp:38:18: warning: 'v' may be used uninitialized [-Wmaybe-uninitialized] 38 | if (par[x] == x) { | ~~~~~^ main.cpp: In function 'int main()': main.cpp:75:9: note: 'v' was declared here 75 | int v; | ^
ソースコード
#include "bits/stdc++.h" #define REP(i,n,N) for(int i=(n); i<(N); i++) #define RREP(i,n,N) for(ll i=(N-1); i>=n; i--) #define CK(n,a,b) ((a)<=(n)&&(n)<(b)) #define ALL(v) (v).begin(),(v).end() #define p(s) cout<<(s)<<endl #define p2(a,b) cout<<(a)<<" "<<(b)<<endl #define v2(T) vector<vector<T>> typedef long long ll; using namespace std; const ll inf = 1e18+7; const int MAX_N = 510; int edge[MAX_N]; struct UnionFind { int par[MAX_N]; // 親ノードの番号 int deph[MAX_N]; // ノードの深さ /* 上以外に持っておくデータ // 例)木であるかどうか(bool値)を持つ場合 // bool istree[MAX_N]; */ UnionFind(int n) { fill(par, par + MAX_N, -1); for (int i = 0; i<n; i++) { par[i] = i; deph[i] = 0; /* 持っておくデータにおいて初期化が必要な場合 // istree[i] = true; */ } } int find(int x) { if (par[x] == x) { return x; } else { return par[x] = find(par[x]); } } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) { /* 同じグループに属しているときの処理 // istree = false; */ return; } if (deph[x] < deph[y]) { par[x] = par[y]; /* ルートをyとする時の処理 */ } else { par[y] = par[x]; if (deph[x] == deph[y]) deph[x]++; /* ルートをxとする時の処理 */ } } bool same(int x, int y) { return find(x) == find(y); } }; int main(){ int N,M; cin>>N>>M; UnionFind uf(N); int v; REP(i,0,M) { int a, b; cin >> a >> b; edge[a]++; edge[b]++; uf.unite(a,b); v = a; } int cnt=0; bool ok=true; REP(i,0,N){ if(edge[i]==0) continue; if(!uf.same(i,v)) ok=false; else if(edge[i]%2) cnt++; } p((ok && (cnt==0||cnt==2)) ?"YES":"NO"); return 0; }