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

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-04 17:50:53

HDU 4263(Red/Blue Spanning Tree-取边贪心)的相关文章

BNUOJ 26229 Red/Blue Spanning Tree

Red/Blue Spanning Tree Time Limit: 2000ms Memory Limit: 131072KB This problem will be judged on HDU. Original ID: 426364-bit integer IO format: %I64d      Java class name: Main Given an undirected, unweighted, connected graph, where each edge is colo

HDU 4912 Paths on the tree LCA 排序贪心

lca... 排个序然后暴力保平安 _(:зゝ∠)_ #pragma comment(linker, "/STACK:102400000,102400000") #include"cstdio" #include"iostream" #include"set" #include"queue" #include"string.h" using namespace std; #define

【HDU 4408】Minimum Spanning Tree(最小生成树计数)

Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Kruskal algorithm of minimum spanning tree, XXX finds that there might be multiple solutions. Given an undirected weighted graph with n (1<=n<=100) vertex

HDU 4912 Paths on the tree(LCA+贪心)

题目链接 Paths on the tree 来源  2014 多校联合训练第5场 Problem B 题意就是给出m条树上的路径,让你求出可以同时选择的互不相交的路径最大数目. 我们先求出每一条路径(u, v)中u和v的LCA:w,按照路径的w的深度大小deep[w]对所有的路径排序. deep[w]越大,排在越前面. 然后从第一条路径开始一次处理,看c[u]和c[v]是否都没被标记过,如果都没被标记过则我们把这条路径选上,把答案加1. 同时标记以w为根的子树的节点为1,方便后续对c数组的查询

(最小生成树) hdu 4263

Red/Blue Spanning Tree Time Limit: 10000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 973    Accepted Submission(s): 365 Problem Description Given an undirected, unweighted, connected graph, where each edge

HDU 4896 Minimal Spanning Tree(矩阵快速幂)

题意: 给你一幅这样子生成的图,求最小生成树的边权和. 思路:对于i >= 6的点连回去的5条边,打表知907^53 mod 2333333 = 1,所以x的循环节长度为54,所以9个点为一个循环,接下来的9个点连回去的边都是一样的.预处理出5个点的所有连通状态,总共只有52种,然后对于新增加一个点和前面点的连边状态可以处理出所有状态的转移.然后转移矩阵可以处理出来了,快速幂一下就可以了,对于普通的矩阵乘法是sigma( a(i, k) * b(k, j) ) (1<=k<=N), 现在

hdu 4896 Minimal Spanning Tree

Minimal Spanning Tree Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 144    Accepted Submission(s): 44 Problem Description Given a connected, undirected, weight graph G, your task is to select

CF1208H Red Blue Tree

CF1208H Red Blue Tree 原本应该放在这里但是这题过于毒瘤..单独开了篇blog 首先考虑如果 $ k $ 无限小,那么显然整个树都是蓝色的.随着 $ k $ 逐渐增大,每个点都会有且仅有一次变色,我们考虑维护这个变色的时间 $ t $ .如果每个点的变色时间都已经被算出来,那么我们可以轻易解决题目的查询操作和修改 $ k $ , 也就是说修改 $ k $ 本身就是个假操作..只需要考虑的是修改单点颜色. 修改单点颜色,看起来就很 $ ddp $ .树链剖分后,用$ f(x)

HDU 4408 Minimum Spanning Tree 最小生成树计数

Minimum Spanning Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) [Problem Description] XXX is very interested in algorithm. After learning the Prim algorithm and Kruskal algorithm of minimum spanning tree, XXX