結果

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

ソースコード

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];
map <int,int> mn;

int main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    memset(uf,-1,sizeof(uf));
    int n,m,a,b;
    cin >> n >> m;
    int N = 0;

    for(int i=0;i<m;i++)
    {
        cin >> a >> b;
        if(mn.find(a)==mn.end())
        {
            mn[a] = N++;
        }
        if(mn.find(b)==mn.end())
        {
            mn[b] = N++;
        }        
        a = mn[a];
        b = mn[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