hiho week 161 register

Ended

Participants:317

Verdict:Accepted
Score:100 / 100
Submitted:2017-07-29 20:59:06

Lang:G++

Edit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<bits/stdc++.h>
using namespace std;
int p[600];
int f(int x){return x==p[x]?x:p[x]=f(p[x]);}
int main()
{
    int T,n,m,x,y;
    cin>>T;
    while(T--)
    {
        cin>>n>>m;
        for(int i=1;i<=n;i++)p[i]=i;
        int ans=0;
        while(m--)
        {
            cin>>x>>y;
            if(f(x)==f(y))ans++;
            p[f(x)]=f(y);
        }
        for(int i=1;i<=n;i++)if(p[i]==i)ans++;
        puts(ans==1?"YES":"NO");
    }
    return 0;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX