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 n. m 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 isreasonable if and only if the following condition is satisfied: For every threedistinct members (X, Y, Z), 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.
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.
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).
4 3 1 3 3 4 1 4
YES
4 4 3 1 2 3 3 4 1 2
NO
10 4 4 3 5 10 8 9 1 2
YES
3 2 1 2 2 3
NO
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一定要有边,问你给定的符不符合要求;
思路:难道说最近数论已将我石化?大清早就那么zz;水题并查集[dfs也可以,表达方式不同,思路一样];
搭建满足条件的所有树,统计树中元素为v,则这棵树上的边有且仅有v*(v-1)/2,也即是每个点要和其所在树每个点都有边,其余为NO;
#include <iostream>
#include <cstdio>
#include <map>
#include <vector>
#include <queue>
#include <set>
#include <algorithm>
using namespace std;
const int MAX_N = 150005;
int n,m,a,b;
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;
}
}
int main() {
scanf("%d %d",&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++) {
scanf("%d %d",&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)
printf("YES\n");
else
printf("NO\n");
return 0;
}
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
int vis[150500],degree[150500];
vector<int> mp[150500];
queue<int> s;
int flag;
void Dfs(int u)
{
vis[u]=1;
s.push(u); // dfs存在的必要:统计同一棵树上的元素
for(int i=0;i<mp[u].size();i++)
{
int v=mp[u][i];
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");
}
}