結果

問題 No.583 鉄道同好会
ユーザー snrnsidy
提出日時 2025-09-08 16:40:41
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 935 bytes
コンパイル時間 2,194 ms
コンパイル使用メモリ 193,968 KB
実行使用メモリ 7,716 KB
最終ジャッジ日時 2025-09-08 16:40:45
合計ジャッジ時間 3,210 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2 WA * 1
other AC * 13 WA * 3
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

int uf[501];
int find(int x)
{
    if(uf[x] < 0) return x;
    return uf[x] = find(uf[x]);
}
bool merge(int x,int y)
{
    x = find(x);
    y = find(y);
    if(x==y) return false;
    uf[x] += uf[y];
    uf[y] = x;
    return true;
}
int degree[501];
int main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    memset(uf,-1,sizeof(uf));
    int n,m,a,b;
    cin >> n >> m;
    for(int i=0;i<m;i++)
    {
        cin >> a >> b;
        merge(a,b);
        degree[a]+=1;
        degree[b]+=1;
    }

    int cnt = 0;
    for(int i=0;i<n;i++)
    {
        if(degree[i]%2==1) cnt+=1;
    }

    if(cnt!=0 && cnt!=2)
    {
        cout << "NO" << '\n';
        return 0;
    }

    cnt = 0;
    for(int i=0;i<n;i++)
    {
        if(uf[i] < 0) cnt+=1;
    }

    if(cnt > 1)
    {
        cout << "NO" << '\n';
        return 0;
    }
    cout << "YES" << '\n';
    return 0;   
}
0