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条边,最后一条边的 ...
随机推荐
- Install Cocos2d-x v3.3 on Ubuntu 14.04 & Ubuntu 14.10(转)
Install Cocos2d-x v3.3 on Ubuntu 14.04 & Ubuntu 14.10 1 get the source code sudo apt-get install ...
- UIView 转 UIImage
这个方法很实用,特别是在做水印相机得时候... - (UIImage*) imageWithUIView:(UIView*) view{ // 创建一个bitmap的context // 并把它设置成 ...
- Cookie知识点小结
问题是什么?有哪些技术?如何解决? 1. Cookie 1)完成回话跟踪的一种机制:采用的是在客户端保存Http状态信息的方案 2)Cookie是在浏览器访问WEB服务器的某个资源时,由WEB服务器在 ...
- Struts2+Spring+Hibernate 三大框架的合并集成
这次来看看Struts2+Spring+Hibernate三大框架的整合应用,主要是Spring和Hibernate框架的整合,因为前边已经将Strtus2+Spring整合过了基本一样. 首先看一 ...
- c++之 printf 打印内容
该代码全部在Visual Studio 2015中编写,有关VS2015的安装流程后期在写相关的博文 首先让我们来输出一下hello, world! 1.首先新建一个main.cpp的文件,然后在该文 ...
- web文档类型DOCTYPE html很重要
之前写html或者jsp页面,从来不注意DOCTYPE 的声明,也不太明白DOCTYPE 的作用.直到最近碰到了一个非常奇葩的bug:某一个页面在IE7和8,Chrome,ff等下正常,但是在IE9下 ...
- 在Maven的配置文件中,自定义私有仓库地址和设置下载的jar包的保存位置
在Maven的settings.xml,可以设置Maven的私有仓库的地址,还可以设置所下载jar包在自己电脑的保存地址(默认不设置保存在个人文件夹的.m2文件夹下). 1.设置私有仓库地址: < ...
- 微软提供了三个核心服务:Windows+Office 365+Azure
微软提供了三个核心服务:Windows+Office 365+Azure 英语新闻来源:http://techcrunch.com/2014/11/10/microsofts-ceo-breaks-d ...
- 用gitolite新建项目,clone后首次push,可能会出现: git: No refs in common and none specified; doing no
用gitolite新建项目,clone后首次push,可能会出现: $ git push No refs in common and none specified; doing nothing ...
- Unity 2D 跑酷道路动起来
之前做2D的游戏怎样让背景动起来?就想着做成滚屏效果不就行了,今天在网上看到人家做的既简单又方便,唉,忏愧啊!不过还好,下次可以为自己所用了!呵呵 废话就不扯了,新建工程! 1 ,打开Unity 5. ...