hdu 4263 (最小生成树)

Red/Blue Spanning Tree

Time Limit: 10000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 813    Accepted Submission(s): 302


Problem Description
Given an undirected, unweighted, connected graph, where each edge is colored either blue or red, determine whether a spanning tree with exactly  k blue edges exists.
 

Input
There will be several test cases in the input. Each test case will begin with a line with three integers:
n m k
Where  n (2≤ n≤1,000) is the number of nodes in the graph,  m (limited by the structure of the graph) is the number of edges in the graph, and  k (0≤ k< n) is the number of blue edges desired in the spanning tree.
Each of the next  m lines will contain three elements, describing the edges:
c f t
Where  c is a character, either capital ‘ R’ or capital ‘ B’, indicating the color of the edge, and  f and  t are integers (1≤ f, tntf) indicating the nodes that edge goes from and to. The graph is guaranteed to be connected, and there is guaranteed to be at most one edge between any pair of nodes.
The input will end with a line with three 0s.
 

Output
For each test case, output single line, containing 1 if it is possible to build a spanning tree with exactly  k blue edges, and 0 if it is not possible. Output no extra spaces, and do not separate answers with blank lines.
 

Sample Input
   
   
   
   
3 3 2 B 1 2 B 2 3 R 3 1 2 1 1 R 1 2 0 0 0
 

Sample Output
   
   
   
   
1 0
 

Source
The University of Chicago Invitational Programming Contest 2012
 

Recommend
liuyiding
 
给一个无向图,每条边有红边或蓝边两种性质,现在问能否恰好用k条蓝边构成一个生成树。这道题的关键点在于是个判断问题,而不是构成问题,如果是构成问题就难了。但是如果只需判断的话,可以根据这条性质:设图的生成树中蓝边数量最多的生成树的蓝边数为a,最少为b,则只要(a <= k && k <= b) ,则恰好用k就是可以满足的。(这个性质我也不会证明)。那么我把所有红边的权设为0,蓝边的权设为1,则a和b就分别对应着原图的最小和最大生成树。这样问题就迎刃而解了。
#include<cstdio>
#include<iostream>
#include<map>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
const int maxn = 1000 + 5;
const int INF = 100000 + 5;

struct Edge{
    int from,to,dis;
}e[maxn*maxn];

int fa[maxn];
int Find(int x){return x==fa[x]?x:fa[x]=Find(fa[x]);}

bool cmp(Edge a,Edge b){
    return a.dis < b.dis;
}

int main(){
    int n,m,k;
    while(scanf("%d%d%d",&n,&m,&k) != EOF){
        if(n == 0 && m == 0 && k == 0) break;
        for(int i = 0;i < m;i++){
            char s[5];
            scanf("%s%d%d",s,&e[i].from,&e[i].to);
            if(s[0] == 'R') e[i].dis = 0;
            else e[i].dis = 1;
        }
        sort(e,e+m,cmp);
        for(int i = 0;i <= n;i++) fa[i] = i;
        int sum1 = 0;
        for(int i = 0;i < m;i++){
            int X = Find(e[i].from);
            int Y = Find(e[i].to);
            if(X != Y){
                fa[X] = Y;
                sum1 += e[i].dis;
            }
        }
        int sum2 = 0;
        for(int i = 0;i <= n;i++) fa[i] = i;
        for(int i = m-1;i >= 0;i--){
            int X = Find(e[i].from);
            int Y = Find(e[i].to);
            if(X != Y){
                fa[X] = Y;
                sum2 += e[i].dis;
            }
        }
        if(k >= sum1 && k <= sum2) printf("1\n");
        else printf("0\n");
    }
    return 0;
}


你可能感兴趣的:(hdu 4263 (最小生成树))