完全无向图 CodeForces
来源:互联网 发布:如何在dos下运行java 编辑:程序博客网 时间:2024/04/30 05:54
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 andY-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 31 33 41 4
YES
4 43 12 33 41 2
NO
10 44 35 108 91 2
YES
3 21 22 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");}
- 完全无向图 CodeForces
- 有向图,无向图,连通图,完全图
- hdu 1941 Justice League 无向完全图
- codeforces #80div2 C【无向图判单环】
- codeforces 508D (无向图欧拉路径)
- 证明无向完全图的边数等于N阶取二(Cn2)
- 图--无向图
- 无向图+BFS
- 无向图+DFS
- 无向图
- 无向图邻接矩阵
- 无向图
- 加权无向图
- 加权无向图
- 邻接矩阵无向图
- 邻接矩阵--无向图
- chapter2-无向图
- 无向图.md
- arpspoof
- main_PPT
- Emacs/Vim 使用摘记
- Linux学习(1)——解决端口占用问题
- 2007年开关稳压电源设计报告!!!
- 完全无向图 CodeForces
- textRank杂谈
- Java工厂设计模式
- css清除浮动的三种方法
- PPT.h
- C++之函数重载
- 机器学习之 神经网络的实现(二)-->手写识别
- PPT.cpp
- 安装fusionPBX