codeforces 791B Bear and Friendship Condition

B. Bear and Friendship Condition
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures).

There are n members, numbered 1 through nm pairs of members are friends. Of course, a member can't be a friend with themselves.

Let A-B denote that members A and B are friends. Limak thinks that a network is reasonable if and only if the following condition is satisfied: For every three distinct members (XYZ), if X-Y and Y-Z then also X-Z.

For example: if Alan and Bob are friends, and Bob and Ciri are friends, then Alan and Ciri should be friends as well.

Can you help Limak and check if the network is reasonable? Print "YES" or "NO" accordingly, without the quotes.

Input

The first line of the input contain two integers n and m (3 ≤ n ≤ 150 000) — the number of members and the number of pairs of members that are friends.

The i-th of the next m lines contains two distinct integers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi). Members ai and bi are friends with each other. No pair of members will appear more than once in the input.

Output

If the given network is reasonable, print "YES" in a single line (without the quotes). Otherwise, print "NO" in a single line (without the quotes).

Examples
input
4 3
1 3
3 4
1 4
output
YES
input
4 4
3 1
2 3
3 4
1 2
output
NO
input
10 4
4 3
5 10
8 9
1 2
output
YES
input
3 2
1 2
2 3
output
NO
Note

The drawings below show the situation in the first sample (on the left) and in the second sample (on the right). Each edge represents two members that are friends. The answer is "NO" in the second sample because members (2, 3) are friends and members (3, 4) are friends, while members (2, 4) are not.



题意:给定N个点M条链路来描述两个点之间的关系,并且A-B,B-C,那么A-C一定要有边,问你给定的符不符合要求

思路:并查集,把连在一起的统计到一棵树上,然后树上所有点的边数都应该相等

AC代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

int n,m,a,b;
const int MAX_N = 150005;
int fa[MAX_N];
int du[MAX_N];
int ran[MAX_N];

int found(int x) {
    return x == fa[x]?x:fa[x] = found(fa[x]);
}
void bing(int x ,int y) {
    x = found(x);
    y = found(y);
    if(x != y) {
        fa[x] = y;
        //ran[y] = ran[x]+1;
    }
}

int main() {
    cin >> n >> m;
    for(int i = 0; i <=n; i++) {
        fa[i] = i;
        du[i] = 0;
        ran[i] = 0;
    }
    for(int i = 0; i < m; i++) {
        cin >> a >> b;
        bing(a,b);
        du[a]++;
        du[b]++;
    }
    for(int i = 0;i <= n;i++){//当时偷懒没有去算每一个根相连的有多少条边
        int x = found(i);
        ran[x]++;
    }
    int flag = 0;
    for(int i = 0; i <= n; i++) {
         int x = found(i);
         if(du[i] != ran[x]-1){
            flag = 1;
            break;
         }
    }
    if(!flag){
        cout << "YES" << endl;
    }else{
        cout << "NO" << endl;
    }
    return 0;
}
昨晚做的,当时只是过了简单样例,后来重测的的时候错的,自己不偷懒,完全可以检查出来的,也不至于后面错呐,真悲伤!!!

你可能感兴趣的:(并查集,codeforces)