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(图)的更多相关文章

  1. HDU 5631 Rikka with Graph 暴力 并查集

    Rikka with Graph 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5631 Description As we know, Rikka ...

  2. HDU 5631 Rikka with Graph

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

  3. HDU 5422 Rikka with Graph

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

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

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

  5. HDU 6090 Rikka with Graph

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

  6. 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) ...

  7. 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 ...

  8. 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 ...

  9. 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条边,最后一条边的 ...

随机推荐

  1. Android 开发笔记-Eclipse中文乱码

    使用eclipse时经常中文乱码网上搜罗了下解决办法:   使用Eclipse编辑文件经常出现中文乱码或者文件中有中文不能保存的问题,Eclipse提供了灵活的设置文件编码格式的选项,我们可以通过设置 ...

  2. 《Java程序员面试笔试宝典》之组合与继承有什么区别

    组合和继承是面向对象中两种代码复用的方式.组合是指在新类里面创建原有类的对象,重复利用已有类的功能.继承是面向对象的主要特性之一,它允许设计人员根据其它类的实现来定义一个类的实现.组合和继承都允许在新 ...

  3. hdu 1885 Key Task(bfs+状态压缩)

    Problem Description The Czech Technical University years of its existence . Some of the university b ...

  4. VIm变成sublime (转)

    sublime在ubuntu下始终支持不是很好, 特别是对中文输入的支持,还有一些插件在ubuntu下也不能用. 在ubuntu下还是用vim吧.  我们一起把vim变成sublime. 只需要三步 ...

  5. 虚拟化之docker安装篇

    1,docker pull centos     下载centos镜像 docker search centos  搜索镜像 2,docker images           查看本地镜像 3,do ...

  6. EffectiveC#7--选择恒定的原子值类型数据

    1.恒定类型就是一但它们被创建,它们(的值)就是固定的. 恒定类型可以很好的在基于哈希代码的集合上工作.以Object.GetHashCode()方法返回的值,对同一个实例是必须相同的 2.一个客户类 ...

  7. ora-24247:网络访问被访问控制列表(ACL)拒绝

    用dba账户使用下面脚本授予报错账户访问外部网络服务的权限,以SCOTT为例: BEGIN -- Only uncomment the following line if ACL "netw ...

  8. fastclick.js介绍

    原文地址:http://www.uedsc.com/fastclick.html 用途:去掉移动端click事件的300ms的延迟. 延迟为什么存在   …在移动浏览器中,当你点击按钮的单击事件时,将 ...

  9. XMLHttpRequest发送请求

    *open(method,url,async) *send(string)//在使用get请求的时候,是没有主体的,所有的信息都会拼在url当中,所以使用send的时候括号里的string可以为空!如 ...

  10. JavaScript ----------- 组合继承

    继承 实现继承:继承实际的方法.ECMAScript 只支持实现继承,而且其实现基础主要是依靠原型链来实现的. 基本思想是:利用原型来实现一个引用类型继承另外一个引用类型的属性和方法. 原型 - 构造 ...