HDU 5631 Rikka with Graph 暴力 并查集
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 暴力 并查集的更多相关文章
- hdu 2473 Junk-Mail Filter (暴力并查集)
Problem - 2473 为什么标题写的是暴力并查集?因为我的解法跟网上的有所不同,方法暴力很多. 先解释题意,这是一个模拟处理垃圾邮件的问题.垃圾邮件要根据它们的性质进行分类.对于10w个邮件, ...
- hdu 5631 Rikka with Graph(图)
n个点最少要n-1条边才能连通,可以删除一条边,最多删除2条边,然后枚举删除的1条边或2条边,用并查集判断是否连通,时间复杂度为O(n^3) 这边犯了个错误, for(int i=0;i<N;i ...
- HDU 5631 Rikka with Graph
如果原图不连通,直接输出0. 如果原图连通,删除X条边之后要保证新图连通,再看数据是n+1条边-->因此,最多只能删去两条边. 因为n=100,可以枚举进行验证,枚举删去每一条边是否连通,枚举删 ...
- hdu 5458 Stability(树链剖分+并查集)
Stability Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 65535/102400 K (Java/Others)Total ...
- [HDU 3712] Fiolki (带边权并查集+启发式合并)
[HDU 3712] Fiolki (带边权并查集+启发式合并) 题面 化学家吉丽想要配置一种神奇的药水来拯救世界. 吉丽有n种不同的液体物质,和n个药瓶(均从1到n编号).初始时,第i个瓶内装着g[ ...
- 2016蓝桥杯省赛C/C++A组第七题 剪邮票(暴力+并查集)
题意:有12张连在一起的12生肖的邮票.现在你要从中剪下5张来,要求必须是连着的.(仅仅连接一个角不算相连) 分析:暴力+并查集. 1.记录下每个数字所在位置. 2.先枚举各不相同的5个数的所有可能情 ...
- HDU 5422 Rikka with Graph
Rikka with Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- HDU 6090 Rikka with Graph
Rikka with Graph 思路: 官方题解: 代码: #include<bits/stdc++.h> using namespace std; #define ll long lo ...
- HDU 5424——Rikka with Graph II——————【哈密顿路径】
Rikka with Graph II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
随机推荐
- linux编程之消息队列
消息队列是内核地址空间中的内部链表,通过linux内核在各个进程之间传递内容,消息顺序地发送到消息队列中,并且以几种不同的方式 从队列中获取,每个消息队列可以用IPC标识符唯一的进行标识,内核中的消息 ...
- centos 安装flash
linux系统中安装flash插件 linux中安装flashplayer插件的简单方法: 1.下载其中最新版本的播放器,下载地址: http://get.adobe.com/cn/flashplay ...
- 64_l2
libcec-devel-4.0.2-3.fc26.x86_64.rpm 22-May-2017 22:13 27942 libcephfs-devel-10.2.7-2.fc26.i686.rpm ...
- expect 实现iterm2自动加载pem登录跳板机
#!/usr/bin/expect set timeout spawn expect { "connecting (yes/no)?" { send "yes\r&quo ...
- mac系统中实现vitualBox中访问内网端口
第一步,增加外网网段 打开vitualbox后,按管理菜单,点击->主机网络管理器,如图1所示.点击创建,创建下个网络主机. 图1 然后,关掉虚拟机,虚拟机的设置中,找到网络选项卡,然后点击网络 ...
- sql server 学习笔记
1. 修改student表中sdept字段改为varchar类型,长度为30,并且不为空 ) not null 2. 删除student表中的address列 alter table student ...
- C++ STL结构总结
1. 什么是STL 它的全名是stand template library, 标准模板库,主要是将一些结构和算法写成模板,以便能够实现对任意类型的对象都可以操作,而不需要再一次去写一些算法及结构. 它 ...
- 14:django 用户认证系统
django认证系统包含三个部分:用户.权限和分组 安装 django项目默认启用了认证系统,如果不是使用django-admin.py创建项目的可以通过在settings配置文件里面的INSTALL ...
- Longest Substring Without Repeating Characters——经典题
Given a string, find the length of the longest substring without repeating characters. For example, ...
- JQuery 实现 锚点跳转
$('.nav-jump').click(function() { $('html,body').animate( { scrollTop:$($.attr(this, 'href')).offset ...