博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU Problem 5631 Rikka with Graph【并查集】
阅读量:6966 次
发布时间:2019-06-27

本文共 2367 字,大约阅读时间需要 7 分钟。

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 1023    Accepted Submission(s): 486

Problem Description
As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them:
Yuta has a non-direct graph with n vertices and n+1 edges. Rikka can choose some of the edges (at least one) and delete them from the graph.
Yuta wants to know the number of the ways to choose the edges in order to make the remaining graph connected.
It is too difficult for Rikka. Can you help her?
 

 

Input
The first line contains a number 
T(T30)——The number of the testcases.
For each testcase, the first line contains a number 
n(n100).
Then n+1 lines follow. Each line contains two numbers 
u,v , which means there is an edge between u and v.
 

 

Output
For each testcase, print a single number.
 

 

Sample Input
1 3 1 2 2 3 3 1 1 3
 

 

Sample Output
9
 

 

Source
 

 

Recommend
hujie   |   We have carefully selected several similar problems for you:            
 

 

一开始被这道题给蒙了一下,要讨论多少种情况。。。。。
后来才知道,数据只有n+1条边,要想成树,最起码要有n-1条边,所以只有两种讨论的情况,删一条边或者删两条边。
#include
#include
using namespace std;int u[121], v[121], par[121];int n, ans;void init() { for(int i = 0;i <= n;i++) { par[i]=i; }}int Find(int x) { if (x == par[x]) return x; return par[x] = Find(par[x]);}void unite(int x, int y) { int fx = Find(x); int fy = Find(y); if(fx != fy) par[fx] = fy;}void dele1(int x) { init(); for(int i=0;i
<= n; i++) { if(par[i] == i) cont++; } if(cont == 1) ans++;}void dele2(int x,int y) { init(); for(int i = 0; i < n + 1; i++) { if(i != x && i != y) { unite(u[i], v[i]); } } int cont = 0; for(int i = 1;i <= n; i++) { if(par[i] == i) cont++; } if(cont == 1) ans++;}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]); } ans = 0; for(int i = 0;i < n+1; i++) { dele1(i); } for(int i = 0;i < n+1; i++) { for(int j = i+1; j < n+1; j++) { dele2(i, j); } } printf("%d\n", ans); } return 0;}

 

 

转载于:https://www.cnblogs.com/cniwoq/p/6770878.html

你可能感兴趣的文章
sql取整函数
查看>>
【详解】嵌入式开发中固件的烧录方式
查看>>
2015下半年学习技术任务计划书
查看>>
在线教学、视频会议 Webus Fox(3) 客户端开发手册
查看>>
快速替换dll命名空间 z
查看>>
HDu 2010 水仙花数
查看>>
AIDL Service Android进程间通信机制
查看>>
android Intent.createChooser 应用选择
查看>>
[转]jQuery插件写法总结以及面向对象方式写法
查看>>
Swift - 自定义UIActivity分享
查看>>
递归算法的数据结构和算法 C++和PHP达到
查看>>
Nagios经check_http监视web申请书server多个tomcat维修
查看>>
Intellij IDEA
查看>>
springMVC乱码问题
查看>>
第六章 插入,更新和删除数据
查看>>
在VS2010上使用C#调用非托管C++生成的DLL文件(图文讲解)
查看>>
Js 类定义的几种方式
查看>>
python之模块cmath
查看>>
java 遍历arrayList的四种方法
查看>>
Activiti系列: 如何添加自定义表单引擎
查看>>