n个点最少要n-1条边才能连通,可以删除一条边,最多删除2条边,然后枚举删除的1条边或2条边,用并查集判断是否连通,时间复杂度为O(n^3)

这边犯了个错误,

for(int i=0;i<N;i++){
fa[i]=i;
}

这个将i<=N,导致错误,值得注意

AC代码:

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include<iostream>
  3. #include<cstdio>
  4. #include<cstring>
  5. #include<cmath>
  6. #include<math.h>
  7. #include<algorithm>
  8. #include<queue>
  9. #include<set>
  10. #include<bitset>
  11. #include<map>
  12. #include<vector>
  13. #include<stdlib.h>
  14. #include <stack>
  15. using namespace std;
  16. #define PI acos(-1.0)
  17. #define max(a,b) (a) > (b) ? (a) : (b)
  18. #define min(a,b) (a) < (b) ? (a) : (b)
  19. #define ll long long
  20. #define eps 1e-10
  21. #define MOD 1000000007
  22. #define N 106
  23. #define inf 1e12
  24. int n,fa[N],a[N],b[N];
  25. void init(){
  26. for(int i=;i<N;i++){
  27. fa[i]=i;
  28. }
  29. }
  30. int find(int x){
  31. return fa[x]==x?x:fa[x]=find(fa[x]);
  32. }
  33. int main()
  34. {
  35. int t;
  36. scanf("%d",&t);
  37. while(t--){
  38. scanf("%d",&n);
  39. for(int i=;i<=n;i++){
  40. scanf("%d%d",&a[i],&b[i]);
  41. }
  42. int ans=;
  43. for(int i=;i<=n;i++){//删除一条边
  44. init();
  45. int cnt=n;
  46. for(int j=;j<=n;j++){
  47. if(j!=i){
  48. int root1=find(a[j]);
  49. int root2=find(b[j]);
  50. if(root1!=root2){
  51. fa[root1]=root2;
  52. cnt--;
  53. }
  54. }
  55. }
  56. if(cnt==) ans++;
  57. }
  58.  
  59. for(int i=;i<=n;i++){//删除两条边
  60. for(int j=i+;j<=n;j++){
  61. init();
  62. int cnt=n;
  63. for(int k=;k<=n;k++){
  64. if(k!=i && k!=j){
  65. int root1=find(a[k]);
  66. int root2=find(b[k]);
  67. if(root1!=root2){
  68. fa[root1]=root2;
  69. cnt--;
  70. }
  71. }
  72. }
  73. if(cnt==)ans++;
  74. }
  75. }
  76. printf("%d\n",ans);
  77.  
  78. }
  79. return ;
  80. }

贴上别人写bfs代码:

  1. #include<cstdio>
  2. #include<cstring>
  3. int n,m;
  4. struct note{
  5. int to,next;
  6. }a[];
  7. int head[];
  8. bool used[];
  9. bool des[];
  10. int que[];
  11. bool bfs(){
  12. memset(used,false,sizeof(used));
  13. int start=,aim=;
  14. que[aim++]=;
  15. used[]=true;
  16. while(start<aim){
  17. int num=que[start++];
  18. for(int i=head[num];i!=-;i=a[i].next){
  19. if(!des[i])continue;
  20. if(used[a[i].to])continue;
  21. used[a[i].to]=true;
  22. que[aim++]=a[i].to;
  23. }
  24. }
  25. bool judge=true;
  26. for(int i=;i<=n;i++){
  27. if(!used[i])judge=false;
  28. }
  29. return judge;
  30. }
  31. int main(){
  32. int T;
  33. // freopen("5631.txt","r",stdin);
  34. scanf("%d",&T);
  35. while(T--){
  36. scanf("%d",&n);
  37. int coun=;
  38. memset(head,-,sizeof(head));
  39. m=n;
  40. for(int i=;i<=m;i++){
  41. int x,y;
  42. scanf("%d%d",&x,&y);
  43. a[coun].to=y;
  44. a[coun].next=head[x];
  45. head[x]=coun++;
  46. a[coun].to=x;
  47. a[coun].next=head[y];
  48. head[y]=coun++;
  49. }
  50. int ans=;
  51. memset(des,true,sizeof(des));
  52. for(int i=;i<=m;i++){
  53. for(int j=i+;j<=m;j++){
  54. des[i<<]=des[i*+]=des[j*]=des[j*+]=false;
  55. if(bfs())ans++;
  56. des[i<<]=des[i*+]=des[j*]=des[j*+]=true;
  57. }
  58. }
  59. for(int i=;i<=m;i++){
  60. des[i<<]=des[i*+]=false;
  61. if(bfs())ans++;
  62. des[i<<]=des[i*+]=true;
  63. }
  64. printf("%d\n",ans);
  65. }
  66. return ;
  67. }

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. C++私有构造函数

    一. 类的构造函数一般是public的,但是也可以是private的.构造函数为私有的类有这样的特点: <1>不能实例化:因为实例化时类外部无法访问其内部的私有的构造函数: <2&g ...

  2. hdu 5642 King's Order(数位dp)

    Problem Description After the king's speech , everyone is encouraged. But the war is not over. The k ...

  3. BackgroundWorker用法

    BackgroundWorker主要用来提供后台运算服务(防止用户前台无响应等待),并提供服务进度的类: 代码如下: BackgroundWorker bgw = new BackgroundWork ...

  4. web前端之 HTML介绍

    概述 HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,他是一种制作万维网页面标准语言(标记).相当于定义统一的一套规则,大家都来遵守他,这样就可以让浏览器 ...

  5. Sort(归并)

    Sort 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 You want to processe a sequence of n distinct integers ...

  6. All X(思维)

    All X Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Subm ...

  7. 解决getJdbcTemplate往oracle数据库中插入数据返回主键出错问题

    我们使用Spring中的JdbcDaoSupport往Mysql中插入数据并返回主键代码,我们使用的mysql数据库,主键在数据库中设置为自增长:该类继承自JdbcDaoSupport,所以能直接使用 ...

  8. [置顶] Java中发邮件的6种方法

    1.官方标准JavaMail Sun(Oracle)官方标准,功能强大,用起来比较繁琐. 官方资料:http://www.oracle.com/technetwork/java/javamail/in ...

  9. [HeadFirst-HTMLCSS学习笔记][第十二章HTML5标记]

    考虑HTML结构 HTML5即是把原来<div>换成一些更特定的元素.能够更明确指示包含什么内容. (页眉,导航,页脚,文章) article nav 导航 header footer t ...

  10. ListView加载两种以上不同的布局

    不同的项目布局(item layout) Listview一种单一的item 布局有时候不能完全满足业务需求,我们需要加载两种或两种以上不同的布局,实现方法很简单: 重写 getViewTypeCou ...