hdu 4786 Fibonacci Tree ( 最小產生樹 ),hdu4786

來源:互聯網
上載者:User

hdu 4786 Fibonacci Tree ( 最小產生樹 ),hdu4786
Fibonacci TreeTime Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2487    Accepted Submission(s): 796


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, ... ) 
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). 
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

24 41 2 12 3 13 4 11 4 05 61 2 11 3 11 4 11 5 13 5 14 2 1
 
Sample Output
Case #1: YesCase #2: No
 



題意 : 給你一張無向圖,其中有的邊為白色,有的邊為黑色,問你是否有一顆生
成樹並且它的白色邊是斐波那契數列中的一個數。
思路 :因為邊為白色的權值為1,黑色的權值為0。即在原圖的基礎上分別求一
遍最小產生樹和一遍最大產生樹。假設求出的最小產生樹的總權值為 Min ,最大
產生樹的總權值為 Max , 那麼我們可知產生樹中最小的白邊數為 Min ,最大白
邊數為 Max。然後我們只要判斷這兩個值之間是否存在斐波那契數就行了。
上述結論其實是要證明對於所有在Min和Max之間的白邊數都可以。
原因 :在最大產生樹上去掉Max - Min 條白邊,可知最大產生樹會變成
Max - Min+1個連通塊,並且這些連通塊中已經有 Min條白邊 ,所以可知這
 Max - Min+1個連通塊可以通過 Max - Min 條黑邊串連起來 。 將這 Max - Min+1
個連通塊看做成 Max - Min+1個點,即這些點可以全通過白邊或黑邊串連起來。
那麼可以得出這些點也可以通過n條白邊和m條黑邊串連起來 (m+n = Max - Min)。

即可行的白邊數為 ans = Min+n , 因為 0 <= n <=Max-Min ,所以 Min <= ans <=Max 。 


#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>using namespace std;const int maxn=100010;const int N=30;struct edge{    int u,v,val;} a[maxn];int id[maxn],f[N]= {0,1,2},n,m,T;bool cmp1(edge p,edge q){    return p.val<q.val;}bool cmp2(edge p,edge q){    return p.val>q.val;}int Find(int x){    if(x!=id[x]) id[x]=Find(id[x]);    return id[x];}void input(){    scanf("%d %d",&n,&m);    for(int i=0; i<m; i++) scanf("%d %d %d",&a[i].u,&a[i].v,&a[i].val);}int kru(){    for(int i=0; i<maxn; i++)  id[i]=i;    int res=0,num=0;    for(int i=0; i<m; i++)    {        int p=Find(a[i].u),q=Find(a[i].v);        if(p!=q)        {            id[p]=q;            res+=a[i].val;        }    }    for(int i=1; i<=n; i++)  if(Find(i)==i)  num++;    if(num>1)  return -1;    return res;}void solve(int co){    int l,r;    sort(a,a+m,cmp1);    l=kru();    sort(a,a+m,cmp2);    r=kru();    if(l==-1 || r==-1)    {        printf("Case #%d: No\n",co);        return ;    }    else    {        for(int i=1; i<N; i++)        {            if(f[i]>=l && f[i]<=r)            {                printf("Case #%d: Yes\n",co);                return ;            }        }        printf("Case #%d: No\n",co);    }}int main(){    for(int i=3; i<N; i++)  f[i]=f[i-1]+f[i-2];    scanf("%d",&T);    for(int co=1; co<=T; co++)    {        input();        solve(co);    }    return 0;}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.