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条边,最后一条边的 ...
随机推荐
- <转载>构造函数与拷贝构造函数
原文地址http://www.cnblogs.com/waynelu/archive/2012/07/01/2572264.html 构造函数 构造函数.析构函数与赋值函数是每个类最基本的函数. 对于 ...
- 【强烈推荐】《剑指Offer:名企面试官精讲典型编程题》一书中IT名企经典面试题
各位程序猿: <剑指Offer>一书源自该书作者何海涛坚持更新与编写的博客(http://zhedahht.blog.163.com/),该博客收集整理了大量如微软.Goo ...
- php5.3 appache phpstudy win7win8win10下 运行速度慢
php5.3 appache phpstudy win7win8win10下 运行速度慢 最近在部署服务器以及本地测试的时候发现了一个奇怪的现象,运行PHP程序的时候非常慢,起先以为是网速的原因,后经 ...
- C++中,访问字符串的三种方法
1.用字符数组存放一个字符串 程序1:定义一个字符数组并初始化,然后输出其中的字符串. #include<iostream> using namespace std; int main() ...
- Java实现将指定目录内的指定类型的文件归类
这两天在学Java IO流,正好让我产生了将自己的电子书归类的打算,说做就做,Why not?看着自己所学所用能解决生活中的实际问题,是不是非常有成就感,那是必须的! package DepthSea ...
- [Hapi.js] Replying to Requests
hapi's reply interface is one of it's most powerful features. It's smart enough to detect and serial ...
- BOOST 线程完全攻略 - 基础篇
http://blog.csdn.net/iamnieo/article/details/2908621 2008-09-10 12:48 9202人阅读 评论(3) 收藏 举报 thread多线程l ...
- [转]CSS vertical-align属性详解 作者:黄映焜
CSS vertical-align属性详解 posted @ 2014-08-26 17:44 黄映焜 前言:关于vertical-align属性. 实践出真知. 垂直居中. 第二种用法. ...
- WIN7系统JavaEE(java+tomcat7+Eclipse)环境配
在进行 Java Web环境开发之前,首先要做的第一件事就是搭建开发环境,开发环境搭建成功,接下来便是对整个开发环境进行测试,可以通过编写一个简单的JSP 程序发布到Tomcat应用服务器上运行. 1 ...
- C#如何解决对ListView控件更新以及更新时界面闪烁问题
第一个问题:如何更新ListView控件内容 很多时候运行窗体程序时,由于程序中使用了多线程加之操作不当,所以在对控件操作时会出现下面这样的异常: 这是因为我们在窗体中添加的控件都有属于自己的线程 ...