hdu 5631 Rikka with Graph(图)

n个点最少要n-1条边才能连通,可以删除一条边,最多删除2条边,然后枚举删除的1条边或2条边,用并查集判断是否连通,时间复杂度为O(n^3)
这边犯了个错误,
for(int i=0;i<N;i++){
fa[i]=i;
}
这个将i<=N,导致错误,值得注意
AC代码:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 106
#define inf 1e12
int n,fa[N],a[N],b[N];
void init(){
for(int i=;i<N;i++){
fa[i]=i;
}
}
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d%d",&a[i],&b[i]);
}
int ans=;
for(int i=;i<=n;i++){//删除一条边
init();
int cnt=n;
for(int j=;j<=n;j++){
if(j!=i){
int root1=find(a[j]);
int root2=find(b[j]);
if(root1!=root2){
fa[root1]=root2;
cnt--;
}
}
}
if(cnt==) ans++;
} for(int i=;i<=n;i++){//删除两条边
for(int j=i+;j<=n;j++){
init();
int cnt=n;
for(int k=;k<=n;k++){
if(k!=i && k!=j){
int root1=find(a[k]);
int root2=find(b[k]);
if(root1!=root2){
fa[root1]=root2;
cnt--;
}
}
}
if(cnt==)ans++;
}
}
printf("%d\n",ans); }
return ;
}
贴上别人写bfs代码:
#include<cstdio>
#include<cstring>
int n,m;
struct note{
int to,next;
}a[];
int head[];
bool used[];
bool des[];
int que[];
bool bfs(){
memset(used,false,sizeof(used));
int start=,aim=;
que[aim++]=;
used[]=true;
while(start<aim){
int num=que[start++];
for(int i=head[num];i!=-;i=a[i].next){
if(!des[i])continue;
if(used[a[i].to])continue;
used[a[i].to]=true;
que[aim++]=a[i].to;
}
}
bool judge=true;
for(int i=;i<=n;i++){
if(!used[i])judge=false;
}
return judge;
}
int main(){
int T;
// freopen("5631.txt","r",stdin);
scanf("%d",&T);
while(T--){
scanf("%d",&n);
int coun=;
memset(head,-,sizeof(head));
m=n;
for(int i=;i<=m;i++){
int x,y;
scanf("%d%d",&x,&y);
a[coun].to=y;
a[coun].next=head[x];
head[x]=coun++;
a[coun].to=x;
a[coun].next=head[y];
head[y]=coun++;
}
int ans=;
memset(des,true,sizeof(des));
for(int i=;i<=m;i++){
for(int j=i+;j<=m;j++){
des[i<<]=des[i*+]=des[j*]=des[j*+]=false;
if(bfs())ans++;
des[i<<]=des[i*+]=des[j*]=des[j*+]=true;
}
}
for(int i=;i<=m;i++){
des[i<<]=des[i*+]=false;
if(bfs())ans++;
des[i<<]=des[i*+]=true;
}
printf("%d\n",ans);
}
return ;
}
hdu 5631 Rikka with Graph(图)的更多相关文章
- HDU 5631 Rikka with Graph 暴力 并查集
Rikka with Graph 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5631 Description As we know, Rikka ...
- HDU 5631 Rikka with Graph
如果原图不连通,直接输出0. 如果原图连通,删除X条边之后要保证新图连通,再看数据是n+1条边-->因此,最多只能删去两条边. 因为n=100,可以枚举进行验证,枚举删去每一条边是否连通,枚举删 ...
- HDU 5422 Rikka with Graph
Rikka with Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- HDU 5424——Rikka with Graph II——————【哈密顿路径】
Rikka with Graph II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- HDU 6090 Rikka with Graph
Rikka with Graph 思路: 官方题解: 代码: #include<bits/stdc++.h> using namespace std; #define ll long lo ...
- HDU 6090 Rikka with Graph —— 2017 Multi-University Training 5
Rikka with Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- hdu 5424 Rikka with Graph II(dfs+哈密顿路径)
Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so h ...
- 2017ACM暑期多校联合训练 - Team 5 1006 HDU 5205 Rikka with Graph (找规律)
题目链接 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, s ...
- hdu 5424 Rikka with Graph II (BestCoder Round #53 (div.2))(哈密顿通路判断)
http://acm.hdu.edu.cn/showproblem.php?pid=5424 哈密顿通路:联通的图,访问每个顶点的路径且只访问一次 n个点n条边 n个顶点有n - 1条边,最后一条边的 ...
随机推荐
- c++之 常用类型
C/C++常用类型的范围 C/C++里常用的类型及表示范围如下表所示: 类型 sizeof 表示范围 说明 char 1 -128 - 127 -2^7 - (2^7 - 1) short 2 -32 ...
- UITabBar背景、icon图标颜色、被选中背景设置以及隐藏UITabBar的两种方式
一.对UITabBar背景和icon图标的一些设置 (1)因为直接给UITabBar设置的背景颜色显示的不纯,半透明的感觉,所以,有时候我们可以直接利用纯色的图片作为背景达到想要的效果: (2)给ic ...
- 同一个页面多个CALayer重绘的办法
//知识点,CALayer的重绘,-(void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx 方法,CALayer的渐变色.多个CALa ...
- samba服务器概述
一.samba服务器概述 Samba是一个能让Linux系统应用Microsoft网络通信协议的软件.而SMB是Server Message Block的缩写,即为服务器消息块.SMB主要作为Micr ...
- js中json的转换
//aa='{"id":0,"appId":"app***********Id","appSecret":"a ...
- FlashbackQuery:SCN与timestamp示例
Flashback QueryFlashback 是ORACLE 自9i 就开始提供的一项特性,在9i 中利用oracle 查询多版本一致的特点,实现从回滚段中读取表一定时间内操作过的数据,可用来进行 ...
- html_day1
第一天学习,了解到html的结构和语法. html的语法: 1.所有的html标签都要放在<>尖括号里. 2.标签不分大小写 建议小写 3.标签中的属性与标签名之间要有一个空格,如多个 ...
- IoC容器Autofac正篇之依赖注入(六)
依赖注入,这个专业词我们可以分为两个部分来理解: 依赖,也就是UML中描述事物之间关系的依赖关系,依赖关系描述了事物A在某些情况下会使用到事物B,事物B的变化会影响到事物A: 注入,医生通过针头将药物 ...
- Web系统如何做到读取客户电脑MAC等硬件信息且兼容非IE浏览器
我们在实际Web应用中,可能会遇到“需要限定特定的电脑或用户才能使用系统”的问题. 对于一般情况来说,我们用得最多的可能是使用ActiveX控件的方法来实现,但此方案只适用于IE浏览器.为了能兼容不同 ...
- oracle导出数据显示出现ora-00109或者LRM-00109出错修改办法
出现这种问题是因为日期格式的问题,调整日期格式后就可以了 改成yyyy-mm-dd的格式就好了