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. codeforces 570 D. Tree Requests (dfs)

    题目链接: 570 D. Tree Requests 题目描述: 给出一棵树,有n个节点,1号节点为根节点深度为1.每个节点都有一个字母代替,问以结点x为根的子树中高度为h的后代是否能够经过从新排序变 ...

  2. linux 前台后台程序切换命令总结

    1.在Linux终端运行命令的时候,在命令末尾加上 & 符号,就可以让程序在后台运行 root@Ubuntu$ ./tcpserv01 & 2.如果程序正在前台运行,可以使用 Ctrl ...

  3. DFS和BFS模板

    DFS: 该DFS框架以2D坐标范围为例,来体现DFS算法的实现思想 #include<cstdio> #include<cstring> #include<cstdli ...

  4. Android偏好设置(3)启动偏好设置后显示的界面PreferenceActivity和PreferenceFragment

    Creating a Preference Activity To display your settings in an activity, extend the PreferenceActivit ...

  5. java中实参与形参的概念

    形参: public void fun(形参类型 形参名){ ... } 实参: public static void main(String[] args){ 类 对象名=new 类(); 对象名. ...

  6. 学习笔记 第九章 使用CSS美化表格

    第9章  使用CSS美化表格 学习重点 正确使用表格标签: 设置表格和单元格属性: 设计表格的CSS样式. 9.1 表格的基本结构 表格由行.列.单元格3部分组成,单元格时行与列交叉的部分. 在HTM ...

  7. vue2.0 动态切换组件

    组件标签是Vue框架自定义的标签,它的用途就是可以动态绑定我们的组件,根据数据的不同更换不同的组件. <!DOCTYPE html> <html lang="en" ...

  8. vba,设置,excel,wps ,页面设置

    全面认识页面设置之 PageSetup 对象我们在写 VBA 代码时,特别是做小型程序开发时,经常会用 VBA 来设置“页面设置”中的选项,还可用要用 VBA 来实现一些特殊的效果,这就需要使用 Pa ...

  9. 前端入门22-讲讲模块化 包括webstrom建立简单ES6

    https://www.cnblogs.com/dasusu/p/10105433.html

  10. 1434:【例题2】Best Cow Fences

    1434:[例题2]Best Cow Fences 时间限制: 1000 ms         内存限制: 65536 KB提交数: 263     通过数: 146 [题目描述] 给定一个长度为n的 ...