B. Bear and friendship Condition time limit per test 1 second memory limit per test, megabytes input, input out Put standard output
Bear Limak examines a social network. Its main functionality are that, the can become friends (then they can talk with each other and share funny pictures ).
There is n members, numbered 1 through N. m pairs of the members is friends. Of course, a member can ' t is a friend with themselves.
Let A-B denote that the members A and B. are friends. Limak thinks that a network isreasonable if and only if the following condition is satisfied:for every threedistinct memb ERs (x, y, z), Ifx-y andy-z then alsox-z.
For example:if Alan and Bob is friends, and Bob and Ciri is friends, then Alan and Ciri should is friends as well.
Can Limak and check if the network is reasonable? Print "YES" or "NO" accordingly, without the quotes. Input
The first line of the input contain integers n andm (3≤n≤150,)-the number of members and the number of pairs Of members that is friends.
The i-th of the next m lines contains the distinct integers AI andbi (1≤ai, Bi≤n, Ai≠bi). Members AI ANDBI is friends with each other. No pair of members would appear more than once in the input. Output
If the given network is reasonable, print "YES" on 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
4
4 3
5
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. Each edge represents the friends. The answer is ' NO ' in the second sample because members (2, 3) were friends and members (3, 4) were friends, while members (2, 4) is not. Main topic:
If 1 know 2,2 know 3, it must be required to have: 1 know 3.
If the above conditions are met, output yes, otherwise output No.
Ideas:
Dfs out a unicom block, record the number of midpoint of this unicom block: CNT, then if the degree of these points are cnt-1. Then the connected block is legal, otherwise it is illegal.
The process can be maintained at will.
AC Code:
#include <stdio.h> #include <string.h> #include <vector> #include <queue> using namespace std;
int vis[150500];
int degree[150500];
Vector<int >mp[150500];
int flag;
Queue<int >s;
void Dfs (int u) {vis[u]=1;
S.push (U);
for (int i=0;i<mp[u].size (); i++) {int v=mp[u][i];
if (Degree[v]!=degree[u]) flag=1;
if (vis[v]==0) {Dfs (v);
}}} int main () {int n,m;
while (~SCANF ("%d%d", &n,&m)) {memset (degree,0,sizeof (degree));
for (int i=1;i<=n;i++) mp[i].clear ();
for (int i=0;i<m;i++) {int x, y;
scanf ("%d%d", &x,&y);
Mp[x].push_back (y);
Mp[y].push_back (x);
degree[x]++;
degree[y]++;
} flag=0;
memset (vis,0,sizeof (VIS));
for (int i=1;i<=n;i++) {if (vis[i]==0) {Dfs (i);int tmp=s.size ();
while (!s.empty ()) {int U=s.front ();
S.pop ();
if (degree[u]!=tmp-1) flag=1;
}}} if (flag==1) printf ("no\n");
else printf ("yes\n"); }
}