HDU 4786Fibonacci Tree(最小生成树)
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5934 Accepted Submission(s):
1845
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.
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
对于最小生成树来说,任意删除一条边,并加入一条没有出现过的边,这样的话权值至多加1,边界为最大生成树
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN=1e6+,INF=1e9+;
inline char nc()
{
static char buf[MAXN],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,,MAXN,stdin),p1==p2)?EOF:*p1++;
}
inline int read()
{
char c=nc();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=nc();}
while(c>=''&&c<=''){x=x*+c-'';c=nc();}
return x*f;
}
struct node
{
int u,v,w;
}edge[MAXN];
int num=;
inline void AddEdge(int x,int y,int z)
{
edge[num].u=x;
edge[num].v=y;
edge[num].w=z;num++;
}
int N,M;
int fib[MAXN];
int fa[MAXN];
int comp1(const node &a,const node &b){return a.w<b.w;}
int comp2(const node &a,const node &b){return a.w>b.w;}
int find(int x)
{
if(fa[x]==x) return fa[x];
else return fa[x]=find(fa[x]);
}
void unionn(int x,int y)
{
int fx=find(x);
int fy=find(y);
fa[fx]=fy;
}
int Kruskal(int opt)
{
if(opt==) sort(edge+,edge+num,comp1);
else sort(edge+,edge+num,comp2);
int ans=,tot=;
for(int i=;i<=num-;i++)
{
int x=edge[i].u,y=edge[i].v,z=edge[i].w;
if(find(x) == find(y)) continue;
unionn(x,y);
tot++;
ans=ans+z;
if(tot==N-) return ans;
}
}
int main()
{
#ifdef WIN32
freopen("a.in","r",stdin);
#else
#endif
int Test=read();
fib[]=;fib[]=;
for(int i=;i<=;i++) fib[i]=fib[i-]+fib[i-];
int cnt=;
while(Test--)
{
N=read(),M=read();num=;
for(int i=;i<=N;i++) fa[i]=i;
for(int i=;i<=M;i++)
{
int x=read(),y=read(),z=read();
AddEdge(x,y,z);
AddEdge(y,x,z);
}
int minn=Kruskal();
for(int i=;i<=N;i++) fa[i]=i;
int maxx=Kruskal();
bool flag=;
for(int i=;i<=;i++)
if(minn <= fib[i] && fib[i] <= maxx)
{printf("Case #%d: Yes\n",++cnt);flag=;break;}
if(flag==) printf("Case #%d: No\n",++cnt);
}
return ;
}
HDU 4786Fibonacci Tree(最小生成树)的更多相关文章
- hdu 5909 Tree Cutting [树形DP fwt]
hdu 5909 Tree Cutting 题意:一颗无根树,每个点有权值,连通子树的权值为异或和,求异或和为[0,m)的方案数 \(f[i][j]\)表示子树i中经过i的连通子树异或和为j的方案数 ...
- hdu Constructing Roads (最小生成树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1102 /************************************************* ...
- HDU 5044 Tree(树链剖分)
HDU 5044 Tree field=problem&key=2014+ACM%2FICPC+Asia+Regional+Shanghai+Online&source=1&s ...
- [HDU 5293]Tree chain problem(树形dp+树链剖分)
[HDU 5293]Tree chain problem(树形dp+树链剖分) 题面 在一棵树中,给出若干条链和链的权值,求选取不相交的链使得权值和最大. 分析 考虑树形dp,dp[x]表示以x为子树 ...
- HDU 4408 Minimum Spanning Tree 最小生成树计数
Minimum Spanning Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- HDU 2489 Minimal Ratio Tree 最小生成树+DFS
Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HDU 4786 Fibonacci Tree 最小生成树
Fibonacci Tree 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4786 Description Coach Pang is intere ...
- HDU 4757 Tree(可持久化Trie+Tarjan离线LCA)
Tree Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others) Total Su ...
- 数据结构与算法分析–Minimum Spanning Tree(最小生成树)
给定一个无向图,如果他的某个子图中,任意两个顶点都能互相连通并且是一棵树,那么这棵树就叫做生成树(spanning tree). 如果边上有权值,那么使得边权和最小的生成树叫做最小生成树(MST,Mi ...
随机推荐
- UI_UIImagePickerController(读取图片)
创建图片 #pragma mark - 创建 photoImageView - (void)createphotoImageView { self.photoImageView = [[UIImage ...
- ioctl.h 分析
ioctl.h 分析 我自己画了个解析图...不要嫌弃丑啊.. . 哈哈 type The magic number. Just choose one number (after consulting ...
- Android 启动界面的制作
直接看实例吧 package com.example.textview; import android.app.Activity; import android.content.Intent; imp ...
- Process Monitor
https://en.wikipedia.org/wiki/Process_Monitor Process Monitor is a free tool from Windows Sysinterna ...
- Linux就该这么学 20181005(第九章SSH远程对话)
参考链接https://www.linuxprobe.com/ nmtui开启网卡设置 ONBOOT=yes systemctl restart network nmcli connection sh ...
- [luogu P3813] [FJOI2017] 矩阵填数 解题报告 (容斥原理)
题目链接: https://www.luogu.org/problemnew/show/P3813 题目: 给定一个 h*w的矩阵,矩阵的行编号从上到下依次为 1..h,列编号从左到右依次1..w. ...
- 最长公共子序列(稀疏序列)nlogn解法
首先这种做法只能针对稀疏序列, 比如这种情况: abc abacabc. 会输出5 ,,,,就比较尴尬, #include<iostream> #include<cstdio> ...
- 2019Pycharm激活方法
1.将“0.0.0.0 account.jetbrains.com”添加到hosts文件中 2.打开http://idea.lanyus.com/ 3.获取激活码,粘贴到第二个选项中 亲测可用.
- 如何去掉边框及input的兼容问题?
右偷个懒,发现别人写的也不错,我就做个小搬运工 如何去掉边框及input的兼容问题? 说到input,又不得不说它的兼容问题.input如何兼容各个浏览器呢? 第一步:清除input的border的默 ...
- (转载)Android自定义ProgressDialog进度等待框
Android自定义ProgressDialog进度等待框 作者:无缘公子 字体:[增加 减小] 类型:转载 时间:2016-01-11我要评论 这篇文章主要介绍了Android自定义Progress ...