Fibonacci Tree 最小生成树

Fibonacci Tree

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 434    Accepted Submission(s): 146


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
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
 

 

Source
 

 

Recommend
We have carefully selected several similar problems for you:  4789 4788 4787 4785 4784 
const int INF = 1000000000;
const double eps = 1e-8;
const int maxn = 300000;
vector<int> g[maxn];
struct Edge
{
    int a;
    int b;
    int c;
    Edge(int _a,int _b,int _c):a(_a),b(_b),c(_c){}
};
void init()
{
    repf(i,1,100010)
        g[i].clear();
}
vector<Edge> edges;
int p[maxn];
int find(int x)
{
    if(p[x] == x)
        return x;
    else
    {
        p[x] = find(p[x]);
        return p[x];
    }
}

int kruskal()
{
    int ans = 0;
    rep(i,0,edges.size())
    {
        int x = find(edges[i].a);
        int y = find(edges[i].b);
        if(x != y)
        {
            p[x] = y;
            ans += edges[i].c;
        }
    }
    return ans;
}

bool cmp1(Edge e1,Edge e2)
{
    return e1.c > e2.c;
}
bool cmp2(Edge e1,Edge e2)
{
    return e1.c < e2.c;
}

int vis[maxn];

int vis1[maxn];
void dfs(int x)
{
    if(vis1[x]) return ;
    vis1[x] = 1;
    rep(i,0,g[x].size())
        dfs(g[x][i]);
}
int main() 
{
    //freopen("in.txt","r",stdin);
    int T;
    scanf("%d",&T);
    clr(vis);
    vis[1] = 1;
    int a = 1;
    int b = 1;
    while(a + b <= 100000)
    {
        vis[a + b] = 1;
        int t = a;
        a = b;
        b = t + b;
    }
    int k = 1;
    while(T--)
    {
        edges.clear();
        init();
        clr(vis1);
        int n,m;
        scanf("%d%d",&n,&m);
        repf(i,1,n) p[i] = i;
        repf(i,1,m)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            g[a].push_back(b);
            g[b].push_back(a);
            edges.push_back(Edge(a,b,c));
        }
        dfs(1);
        int tag = 1;
        repf(i,1,n) if(vis1[i] == 0) 
        {
            tag = 0;break;
        }
        if(tag == 0)
        {
            printf("Case #%d: ",k++);
            cout<<"No"<<endl;
            continue;
        }
        sort(edges.begin(),edges.end(),cmp1);
        
        int  Max = kruskal();
        repf(i,1,n) p[i] = i;
        sort(edges.begin(),edges.end(),cmp2);
        
        int Min = kruskal();
        
        int flag = 0;
        repf(i,Min,Max)
            if(vis[i])
            {
                flag =1;
                break;
            }
        printf("Case #%d: ",k++);
        if(flag)
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;
    }
    return 0;
}

 

posted on 2013-11-27 16:27  keep trying  阅读(249)  评论(0编辑  收藏  举报

导航