HDU 5631 Rikka with Graph (并查集)
来源:互联网 发布:迈克尔乔丹生涯数据 编辑:程序博客网 时间:2024/05/19 19:43
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5631
代码:
#include<stdio.h>#include<string.h>using namespace std;int u[121];int v[121];int f[121];int n;int output;int find(int x){ return f[x] == x ? x : (f[x] = find(f[x]));}void merge(int a,int b){ int A,B; A=find(a); B=find(b); if(A!=B) f[B]=A;}void init(){ for(int i=0;i<=n;i++) { f[i]=i; }}void solve(int x){ init(); for(int i=0;i<n+1;i++) { if(i!=x) { merge(u[i],v[i]); } } int cont=0; for(int i=1;i<=n;i++) { if(f[i]==i) cont++; } if(cont==1) output++;}void solve2(int x,int y){ init(); for(int i=0;i<n+1;i++) { if(i!=x&&i!=y) { merge(u[i],v[i]); } } int cont=0; for(int i=1;i<=n;i++) { if(f[i]==i) cont++; } if(cont==1) output++;}int main(){ int t; scanf("%d",&t); while(t--) { scanf("%d",&n); for(int i=0;i<n+1;i++) { scanf("%d%d",&u[i],&v[i]); } output=0; for(int i=0;i<n+1;i++) { solve(i); } for(int i=0;i<n+1;i++) { for(int j=i+1;j<n+1;j++) { solve2(i,j); } } printf("%d\n",output); }}
让 n 个点联通最少需要 n−1 条边,所以最多只能删除两条边,我们可以枚举删除的这两条边(或者唯一的一条边),然后暴力BFS判断连通性就好了。时间复杂度 O(n3)。
0 0
- HDU 5631 Rikka with Graph(并查集)
- HDU 5631 Rikka with Graph (并查集)
- hdu 5631 Rikka with Graph(并查集)
- hdu 5631 Rikka with Graph(并查集)
- hdu 5631 Rikka with Graph(并查集+枚举)
- HDU 5631 Rikka with Graph(并查集)
- 并查集-HDU-5631-Rikka with Graph
- hdu 5631 Rikka with Graph(并查集)
- HDU Problem 5631 Rikka with Graph【并查集】
- HDU 5631 Rikka with Graph 并查集
- hdu 5631 Rikka with Graph【并查集】
- HDU Rikka with Graph (并查集+枚举)
- Rikka with Graph(并查集)
- hdoj Rikka with Graph 5631 (并查集)
- HDU 5631 (BestCoder Round #73 (div.1) 1001)Rikka with Graph(并查集)
- HDU5631--Rikka with Graph--并查集
- HDOJ 5631 Rikka with Graph(并查集)
- hdoj 5631 Rikka with Graph 【并查集】
- gdb调试——set
- 搭建hive
- 广度优先搜索
- POJ 2533 —— Longest Ordered Subsequence 最长上升子序列裸题
- 优秀的编码习惯
- HDU 5631 Rikka with Graph (并查集)
- VMware虚拟机重启系统缓慢并显示piix4_smbus: Host SMBus controller not enabled!
- J2EE十三种技术规范
- HDU 1506 Largest Rectangle in a Histogram(单调队列)
- ***基于协同过滤,NMF和Baseline的推荐算法
- [Python入门]Chapter5 条件语句和递归
- OS X_常用命令行
- 【DP】BZOJ4347[POI2016]Nim z utrudnieniem
- 搭建Hbase集群