HDU 4263(Red/Blue Spanning Tree-取边贪心)

Red/Blue Spanning Tree

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


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
 

Source
The University of Chicago Invitational Programming Contest 2012
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:   4257  4258  4260  4261  4262 

设红边为2,蓝边为1,求MST得最多蓝边数

同理得最少蓝边数,观察k是否在区间中


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (3*2000+10)
#define MAXM (10000000+10)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
class bingchaji
{
public:
    int father[MAXN],n;
    void mem(int _n)
    {
        n=_n;
        For(i,n) father[i]=i;
    }
    int getfather(int x)
    {
        if (father[x]==x) return x;

        return father[x]=getfather(father[x]);
    }
    void unite(int x,int y)
    {
        father[x]=getfather(father[x]);
        father[y]=getfather(father[y]);
        father[father[x]]=father[father[y]];
    }
    bool same(int x,int y)
    {
        return getfather(x)==getfather(y);
    }
}S;

int n,m,k;
struct edge
{
    int a,b,c;
    edge(){}
    edge(int _x,int _y,int _c):a(_x),b(_y),c(_c){}
    friend bool operator<(edge a,edge b){return a.c<b.c;}
}e[MAXM];

int kr()
{
    S.mem(n);
    sort(e+1,e+1+m);
    int ans1=0,t=0;
    For(i,m) if (!S.same(e[i].a,e[i].b))
    {
        S.unite(e[i].a,e[i].b),ans1+=e[i].c,++t;
    //    cout<<e[i].a<<' '<<e[i].b<<' '<<e[i].c<<endl;
    }
    if (t<n-1) return -1;

    return ans1;
}
int main()
{
    //    freopen("g.in","r",stdin);
    //    freopen(".out","w",stdout);

    while(cin>>n>>m>>k)
    {
        if (n+m+k==0) return 0;
        S.mem(n);

        For(i,m)
        {
            char c;int a,b,t;
            scanf("\n%c %d %d",&c,&a,&b);
            if (c=='B') t=1;else t=2;
            e[i]=edge(a,b,t);
        }


        int t1=kr();

        For(i,m) e[i].c=3-e[i].c;

        int t2=kr();



//        cout<<t1<<' '<<t2<<endl;

        if (t1!=-1)
        {

            t1=t1-(n-1);
            t1=n-1-t1;
            t2=t2-(n-1);
  //          cout<<t1<<' '<<t2<<endl;

            if (t1>=k&&t2<=k)
                {
                    cout<<"1\n";
                    continue;
                }
        }


        cout<<"0\n";






    }

    return 0;
}



你可能感兴趣的:(HDU 4263(Red/Blue Spanning Tree-取边贪心))