Rikka with Graph

题目连接:

http://acm.hdu.edu.cn/showproblem.php?pid=5631

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(T≤30)——The number of the testcases.

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

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

Hint

题意

众所周知,萌萌哒六花不擅长数学,所以勇太给了她一些数学问题做练习,其中有一道是这样的:

给出一张 n 个点 n+1 条边的无向图,你可以选择一些边(至少一条)删除。

现在勇太想知道有多少种方案使得删除之后图依然联通。

当然,这个问题对于萌萌哒六花来说实在是太难了,你可以帮帮她吗?

题解:

n个点,n+1条边,显然你最多就只能删除两个点让他成为一棵树

所以我们就直接暴力枚举两个点就好了,然后我们再用并查集去check一下

代码

#include<algorithm>
#include<stdio.h>
#include<math.h>
#include<iostream>
using namespace std;
const int maxn = 150;
int fa[maxn];
int fi(int u){
return u != fa[u] ? fa[u] = fi( fa[u] ) : u;
} void uni(int u ,int v){
int p1 = fi( u ) , p2 = fi( v );
if( p1 != p2 ) fa[p1] = p2;
}
int v[maxn],u[maxn];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;scanf("%d",&n);
int E = n+1;
for(int i=1;i<=E;i++)
scanf("%d%d",&v[i],&u[i]);
int ans = 0;
for(int i=1;i<=E;i++)
{
for(int j=1;j<=n;j++)
fa[j]=j;
for(int j=1;j<=E;j++)
{
if(i==j)continue;
uni(v[j],u[j]);
}
int flag = 1;
for(int j=1;j<=n;j++)
if(fi(fa[j])!=fi(fa[1]))
flag = 0;
if(flag)ans++;
}
for(int i=1;i<=E;i++)
{
for(int j=i+1;j<=E;j++)
{
if(i==j)continue;
for(int k=1;k<=n;k++)
fa[k]=k;
for(int k=1;k<=E;k++)
{
if(k==i)continue;
if(k==j)continue;
uni(v[k],u[k]);
}
int flag = 1;
for(int k=1;k<=n;k++)
if(fi(fa[k])!=fi(fa[1]))
flag = 0;
if(flag)ans++;
}
}
cout<<ans<<endl;
}
}

HDU 5631 Rikka with Graph 暴力 并查集的更多相关文章

  1. hdu 2473 Junk-Mail Filter (暴力并查集)

    Problem - 2473 为什么标题写的是暴力并查集?因为我的解法跟网上的有所不同,方法暴力很多. 先解释题意,这是一个模拟处理垃圾邮件的问题.垃圾邮件要根据它们的性质进行分类.对于10w个邮件, ...

  2. hdu 5631 Rikka with Graph(图)

    n个点最少要n-1条边才能连通,可以删除一条边,最多删除2条边,然后枚举删除的1条边或2条边,用并查集判断是否连通,时间复杂度为O(n^3) 这边犯了个错误, for(int i=0;i<N;i ...

  3. HDU 5631 Rikka with Graph

    如果原图不连通,直接输出0. 如果原图连通,删除X条边之后要保证新图连通,再看数据是n+1条边-->因此,最多只能删去两条边. 因为n=100,可以枚举进行验证,枚举删去每一条边是否连通,枚举删 ...

  4. hdu 5458 Stability(树链剖分+并查集)

    Stability Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)Total ...

  5. [HDU 3712] Fiolki (带边权并查集+启发式合并)

    [HDU 3712] Fiolki (带边权并查集+启发式合并) 题面 化学家吉丽想要配置一种神奇的药水来拯救世界. 吉丽有n种不同的液体物质,和n个药瓶(均从1到n编号).初始时,第i个瓶内装着g[ ...

  6. 2016蓝桥杯省赛C/C++A组第七题 剪邮票(暴力+并查集)

    题意:有12张连在一起的12生肖的邮票.现在你要从中剪下5张来,要求必须是连着的.(仅仅连接一个角不算相连) 分析:暴力+并查集. 1.记录下每个数字所在位置. 2.先枚举各不相同的5个数的所有可能情 ...

  7. HDU 5422 Rikka with Graph

    Rikka with Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  8. HDU 6090 Rikka with Graph

    Rikka with Graph 思路: 官方题解: 代码: #include<bits/stdc++.h> using namespace std; #define ll long lo ...

  9. HDU 5424——Rikka with Graph II——————【哈密顿路径】

    Rikka with Graph II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

随机推荐

  1. MSF爆破MSSQL

    show options: msf auxiliary(scanner/mssql/mssql_login) > show options Module options (auxiliary/s ...

  2. Python3 xml模块的增删改查

    xml数据示例 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 <data>     < ...

  3. linux===进程操作

    ps -ef ps -aux|grep chat.js a:显示所有程序 u:以用户为主的格式来显示 x:显示所有程序,不以终端机来区分 kill -9 nohup python da.py & ...

  4. 工具安装===Sublime Text-安装

    Sublime Text 是一款通用型轻量级编辑器,支持多种编程语言.有许多功能强大的快捷键(如 Ctrl+d),支持丰富的插件扩展.如果平时需要在不同编程语言间切换,那么它将会是一个,不错的选择. ...

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

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

  6. Mac OSX下Appium驱动iPhone真机

    1.安装Xcode.Command Line Tools和Appium. 2.安装brew:/usr/bin/ruby -e "$(curl -fsSL https://raw.github ...

  7. Aspxgridview 根据条件来自定义计算Totalsummery

    protected void ASPxGridView1_CustomSummaryCalculate(object sender, DevExpress.Data.CustomSummaryEven ...

  8. win7下安装 LINUX虚拟机

    文件名: VMware-workstation-full-10.0.6-2700073.exe 百度云共享链接: pan.baidu.com/s/1o6McGmI VMware workstation ...

  9. [转] Socket心跳包异常检测的C语言实现,服务器与客户端代码案例

    转载自:zxh2075的专栏 在Socket心跳机制中,心跳包可以由服务器发送给客户端,也可以由客户端发送给服务器,不过比较起来,前者开销可能较大.本文实现的是由客户端给服务器发送心跳包,服务器不必返 ...

  10. Insertion Sort List——链表的插入排序

    Sort a linked list using insertion sort. 这道题跟 Sort List 类似,要求在链表上实现一种排序算法,这道题是指定实现插入排序.插入排序是一种O(n^2) ...