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), ifX-Y and Y-Z then alsoX-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 andm (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 andbi (1 ≤ ai, bi ≤ n, ai ≠ bi). Members ai andbi 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.
题意:确定朋友关系,比如对于(x, y, z),x和y是朋友,x和z是朋友,当y和z也是朋友是,称为满足关系。
解法:其实构成了一个完全无向图,不过单独节点也算符合。用并查集将联通的边建树,然后从1到n查看有没有不符合完全图的(每个点度数等于端点数-1)。
并查集做法:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>
#include <set>
#include <cmath>
#include <cstdlib>
using namespace std;
int par[150010];
int node[150010];
int deg[150010];
int Find(int x)
{
if(par[x] == x) return x;
return par[x] = Find(par[x]);
}
void unite(int x, int y)
{
x = Find(x);
y = Find(y);
if(x == y) return ;
par[x] = y;
}
int main()
{
int x, y, n, m;
memset(node, 0, sizeof(node));
memset(deg, 0, sizeof(deg));
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++)
par[i] = i;
for(int i = 0; i < m; i++) {
scanf("%d%d", &x, &y);
unite(x, y);
deg[x]++;
deg[y]++;
}
for(int i = 1; i <= n; i++)
node[Find(i)]++;
for(int i = 1; i <= n; i++) {
if(deg[i] != node[Find(i)] - 1) {
puts("NO");
return 0;
}
}
puts("YES");
return 0;
}
dfs做法:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>
#include <set>
#include <cmath>
#include <cstdlib>
#include <queue>
using namespace std;
int deg[150010];
int vist[150010];
queue<int> que;
vector<int> v[150010];
bool flag;
void dfs(int s)
{
vist[s] = 1;
que.push(s);
if(flag) return ;
for(int i = 0; i < v[s].size(); i++) {
int u = v[s][i];
if(deg[u] != deg[s]) flag = 1;
if(!vist[u]) dfs(u);
}
}
int main()
{
int n, m, x, y;
scanf("%d%d", &n, &m);
for(int i = 0; i < m; i++) {
scanf("%d%d", &x, &y);
deg[x]++;
deg[y]++;
v[x].push_back(y);
v[y].push_back(x);
}
flag = 0;
for(int i =1; i <= n; i++) {
if(!vist[i]) {
dfs(i);
int tmp = que.size();
while(!que.empty()) {
int u = que.front();
que.pop();
if(deg[u] != tmp-1) {
flag = 1;
break;
}
}
if(flag) break;
}
}
if(flag) puts("NO");
else puts("YES");
}