Fibonacci Tree
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5448 Accepted Submission(s): 1699
Problem Description
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, ... )
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, ... )
Input
The first line of the input contains an integer T, 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).
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).
Output
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.
Sample Input
2 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
Sample Output
Case #1: Yes Case #2: No
注意点:1.重复定义同一个数据竟然会让数据初始化:比如在自己写的函数中用了下面的m,n需要在程序的开头就定义否则自己写的函数里定义一次,又在main函数中定义一次就会出错
2:路径压缩(这题不用会TLE)
2.1:朴素的findx写法是
int finx(int x)
{
int t=x;
while(r!=bin[r])
{
r=bin[r];
}
return r;
}
2.2 路径压缩写法是:
int findx(int x)
return x==bin[x]?x:bin[x]=find(bin[x]);
路径压缩后所有的子节点都连向一个父节点
状态压缩后省了1300ms
#include <stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
int n,m;
int flag;
int z[100017];
void fb()
{
z[1]=1,z[2]=2;
for(int i=3;;i++)
{
z[i]=z[i-1]+z[i-2];
if(z[i]>100017)
break;
}
}
int judge(int max,int min)
{
int flag=0;
for(int i=1;z[i]<=max;i++)
{
if(z[i]>=min&&z[i]<=max)
{
flag=1;
break;
}
}
return flag;
}
int bin[100017];
struct node{
int u,v,c;
}a[100017];
int findx(int x)
{
return x==bin[x]?x:bin[x]=findx(bin[x]);
}
int findx(int x)
{
return bin[x]==x?x:bin[x]=findx(bin[x]);
}
int merge(int target)
{
int k=0,i;
for(i=1;i<=n;i++)
{
bin[i]=i;
}
for(i=0;i<m;i++)
{
if(a[i].c!=target)
{
int fx=findx(a[i].u);
int fy=findx(a[i].v);
if(fx!=fy)
{
bin[fx]=fy;
k++;
}
}
}
return k;
}
int main(int argc, char *argv[])
{
int q,t;
fb();
scanf("%d",&t);
for(q=1;q<=t;q++)
{
int i;
scanf("%d %d",&n,&m);
for(i=0;i<m;i++)
{
scanf("%d %d %d",&a[i].u,&a[i].v,&a[i].c);
}
int test=merge(2);
if(test!=n-1)
printf("Case #%d: No\n",q);
else
{
int max=merge(0);
int min=n-merge(1)-1;
int flag2=judge(max,min);
if(flag2)
printf("Case #%d: Yes\n",q);
else
printf("Case #%d: No\n",q);
}
}
return 0;
}
探讨如何在双向图中寻找包含特定数量白色边的生成树,这些特定数量为斐波那契数列中的数值。文章通过算法实现,解决了如何判断这样的生成树是否存在。
195

被折叠的 条评论
为什么被折叠?



