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. 利用Python 发送邮件

    概要 我们都知道SMTP(简单邮件传输协议),是一组用于从原地址到目的地址传输邮件的规范,通过它来控制邮件的中转方式.SMTP规定电子邮件应该如何格式化.如何加密,以及如何在邮件服务器之间传递.SMT ...

  2. java===java基础学习(5)---文件读取,写入操作

    文件的写入读取有很多方法,今天学到的是Scanner和PrintWriter 文件读取 Scanner in = new Scanner(Paths.get("file.txt") ...

  3. 2017多校第9场 HDU 6162 Ch’s gift 树剖加主席树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6162 题意:给出一棵树的链接方法,每个点都有一个数字,询问U->V节点经过所有路径中l < ...

  4. Mysql安装发生「Access denied for user ‘root’@’localhost’ (using password: NO)」错误

    参考:http://www.aipacommander.com/entry/2014/05/26/152247 mysql_secure_installation 依赖重置密码

  5. 1.Python3标准库--前戏

    Python有一个很大的优势便是在于其拥有丰富的第三方库,可以解决很多很多问题.其实Python的标准库也是非常丰富的,今后我将介绍一下Python的标准库. 这个教程使用的书籍就叫做<Pyth ...

  6. Hierarchical Attention Based Semi-supervised Network Representation Learning

    Hierarchical Attention Based Semi-supervised Network Representation Learning 1. 任务 给定:节点信息网络 目标:为每个节 ...

  7. Python+Selenium 自动化实现实例-Css捕捉元素的几种方法

    #coding=utf-8 from selenium import webdriverimport timedriver = webdriver.Chrome()driver.get("h ...

  8. 【机器学习】k-近邻算法以及算法实例

    机器学习中常常要用到分类算法,在诸多的分类算法中有一种算法名为k-近邻算法,也称为kNN算法. 一.kNN算法的工作原理 二.适用情况 三.算法实例及讲解 ---1.收集数据 ---2.准备数据 -- ...

  9. Android与html5交互 -- WebView使用(一)

    Android中使用WebView可加载html5,具体步骤如下: (前提:本地Html5存放到assets文件夹下) 一:使用WebView加载Html5,简单显示 1:清单文件中添加访问权限:an ...

  10. WebMail

    try { WebMail.SmtpServer = "smtp.example.com";      WebMail.SmtpPort = ;      WebMail.Enab ...