結果

問題 No.583 鉄道同好会
ユーザー ianlinathomeianlinathome
提出日時 2017-11-10 06:51:47
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 20 ms / 2,000 ms
コード長 846 bytes
コンパイル時間 288 ms
コンパイル使用メモリ 26,928 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-16 01:29:26
合計ジャッジ時間 1,073 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 6 ms
4,376 KB
testcase_12 AC 8 ms
4,376 KB
testcase_13 AC 7 ms
4,380 KB
testcase_14 AC 8 ms
4,376 KB
testcase_15 AC 9 ms
4,380 KB
testcase_16 AC 16 ms
4,376 KB
testcase_17 AC 20 ms
4,380 KB
testcase_18 AC 20 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
int find(int n,int p[]){
    if(p[n]==n)return n;
    return p[n]=find(p[n],p);
}
int main(){
    int p[1000],n,m,l,r,ans=-1,a[1000];
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++){
        p[i]=-1;
        a[i]=0;
    }
    for(int i=0;i<m;i++){
        scanf("%d%d",&l,&r);
        a[l]++;
        a[r]++;
        if(p[l]==-1)p[l]=l;
        if(p[r]==-1)p[r]=r;
        l=find(l,p);
        r=find(r,p);
        if(l>r)p[l]=r;
        else p[r]=l;
    }
    for(int i=0;i<n;i++){
        if(find(p[i],p)!=-1){
            if(ans==-1)ans=find(p[i],p);
            else if(ans!=find(p[i],p)){
                printf("NO\n");
                return 0;
            }
        }
        else ans=find(p[i],p);
    }
    ans=0;
    for(int i=0;i<n;i++)if(a[i]%2==1)ans++;
    if(ans>2)printf("NO\n");
    else printf("YES\n");
}
0