hdu 4786(生成树)
Fibonacci Tree
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4028 Accepted Submission(s): 1252
Coach Pang is interested in Fibonacci numbers while Uncle Yang wants
him to do some research on Spanning Tree. So Coach Pang decides to solve
the following problem:
Consider a bidirectional graph G with N
vertices and M edges. All edges are painted into either white or black.
Can we find a Spanning Tree with some positive Fibonacci number of white
edges?
(Fibonacci number is defined as 1, 2, 3, 5, 8, ... )
For each test case, the first line contains two integers N(1 <= N <= 105) and M(0 <= M <= 105).
Then M lines follow, each contains three integers u, v (1 <= u,v
<= N, u<> v) and c (0 <= c <= 1), indicating an edge
between u and v with a color c (1 for white and 0 for black).
For each test case, output a line “Case #x: s”. x is the case number
and s is either “Yes” or “No” (without quotes) representing the answer
to the problem.
4 4
1 2 1
2 3 1
3 4 1
1 4 0
5 6
1 2 1
1 3 1
1 4 1
1 5 1
3 5 1
4 2 1
Case #2: No
- #include <stdio.h>
- #include <algorithm>
- #include <string.h>
- #include <math.h>
- #include <queue>
- using namespace std;
- const int N = ;
- int father[N];
- struct Edge{
- int u,v,color;
- }edge[N];
- int _find(int x){
- if(x!=father[x]) {
- father[x] = _find(father[x]);
- }
- return father[x];
- }
- int n,m;
- int cmp(Edge a,Edge b){
- return a.color>b.color;
- }
- int cmp1(Edge a,Edge b){
- return a.color<b.color;
- }
- int kruskal(){
- int cost=;
- for(int i=;i<m;i++){
- int x=_find(edge[i].u);
- int y=_find(edge[i].v);
- if(x!=y){
- father[x] = y;
- cost+=edge[i].color;
- }
- }
- return cost;
- }
- bool vis[N];
- void init(){
- memset(vis,false,sizeof(vis));
- int a=,b=;
- vis[]=true,vis[] =true;
- while(a+b<N){
- vis[a+b]=true;
- swap(a,b);
- b = a+b;
- }
- }
- int main()
- {
- int tcase;
- scanf("%d",&tcase);
- int t = ;
- init();
- while(tcase--){
- scanf("%d%d",&n,&m);
- for(int i=;i<m;i++){
- scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].color);
- }
- for(int i=;i<=n;i++) father[i] = i;
- sort(edge,edge+m,cmp);
- int maxn = kruskal();
- int ans = ;
- for(int i=;i<=n;i++){
- if(father[i]==i) ans++;
- if(ans>) break;
- }
- if(ans>) {
- printf("Case #%d: No\n",t++);
- continue;
- }
- for(int i=;i<=n;i++) father[i] = i;
- sort(edge,edge+m,cmp1);
- int minn = kruskal();
- ans = ;
- for(int i=;i<=n;i++){
- if(father[i]==i) ans++;
- if(ans>) break;
- }
- if(ans>) {
- printf("Case #%d: No\n",t++);
- continue;
- }
- //printf("%d %d\n",minn,maxn);
- bool flag = false;
- for(int i=minn;i<=maxn;i++){
- if(vis[i]){
- flag = true;
- break;
- }
- }
- if(flag) printf("Case #%d: Yes\n",t++);
- else printf("Case #%d: No\n",t++);
- }
- return ;
- }
hdu 4786(生成树)的更多相关文章
- HDU 4786 生成树 并查集+极大极小值 黑白边 确定选择白边的数量
题意: 给定一个无向图 n 个点 m条无向边 u v val val == 1 表示边(u, v) 为白边 问能否找到n个点的生成树, 使得白边数为斐波那契数 思路: 并查集求图是否连通( 是否存在生 ...
- HDU 4786 Fibonacci Tree(生成树,YY乱搞)
http://acm.hdu.edu.cn/showproblem.php? pid=4786 Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others ...
- HDU 4786 Fibonacci Tree 生成树
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4786 题意:有N个节点(1 <= N <= 10^5),M条边(0 <= M <= ...
- hdu 4786 最小生成树与最大生成树
/* 题意 :有一些边权值为1和0,判断是否存在一个生成树使得他的总权值为一个斐波那契数. 解法:建立一个最小生成树向里面加权值为1的边替换为0的边,保证原来的联通.因为权值为1,可直接求出最大生成树 ...
- hdu 4786 Fibonacci Tree (最小、最大生成树)
题意: N个点,M条边.每条边连接两个点u,v,且有一个权值c,c非零即一. 问能否将N个点形成一个生成树,并且这棵树的边权值和是一个fibonacii数. (fibonacii数=1,2,3,5,8 ...
- hdu 4786 Fibonacci Tree (2013ACMICPC 成都站 F)
http://acm.hdu.edu.cn/showproblem.php?pid=4786 Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others) ...
- HDU 4786 Fibonacci Tree 最小生成树
Fibonacci Tree 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4786 Description Coach Pang is intere ...
- HDU 4786(最小生成树 kruskal)
题目链接:pid=4786" target="_blank">http://acm.hdu.edu.cn/showproblem.php?pid=4786 Prob ...
- 【HDU 4786 Fibonacci Tree】最小生成树
一个由n个顶点m条边(可能有重边)构成的无向图(可能不连通),每条边的权值不是0就是1. 给出n.m和每条边的权值,问是否存在生成树,其边权值和为fibonacci数集合{1,2,3,5,8...}中 ...
随机推荐
- 第39-43课 thinkphp5完成商品会员价格功能(后置勾子afterInsert)
目录 功能一:利用后置勾子,处理好商品主键id,会员的价格,再插入member_price表里. 要实现的功能: 思路: html里 控制器里 模型里的后置勾子afterInsert() 功能二:利用 ...
- Codeforces Round #459 (Div. 2):B. Radio Station
B. Radio Station time limit per test2 seconds memory limit per test256 megabytes Problem Dsecription ...
- 8、python中的集合
集合是python中无序.可变的数据结构.集合与字典类似,集合中的元素必须是可哈希的(等同于字典中的键),也就是说集合中的元素是唯一.不可变的数据类型.这里前面说集合可变,后面又说集合中的元素不可变是 ...
- A JavaScript Image Gallery
childNodes property: The childNodes property is a way of getting information about the children of ...
- SpringMvc路径参数和url的两种实现方式
我们经常采用的SpringMvc路径参数经常的操作是在url后面采用?参数名=值1&参数名2=值2这种方式实现 RequestMapping的作用: 1)当作用在controller时,我们通 ...
- Install ADDS on Windows Server 2012 R2 with PowerShell
Install ADDS on Windows Server 2012 R2 with PowerShell Posted by ethernuno on 20/04/2014 In this tut ...
- error C2011: “Picture”:“struct”类型重定义
今天引用外来库时出现问题,也许是版本问题. 错误如下: .....\oursun\cincludes\quickdraw.h(309): error C2011: “Picture”:“struct” ...
- oracle常用关键字和函数
数据库的增删改查: 增:insert into ... values(); 例:insert into p_emp values(sq_emp.nextval,,sysdate,,null,,); c ...
- Leetcode 611.有效三角形的个数
有效三角形的个数 给定一个包含非负整数的数组,你的任务是统计其中可以组成三角形三条边的三元组个数. 示例 1: 输入: [2,2,3,4] 输出: 3 解释: 有效的组合是: 2,3,4 (使用第一个 ...
- 理解点击屏幕的事件响应--->对UIView的hitTest: withEvent: 方法的理解
要理解这两个方法.先了解一下用户触摸屏幕后的事件传递过程. 当用户点击屏幕后,UIApplication 先响应事件,然后传递给UIWindow.如果window可以响应.就开始遍历window的su ...