結果

問題 No.583 鉄道同好会
ユーザー face4
提出日時 2018-09-02 17:01:11
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 60 ms / 2,000 ms
コード長 1,093 bytes
コンパイル時間 770 ms
コンパイル使用メモリ 85,388 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-10-01 20:36:55
合計ジャッジ時間 1,724 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

// domino?
#include<iostream>
#include<vector>
#include<queue>
#include<set>
using namespace std;

int s[500] = {};
vector<vector<int>> train(500);
bool visit[500] = {};
set<int> stations;

int main(){
    int n, m;
    cin >> n >> m;

    int a, b;
    for(int i = 0; i < m; i++){
        cin >> a >> b;
        s[a]++;
        s[b]++;
        train[a].push_back(b);
        train[b].push_back(a);
        stations.insert(a);
        stations.insert(b);
    }

    // 全ての列車にちょうど1回ずつ乗れるかどうかの判定
    int odd = 0;
    for(int i = 0; i < n; i++){
        odd += (s[i] % 2);
    }

    bool rule1 = (odd == 0 || odd == 2);

    // 連結判定
    int cnt = 0;
    queue<int> q;
    q.push(a);
    while(!q.empty()){
        int now = q.front(); q.pop();
        if(visit[now])  continue;

        visit[now] = true;
        cnt++;

        for(int x : train[now]) if(!visit[x])   q.push(x);
    }

    bool rule2 = (cnt == stations.size());

    if(rule1 && rule2)  cout << "YES" << endl;
    else                cout << "NO" << endl;

    return 0;
}
0