hdoj 4786 Fibonacci Tree【并查集+最小生成树(kruskal算法)】
Fibonacci Tree
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2915 Accepted Submission(s):
931
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, ... )
the number of test cases.
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).
the case number and s is either “Yes” or “No” (without quotes) representing the
answer to the problem.
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define MAX 100010
using namespace std;
struct recode
{
int beg;
int end;
int bian;
}s[MAX];
bool cmp1(recode a,recode b)
{
return a.bian>b.bian;
}
bool cmp2(recode a,recode b)
{
return a.bian<b.bian;
}
int set[MAX];
int fib[MAX];
void biao()
{
int i,j;
fib[1]=1;
fib[2]=2;
for(i=3;fib[i]<MAX;i++)
{
fib[i]=fib[i-1]+fib[i-2];
}
}
int find(int fa)
{
int t;
int ch=fa;
while(fa!=set[fa])
fa=set[fa];
while(ch!=fa)
{
t=set[ch];
set[ch]=fa;
ch=t;
}
return fa;
}
void mix(int x,int y)
{
int fx,fy;
fx=find(x);
fy=find(y);
if(fx!=fy)
set[fx]=fy;
}
int main()
{
int n,m,j,i,t;
scanf("%d",&t);
int k=1;
biao();
while(t--)
{
scanf("%d%d",&n,&m);
int sum=0;
for(i=0;i<m;i++)
scanf("%d%d%d",&s[i].beg,&s[i].end,&s[i].bian);
int min=0,max=0;
for(i=0;i<=n;i++)
set[i]=i;
sort(s,s+m,cmp1);
for(i=0;i<m;i++)
{
//printf("%d %d # ",s[i].beg,s[i].end);
if(find(s[i].beg)!=find(s[i].end))
{
mix(s[i].beg,s[i].end);
if(s[i].bian==1)
max++;
}
}
// printf("\n");
// printf("%d \n",max);
for(i=0;i<=n;i++)
set[i]=i;
sort(s,s+m,cmp2);
for(i=0;i<m;i++)
{
// printf("%d %d # ",s[i].beg,s[i].end);
if(find(s[i].beg)!=find(s[i].end))
{
mix(s[i].beg,s[i].end);
if(s[i].bian==1)
min++;
}
}
// printf("\n");
// printf("%d \n",min);
printf("Case #%d: ",k++);
int wrong=0;
int mis=0;
for(i=1;i<=n;i++)
{
if(set[i]==i)
wrong++;
if(wrong>1)
{
mis=1;
break;
}
}
if(mis)
{
printf("No\n");
continue;
}
int ok=0;
for(i=1;fib[i]<=m;i++)
{
if(fib[i]>=min&&fib[i]<=max)
{
printf("Yes\n");
ok=1;
break;
}
}
if(!ok)
printf("No\n");
}
return 0;
}
hdoj 4786 Fibonacci Tree【并查集+最小生成树(kruskal算法)】的更多相关文章
- 最小生成数(并查集)Kruskal算法
并查集:使用并查集可以把每个连通分量看作一个集合,该集合包含连通分量的所有点.这两两连通而具体的连通方式无关紧要,就好比集合中的元素没有先后顺序之分,只有属于和不属于的区别.#define N 100 ...
- 并查集 & 最小生成树详细讲解
并查集 & 最小生成树 并查集 Disjoint Sets 什么是并查集? 并查集,在一些有N个元素的集合应用问题中,我们通常是在开始时让每个元素构成一个单元素的集合,然后按一定顺序将 ...
- 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(生成树,YY乱搞)
http://acm.hdu.edu.cn/showproblem.php? pid=4786 Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others ...
- 【转】最小生成树——Kruskal算法
[转]最小生成树--Kruskal算法 标签(空格分隔): 算法 本文是转载,原文在最小生成树-Prim算法和Kruskal算法,因为复试的时候只用到Kruskal算法即可,故这里不再涉及Prim算法 ...
- HDU 4786 Fibonacci Tree 最小生成树
Fibonacci Tree 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4786 Description Coach Pang is intere ...
- hdu 4786 Fibonacci Tree(最小生成树)
Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- CodeForces892E 可撤销并查集/最小生成树
http://codeforces.com/problemset/problem/892/E 题意:给出一个 n 个点 m 条边的无向图,每条边有边权,共 Q 次询问,每次给出 ki 条边,问这些边 ...
- CodeForces - 891C: Envy(可撤销的并查集&最小生成树)
For a connected undirected weighted graph G, MST (minimum spanning tree) is a subgraph of G that con ...
随机推荐
- JavaScript 删除数组重复元素
unique :function (array){ var n = {}, r = [], len = array.length, val, type; for (var i = 0; i < ...
- Node之express
Express 是一个简洁.灵活的 node.js Web 应用开发框架, 它提供一系列强大的特性,帮助你创建各种 Web 和移动设备应用. 如何安装: npm install -g express ...
- android入门到熟练(三)----UI界面
1.TextView 以下只是一部分属性,还有很多属性需要在用到时候再说 <TextView android:textSize="24sp"//文字大小 android:te ...
- LICAppInfo
[Common] Enabled=true All=true [AppList] #AppName #Enabled #Validit ...
- Rendering Transparent 3D Surfaces in WPF with C#(转载)
Rendering Transparent 3D Surfaces in WPF with C# The primary problems that arise when rendering semi ...
- swift从0加到1000(不包括1000)的五种写法
用了while, do...while, for in, for in ... { temp += i i++ } println(temp) do { temp2 += j j++ } ) prin ...
- mybaits不能出现小于号
org.xml.sax.SAXParseException; lineNumber: 146; columnNumber: 54; The content of elements must consi ...
- Android Audio 分析
一.架构 二.MediaServer初始化 所有的media服务都在进程mediaserver里.其代码在framework/base/media/mediaserver/main_mediaserv ...
- 使用Unity游戏引擎在IOS模拟器中运行的方法
在Unity编译IOS程序时,在Unity导航栏菜单中选择Edit->ProjectSettings ->Player(菜单项)选择IOS平台在下方SDK Version处选择运行设备为I ...
- 【HDU3065】 病毒侵袭持续中(AC自动机)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...