HDU 4263 Red/Blue Spanning Tree【最小生成树原理】

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, tn, tf) 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
 

题目大意: 给出m条边,是否有满足K条蓝色形成的最小生成数;
思路:根据最小生成树原理;你先把红边放在前面,贪心,求出一个用了k1条蓝边的生成树;再把蓝边放在前面,贪心,求出一个用了k2条蓝边的生成树
    如果满足k1<=k<=k2,则输出1;否则输出0;

代码入下:

View Code
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int f[10500]; 
struct point
{
    int ch, st, end;
}p[1100005]; 
int find(int x)
{
    return x==f[x]?x:f[x]=find(f[x]);
}
int cmp1(point x, point y)
{
    return x.ch<y.ch; 
} 
int cmp2(point x, point y)
{
    return x.ch>y.ch;
} 
int main()
{
    int m, n, k, i, j, ans1, ans2;
    while(scanf("%d%d%d", &n, &m, &k)!=EOF)
    {
        if(n==0&&m==0&&k==0)
            break;
    //    point p[1100005];  定义在main()内脑袋短路了,,,RE 了N次 
        for(i=0; i<m; i++)
        {
            getchar(); 
            scanf("%c%d%d", &p[i].ch, &p[i].st, &p[i].end);
        } 
        sort(p, p+m, cmp1); //蓝的放在前面
        for(i=0; i<=n; i++)    
            f[i]=i;
        ans1=0; 
        for(i=0; i<m; i++)
        {
            int xx=find(p[i].st), yy=find(p[i].end);
            if(xx!=yy)
            {
                f[xx]=yy; 
                if(p[i].ch=='B')
                    ans1++;
            }
        }
        sort(p, p+m, cmp2); //红的放在前面
        for(i=0; i<=n; i++)    
            f[i]=i;
        ans2=0; 
        for(i=0; i<n; i++)
        {
            int xx=find(p[i].st), yy=find(p[i].end);
            if(xx!=yy)
            {
                f[xx]=yy; 
                if(p[i].ch=='B')
                    ans2++;
            }
        }
        if(k>=ans2&&k<=ans1)
            printf("1\n");
        else
            printf("0\n"); 
    } 
}     

 

 

转载于:https://www.cnblogs.com/Hilda/archive/2012/08/25/2657033.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值