链接:http://acm.hdu.edu.cn/showproblem.php?pid=4263

杭电热身赛的题,属于简单题,两次Kruskal算法,先把所有的红边加完,红边可以保证一部分的图是联通的,然后加蓝边构成生成树,那么这些蓝边就是整个生成树当中必须要有的边(其实不是因为这些边必须要有,因为这些边连的点必须要加到生成树里,所以必须要有一定数量的蓝边,可能这些蓝边的选择方案有很多,但是数量是一定的),然后把图还原,把原来必须的蓝边先加上,再一直加没有用过的蓝边,最后看加的蓝边的数量是不是>=k如果满足则能有方案做到,否则不能。

View Code

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #define N 1005
  5 using namespace std;
  6 int f[N];
  7 int n;
  8 bool used[N*N];
  9 struct edge
 10 {
 11     int u,v;
 12 };
 13 void init()
 14 {
 15     int i;
 16     for(i=1;i<=n;i++)
 17     f[i]=i;
 18     memset(used,0,sizeof(used));
 19 }
 20 int find(int i)
 21 {
 22     if(i!=f[i])
 23     {
 24         f[i]=find(f[i]);
 25     }
 26     return f[i];
 27 }
 28 void uni(int x,int y)
 29 {
 30     int t1=find(x);
 31     int t2=find(y);
 32     if(t1!=t2)
 33     f[t2]=t1;
 34 }
 35 edge R[N*N],B[N*N];
 36 int main()
 37 {
 38     int m,k,i,j,a,b;
 39     char s[2];
 40     int cnt1,cnt2;
 41     while(scanf("%d%d%d",&n,&m,&k)&&(n||m||k))
 42     {
 43         cnt1=cnt2=0;
 44         init();
 45         for(i=1;i<=m;i++)
 46         {
 47             scanf("%s%d%d",s,&a,&b);
 48             if(s[0]=='R')
 49             {
 50                 R[cnt1].u=a;
 51                 R[cnt1++].v=b;
 52             }
 53             else
 54             {
 55                 B[cnt2].u=a;
 56                 B[cnt2++].v=b;
 57             }
 58         }
 59         if(cnt2<k)
 60         {
 61             printf("0\n");
 62             continue;
 63         }
 64         //printf("%d %d\n",cnt1,cnt2);
 65         for(i=0;i<cnt1;i++)
 66         {
 67             uni(R[i].u,R[i].v);
 68         }
 69         int num=0;
 70         int num1=0;
 71         for(i=0;i<cnt2;i++)
 72         {
 73             if(find(B[i].u)!=find(B[i].v))
 74             {
 75                 num++;
 76                 uni(B[i].u,B[i].v);
 77                 used[i]=true;
 78             }
 79         }
 80         if(num>k)//如果必须加的蓝边的数量>k那就意味着无解了
 81         {
 82             printf("0\n");
 83             continue;
 84         }
 85         for(i=1;i<=n;i++)
 86         {
 87             f[i]=i;
 88         }
 89         for(i=0;i<cnt2;i++)
 90         {
 91             if(used[i])
 92             {
 93                 uni(B[i].u,B[i].v);
 94             }
 95         }
 96         for(i=0;i<cnt2;i++)
 97         {
 98             if(!used[i]&&find(B[i].u)!=find(B[i].v))
 99             {
100                 num++;
101                 uni(B[i].u,B[i].v);
102             }
103         }
104         if(num>=k)
105         printf("1\n");
106         else
107         printf("0\n");
108     }
109     return 0;
110 }

转载于:https://www.cnblogs.com/caozhenhai/archive/2012/08/26/2657188.html

UCIPC2012-Red/Blue Spanning Tree解题报告相关推荐

  1. 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)

    [LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  2. POJ 2054 Color a Tree解题报告

    题干 Bob is very interested in the data structure of a tree. A tree is a directed graph in which a spe ...

  3. LeetCode 1104. Path In Zigzag Labelled Binary Tree解题报告

    1104. Path In Zigzag Labelled Binary Tree Path In Zigzag Labelled Binary Tree python solution 题目描述 I ...

  4. 【LeetCode】538. Convert BST to Greater Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

  5. 655. Print Binary Tree 解题报告(树)

    第一部分:搜索.遍历 [例子1]655. Print Binary Tree Example 1: Input:1/2 Output: [["", "1", & ...

  6. 【原创】leetCodeOj ---Convert Sorted List to Binary Search Tree 解题报告

    原题地址: https://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ 题目内容: Given a sing ...

  7. LeetCode: Convert Sorted Array to Binary Search Tree 解题报告

    Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...

  8. LeetCode Convert Sorted List to Binary Search Tree 解题报告

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

  9. Sicily 1156. Binary tree 解题报告

    题目地址:1156. Binary tree 思路: 简单的一道二叉树相关的题目,题目会给出一颗树现在的形态,然后用前序遍历这棵树的节点输出数据即可. 每个节点会输入该节点的identifier,有点 ...

  10. [poj1741]tree 解题报告

    经典的点分做法就不说了(然而我写点分t了..) 线段树/平衡树启发式合并的话,就是维护子树每个节点到子树跟的距离,打一个整棵子树的标记,然后按dfs/bfs序启发式合并,合并之前先查询一下答案即可. ...

最新文章

  1. java字典序列化_Java对象序列化,Serialize Java Data Object,音标,读音,翻译,英文例句,英语词典...
  2. 删除windows上的oracle产品
  3. 英语学习APP的案例分析
  4. plc模拟器软件_关于PLC虚拟化的思考当下及未来
  5. 经典C语言程序100例之九二
  6. [转]XNA 3.1 转换到 XNA4.0 的备忘录
  7. leetcode 448. Find All Numbers Disappeared in an Array | 448. 找到所有数组中消失的数字(原地,位运算)
  8. 为什么安装了Microsoft .NET Framework 4之后我的电脑网卡启动会变得很慢很慢。。...
  9. C# 8.0 的默认接口方法
  10. DBUtils 主要结果集说明
  11. web前端是什么?如何能成为一名合格的前端开发工程师?
  12. MySQL导入MongoDB
  13. 使用NodeJS调用Dubbo工程
  14. 文本分类的python实现-基于Xgboost算法
  15. 科学计算与可视化python_Python科学计算和可视化
  16. (图文详细)设置文件夹中的文件显示文件后缀名的方法
  17. 隐藏在Windows XP中的28个秘密武器
  18. 1931CIE基础知识
  19. 小微企业内部用服务器应该怎么选择配置
  20. fiddler手机抓包配置代理后没有网络(手机不能上网)

热门文章

  1. innodb_force_recovery
  2. TCP加速机制是如何加速的?
  3. 计蒜客NOIP2017提高组模拟赛(三)day2-小区划分
  4. Ubuntu 16.04 安装VNC服务端
  5. Java Web前后端分离的思考与实践
  6. 使cmd窗口不自动关闭
  7. ArcMAp10.2生成栅格数据时报错
  8. SQLServer - 约束
  9. 2013年值得我们学习的网页设计作品【系列二】
  10. Software - ASCII码表