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. NTP配置实践

    前言 NTP(Network Time Protocol,网络时间协议)是用来使网络中的各个计算机时间同步的一种协议.不管是平时使用的私人计算机还是在工作中搭建的服务器集群.时间的统一性和准确性是十分 ...

  2. [think in java]知识点学习

    java中 全部数值都有正负号,不存在无符号整数. java中的基本类型存储在堆栈中. 其它对象存储在堆中. java确保数组会被初始化,并且不能在它的范围之外被訪问. 下面代码在c和c++中是合法的 ...

  3. [转]Laravel 4之Eloquent ORM

    Laravel 4之Eloquent ORM http://dingjiannan.com/2013/laravel-eloquent/ 定义Eloquent模型 模型通常放在app/models目录 ...

  4. 推荐一本好书给即将走入工作的程序员and程序媴

    近期买了几本IT届推崇的经典书籍.当中有一本<程序猿修炼之道:专业程序猿必知的33个技巧>.由于这本比較薄,所以先翻着看. 这本书有别于其它的技术书籍,事实上算不上一本技术书籍.它不是教你 ...

  5. poj1562--Oil Deposits

    Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...

  6. HTML中将背景颜色渐变

    通过使用 css3 渐变可以让背景两个或多个指定的颜色之间显示平稳的过渡,由于用到css3所以需要考虑下浏览器兼容问题,例如:从左到右的线性渐变,且带有透明度的样式:#grad {background ...

  7. 如何用浏览器调试js代码

    按F12打开调试工具

  8. sqlserver2012一直显示正在还原(Restoring)和从单用户转换成多用户模式(单用户连接中)

    如果不需要还原,则使用: restore database test with recovery如果只需要还原,则使用: restore database test with norecovery U ...

  9. c++设计模式之策略模式

    概念:通过定义一系列封装的算法,使得调度者可以互相替换,此模式让算法的变化,不会影响到使用算法的客户. 特点: 1)根据不同的情况创建不同的对象. 2)每个对象的方法名相同,但实现却不同. 结构: 1 ...

  10. UVA 11754 Code Feat (枚举,中国剩余定理)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud C Code Feat   The government hackers at C ...