(最小生成树) 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 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

题意:

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

太水 就不写了额。。。。

时间: 2024-10-26 23:20:23

(最小生成树) hdu 4263的相关文章

最小生成树 || HDU 1301 Jungle Roads

裸的最小生成树 输入很蓝瘦 **并查集 int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } 找到x在并查集里的根结点,如果两个端点在同一个集合内,find之后两个值就相等了 每次找到权值最小的端点不在同一集合的边 把两个集合合并 #include <iostream> #include <cstdio> #include <algorithm> using namespace std; int

(最小生成树) hdu 4424

Conquer a New Region Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1382    Accepted Submission(s): 455 Problem Description The wheel of the history rolling forward, our king conquered a new re

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 edg

最小生成树 hdu 1233 模板题

#include <bits/stdc++.h> using namespace std; const int maxn=1e4+7; int n,m,cot,sum; struct edge { int u,v,w; bool operator <(const edge &b)const{ return w<b.w; } }g[maxn]; int f[maxn]; int Find(int x) { return f[x]==x?x:f[x]=Find(f[x]); }

HDU 4081 Qin Shi Huang&#39;s National Road System 最小生成树

分析:http://www.cnblogs.com/wally/archive/2013/02/04/2892194.html 这个题就是多一个限制,就是求包含每条边的最小生成树,这个求出原始最小生成树然后查询就好了 然后预处理那个数组是O(n^2)的,这样总时间复杂度是O(n^2+m) 这是因为这个题n比较小,如果n大的时候,就需要路径查询了,比如LCA 或者树链剖分达到O(mlogn) #include <iostream> #include <algorithm> #incl

HDU 5624 KK&#39;s Reconstruction(最小生成树)

题目链接:点击打开链接 题意:n个城市, m条可以修建的路, 修每条路有一个费用, 要求修建路将n个城市全部联通,并且最大费用减去最小费用最小. 思路:枚举最小边, 然后重新求一遍最小生成树,复杂度m^2, 出的数据水了, 左边BC水过了.. 细节参见代码: #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<string> #inc

Hdu 3371 Connect the Cities(最小生成树)

地址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 其实就是最小生成树,但是这其中有值得注意的地方:就是重边.题目没有告诉你两个城市之间只有一条路可走,所以两个城市之间可能有多条路可以走. 举例: 输入可以包含 1 2 3  // 1到2的成本为3   1 2 5  //1到2的成本为5      因此此时应选成本较低的路. 然后,已经连通的城市之间的连通成本为0. 这题用G++提交得到984ms的反馈,用C++提交则得到484ms的反馈. 很想知

Hdu 1301 Jungle Roads (最小生成树)

地址:http://acm.hdu.edu.cn/showproblem.php?pid=1301 很明显,这是一道“赤裸裸”的最小生成树的问题: 我这里采用了Kruskal算法,当然用Prim算法也一样可以解题. #include <iostream> #include <cstring> #include <cstdio> #include <cstdlib> using namespace std; typedef struct node{ int f

hdu oj1102 Constructing Roads(最小生成树)

Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13995    Accepted Submission(s): 5324 Problem Description There are N villages, which are numbered from 1 to N, and you should