Red/Blue Spanning Tree

Time Limit: 2000ms
Memory Limit: 131072KB

This problem will be judged on HDU. Original ID: 4263
64-bit integer IO format: %I64d      Java class name: Main

 
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, andk (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

 
解题:Kruskal最小生成树模型!先把红边放在前面,做一次Kruskal,再把蓝边放在前面,做一次Kruskal,记录两次蓝边的选用情况分布为x,y!if x <= k <= y那么输出1,否则输出0.
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct arc{
int u,v;
char color;
arc(int x = ,int y = ,char ch = '#'):u(x),v(y),color(ch){}
};
bool cmp1(const arc &x,const arc &y){
return x.color < y.color;
}
bool cmp2(const arc &x,const arc &y){
return x.color > y.color;
}
arc e[];
int n,m,k,uf[maxn];
int Find(int x){
if(x != uf[x]){
uf[x] = Find(uf[x]);
}
return uf[x];
}
void kruskal(int &cnt,char ch){
if(ch == 'B') sort(e,e+m,cmp1);
else sort(e,e+m,cmp2);
int i,j,tx,ty;
for(i = ; i <= n; i++) uf[i] = i;
for(cnt = i = ; i < m; i++){
tx = Find(e[i].u);
ty = Find(e[i].v);
if(tx != ty){
uf[tx] = ty;
if(e[i].color == 'B') cnt++;
}
}
}
int main() {
int i,j,u,v,x,y;
char s[];
while(scanf("%d %d %d",&n,&m,&k),n||m||k){
for(i = ; i < m; i++){
scanf("%s %d %d",s,&u,&v);
e[i] = arc(u,v,s[]);
}
kruskal(x,'R');
kruskal(y,'B');
if(k >= x && k <= y) puts("");
else puts("");
}
return ;
}

比较另类但是比较好写的写法,摘自。。。

 #include<stdio.h>
#include<string.h>
#include<iostream>
#include<map>
#include<queue>
#include<algorithm>
using namespace std; const int maxN = ;
bool vis[maxN]; struct node{
int x, y;
}p[maxN * ]; int find(int u,int *f) {
if(f[u] == u)
return u;
return f[u] = find(f[u], f);
}
bool fun(int u,int v,int *f) {
int px = find(u, f), py = find(v, f);
if(px != py) {
f[px] = py;
return true;
}
return false;
} int main() {
int N, M, K;
while(scanf("%d%d%d", &N, &M, &K) && (N + M + K)) {
int f1[maxN], f2[maxN], f3[maxN], num = ;
for(int i = ;i <= N;++ i)
f1[i] = f2[i] = f3[i] = i;
for(int i = ;i < M; ++ i) {
char s[];
int u, v;
scanf("%s%d%d", s, &u, &v);
if(s[] == 'R')
fun(u, v, f2);
else {
p[num].x = u;
p[num ++].y = v;
}
}
memset(vis, , sizeof(vis));
int sum = , ans = ;
for(int i = ;i <= N; ++ i) {
int px = find(i, f2);
if(!vis[px]) {
sum ++;
vis[px] = true;
}
}
for(int i = ;i < num; ++ i)
if(fun(p[i].x, p[i].y, f2)) {
sum --;
K --;
fun(p[i].x, p[i].y, f3);
p[i].x = p[i].y = -;
}
int flag = ;
if(sum == && K >= ) {
for(int i = ;i < num && K > ; ++ i)
if(p[i].x > && fun(p[i].x, p[i].y, f3)) {
K --;
fun(p[i].x, p[i].y, f2);
}
}
if(sum == && K == )flag = ;
printf("%d\n", flag);
}
}

BNUOJ 26229 Red/Blue Spanning Tree的更多相关文章

  1. CF1208H Red Blue Tree

    CF1208H Red Blue Tree 原本应该放在这里但是这题过于毒瘤..单独开了篇blog 首先考虑如果 $ k $ 无限小,那么显然整个树都是蓝色的.随着 $ k $ 逐渐增大,每个点都会有 ...

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

    Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Krusk ...

  3. 数据结构与算法分析–Minimum Spanning Tree(最小生成树)

    给定一个无向图,如果他的某个子图中,任意两个顶点都能互相连通并且是一棵树,那么这棵树就叫做生成树(spanning tree). 如果边上有权值,那么使得边权和最小的生成树叫做最小生成树(MST,Mi ...

  4. Educational Codeforces Round 3 E. Minimum spanning tree for each edge LCA/(树链剖分+数据结构) + MST

    E. Minimum spanning tree for each edge   Connected undirected weighted graph without self-loops and ...

  5. Codeforces Edu3 E. Minimum spanning tree for each edge

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  6. CF# Educational Codeforces Round 3 E. Minimum spanning tree for each edge

    E. Minimum spanning tree for each edge time limit per test 2 seconds memory limit per test 256 megab ...

  7. Codeforces Educational Codeforces Round 3 E. Minimum spanning tree for each edge LCA链上最大值

    E. Minimum spanning tree for each edge 题目连接: http://www.codeforces.com/contest/609/problem/E Descrip ...

  8. MST(Kruskal’s Minimum Spanning Tree Algorithm)

    You may refer to the main idea of MST in graph theory. http://en.wikipedia.org/wiki/Minimum_spanning ...

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

    Minimum Spanning Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

随机推荐

  1. [SDOI2016]墙上的句子

    题目描述 考古学家发现了一堵写有未知语言的白色墙壁,上面有一个n行m列的格子,其中有些格子内被填入了某个A至Z的大写字母,还有些格子是空白的. 一直横着或竖着的连续若干个字母会形成一个单词,且每一行的 ...

  2. [Usaco2017 Feb]Why Did the Cow Cross the Road II (Platinum)

    Description Farmer John is continuing to ponder the issue of cows crossing the road through his farm ...

  3. _bzoj1087 [SCOI2005]互不侵犯King【dp】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1087 令f(i, j, k)表示前i列,二进制状态为j,已经用了k个国王的方案数,则 f(i ...

  4. 洛谷 P3203 [HNOI2010]弹飞绵羊 || bzoj2002

    看来这个lct板子的确没什么问题 好像还可以分块做 #include<cstdio> #include<algorithm> using namespace std; type ...

  5. Triangular Pastures POJ - 1948

    Triangular Pastures POJ - 1948 sum表示木条的总长.a[i]表示第i根木条长度.ans[i][j][k]表示用前i条木条,摆成两条长度分别为j和k的边是否可能. 那么a ...

  6. swing中的线程

    1. 初始化线程 初始化线程用于创建各种容器,组件并显示他们,一旦创建并显示,初始化线程的任务就结束了. 2. 事件调度线程(单线程:只有一个线程在负责事件的响应工作.) 通过事件监听的学习,我们了解 ...

  7. Hanlder + 弱引用防内存漏泄示例*

    Hanlder + 弱引用防内存漏泄示例: public class MainActivity extends AppCompatActivity { public final MyHandler h ...

  8. 200 Number of Islands 岛屿的个数

    给定 '1'(陆地)和 '0'(水)的二维网格图,计算岛屿的数量.一个岛被水包围,并且通过水平或垂直连接相邻的陆地而形成.你可以假设网格的四个边均被水包围.示例 1:11110110101100000 ...

  9. 转】R利剑NoSQL系列文章 之 Cassandra

    原博文出自于: http://blog.fens.me/category/%E6%95%B0%E6%8D%AE%E5%BA%93/page/3/ 感谢! R利剑NoSQL系列文章 之 Cassandr ...

  10. poj3233Matrix Power Series

    链接 也是矩阵经典题目  二分递归求解 a+a^2+a^3+..+a^(k/2)+a^(k/2+1)+...+a^k = a+a^2+..+a^k/2+a^k/2(a^1+a^2+..+a^k/2)( ...