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?

InputThe first line contains a number T(T≤30)T(T≤30)——The number of the testcases. 

For each testcase, the first line contains a number n(n≤100)n(n≤100). 

Then n+1 lines follow. Each line contains two numbers u,vu,v , which means there is an edge between u and v.OutputFor each testcase, print a single number.Sample Input

1
3
1 2
2 3
3 1
1 3

Sample Output

9

题意:给出n个点,和n+1条边,问可以有多少种去掉边的方法,使去掉边后整个图仍然是连通的

题解:使用并查集来判断是否连通,再通过逐个枚举去掉一条边和去掉两条边的情况,判断整个图是否连通,如果是则ans++ 否则ans不变

AC代码:

#include<iostream>
#include<cstdio> using namespace std; int s[105], e[105];
int t, n;
int a, b;
int pre[105]; int Find(int r) {
return pre[r] = pre[r] == r ? r : Find(pre[r]);
} int check(int a, int b) {
for (int i = 1; i <= n; i++) {
pre[i] = i;
}
for (int i = 0; i <= n; i++) {
//与a , b 相连的边直接去掉,查看是否还能够全部联通
if (i == a || i == b)
continue;
int f1 = Find(s[i]), f2 = Find(e[i]);
if (f1 != f2)
pre[f1] = f2;
}
int cnt = 0;
for (int i = 1; i <= n; i++) {
if (pre[i] == i)
cnt++;
if (cnt > 1)
return 0;
}
return 1;
}
int main() {
cin >> t;
while (t--) {
cin >> n;
for (int i = 0; i <= n; i++) {
cin >> s[i] >> e[i];
}
int ans = 0;
//逐个查找,i = j 代表是取一条边,不等代表是取两条边
//要想全部联通至少需要n-1条边
for (int i = 0; i <= n; i++) {
for (int j = i; j <= n; j++) {
ans += check(i, j);
}
}
cout << ans << endl;
}
return 0;
}

  

B - Rikka with Graph HDU - 5631 (并查集+思维)的更多相关文章

  1. hdu 4514 并查集+树形dp

    湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  2. HDU 3926 并查集 图同构简单判断 STL

    给出两个图,问你是不是同构的... 直接通过并查集建图,暴力用SET判断下子节点个数就行了. /** @Date : 2017-09-22 16:13:42 * @FileName: HDU 3926 ...

  3. HDU 4496 并查集 逆向思维

    给你n个点m条边,保证已经是个连通图,问每次按顺序去掉给定的一条边,当前的连通块数量. 与其正过来思考当前这边会不会是桥,不如倒过来在n个点即n个连通块下建图,检查其连通性,就能知道个数了 /** @ ...

  4. 2015多校第6场 HDU 5354 Bipartite Graph CDQ,并查集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5354 题意:求删去每个点后图是否存在奇环(n,m<=1e5) 解法:很经典的套路,和这题一样:h ...

  5. HDU 1232 并查集/dfs

    原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...

  6. HDU 2860 并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=2860 n个旅,k个兵,m条指令 AP 让战斗力为x的加入y旅 MG x旅y旅合并为x旅 GT 报告x旅的战斗力 ...

  7. Mr. Kitayuta's Colorful Graph 多维并查集

    Mr. Kitayuta's Colorful Graph 并查集不仅可以用于一维,也可以用于高维. 此题的大意是10W个点10W条边(有多种颜色),10W个询问:任意两个节点之间可以由几条相同颜色的 ...

  8. hdu_5354_Bipartite Graph(cdq分治+并查集判二分图)

    题目链接:hdu_5354_Bipartite Graph 题意: 给你一个由无向边连接的图,问对于每一个点来说,如果删除这个点,剩下的点能不能构成一个二分图. 题解: 如果每次排除一个点然后去DFS ...

  9. hdu 1198 (并查集 or dfs) Farm Irrigation

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1198 有题目图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种土地块组成,需要浇 ...

随机推荐

  1. 设置ViewPager 自动滑动时间,速度 方便展示动画

    ViewPager.setCurrentItem(position),即使已设置动画,但是没有动画效果 原因:因为ViewPager滑动之前的时间间隔太短,可以通过反射,去修改ViewPager自动滑 ...

  2. [MIT 18.06 线性代数]Intordution to Vectors向量初体验

    目录 1.1. Vectors and Linear Combinations向量和线性组合 REVIEW OF THE KEY IDEAS 1.2 Lengths and Dot Products向 ...

  3. there is nothing(i春秋CTF题解)

      (1)打开页面,显示为:there is nothing (2)抓包发现提示 (3)hint:ip,Large internal network 意为最大的内网网段IP,hint: ip,Larg ...

  4. kali msf6 更新及bug处理

    问题描述 Metasploit 漏洞库更新,利用msfupdate命令更新,出现已停止该命令更新,出现如下提示: 利用一句话安装更新,命令如下,安装过程中有部分警告出现 curl https://ra ...

  5. WBX24T2X CPEX国产化万兆交换板

      WBX24T2X是基于盛科CTC5160设计的国产化6U三层万兆CPEX交换板,提供24路千兆电口和2路万兆光口,采用龙芯 2K1000处理器.支持常规的L2/L3协议,支持Telnet.SNMP ...

  6. Java基础:运算符

    算数运算符:+,-,*,/,%,++,-- 赋值运算符:= 关系运算符:>,<,>=,<=,==,!=,instanceof 逻辑运算符:&&,||,! 位运算 ...

  7. GTID介绍

    从MySQL5.6开始增加GTID这个特性,Global Transaction ID,全局事务ID,用来强化主从数据库的一致性,故障恢复,以及容错能力,来替代传统的人工的主从复制: 有了GTID,在 ...

  8. Docker 一图胜千言

    Docker 一图胜千言 0 为什么 Docker 比虚拟机快? 注:少了一层抽象层( Hypervisor ) 1 Docker Logo 要素: 大海 宿主机 Host 鲸鱼 Docker 集装箱 ...

  9. PTE 准备之 Personal introduction

    Task strategies Be prepared! This is your opportunity to give the admissions officers a first impres ...

  10. python--requests模块详解

    GET请求 首先构造一个最简单的get请求,请求的链接为http://httpbin.org/get import requests 2 r = requests.get("http://h ...