HDU5631(并查集)
2016-02-24 14:17
204 查看
题目大意:n个顶点,n+1条边,问删除几个边之后仍是连通图的方案数有多少?
思路:如果图连通的话,只能删1~2条边,而且题目数据小,所以暴力就好。
思路:如果图连通的话,只能删1~2条边,而且题目数据小,所以暴力就好。
#include <cstdio> #include <algorithm> #include <cstring> #include <iostream> #include <cmath> #include <string> using namespace std; int fa[111]; int from[111], to[111]; int n; void init() { for(int i=0; i<=n; i++) { fa[i] = i; } } int find_fa(int x) { return fa[x] == x ? fa[x] : fa[x] = find_fa(fa[x]); } int main() { int T; scanf("%d", &T); while(T--) { scanf("%d", &n); for(int i=0; i<=n; i++) { scanf("%d%d", &from[i], &to[i]); } int sum; int ans = 0; for(int i=0; i<=n; i++) { sum = n; init(); for(int j=0; j<=n; j++) { if(j != i) { int fx = find_fa(from[j]); int fy = find_fa(to[j]); if(fx != fy) { fa[fx] = fy; sum--; } } } if(sum == 1) ans++; } for(int i=0; i<=n; i++) { for(int j=i+1; j<=n; j++) { init(); sum = n; for(int k=0; k<=n; k++) { if(k != i && k != j) { int fx = find_fa(from[k]); int fy = find_fa(to[k]); if(fx != fy) { fa[fx] = fy; sum--; } } } if(sum == 1) ans++; } } printf("%d\n", ans); } return 0; }
相关文章推荐
- Android之项目全局变量的定义
- PAT (Basic Level)1017. A除以B
- Java动态代理、cglib动态代理
- linux 下 apache启动、停止、重启命令
- 同时启动两个jboss端口提示占用问题
- 对待开源的态度
- freemarker切割字符串长度
- Unity 5.X扩展编辑器之打包assetbundle
- Unity 5.X扩展编辑器之打包assetbundle
- redis服务器初始化化
- java_内部类学习笔记(二)
- Struts2 全局结果集
- 网络框架Volley原理分析
- [算法练习]Two Sum
- 自定义Toast样式
- android 获取当前的activity
- JavaScript设计模式经典之命令模式
- C++控制台程序实现暂停功能
- 小鸡 宕机的过程记录在实例告警日志中
- redis.conf配置文件说明