結果

問題 No.583 鉄道同好会
ユーザー dnishdnish
提出日時 2018-06-22 15:10:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 47 ms / 2,000 ms
コード長 2,137 bytes
コンパイル時間 1,545 ms
コンパイル使用メモリ 168,104 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-13 07:57:03
合計ジャッジ時間 2,603 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,504 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 12 ms
4,376 KB
testcase_12 AC 16 ms
4,380 KB
testcase_13 AC 17 ms
4,380 KB
testcase_14 AC 17 ms
4,380 KB
testcase_15 AC 21 ms
4,380 KB
testcase_16 AC 40 ms
4,380 KB
testcase_17 AC 47 ms
4,376 KB
testcase_18 AC 47 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
メンバ関数 ‘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: 警告: ‘v’ may be used uninitialized [-Wmaybe-uninitialized]
   38 |         if (par[x] == x) {
      |             ~~~~~^
main.cpp: 関数 ‘int main()’ 内:
main.cpp:75:9: 備考: ‘v’ はここで定義されています
   75 |     int v;
      |         ^

ソースコード

diff #

#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;
}
0