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. CF916E

    Codeforces 916E 简要题解Description Description 有一棵n个点的树,每个节点上有一个权值wi,最开始根为1号点.现在有3种类型的操作: 1 root, 表示将根设 ...

  2. DP Codeforces Round #260 (Div. 1) A. Boredom

    题目传送门 /* 题意:选择a[k]然后a[k]-1和a[k]+1的全部删除,得到点数a[k],问最大点数 DP:状态转移方程:dp[i] = max (dp[i-1], dp[i-2] + (ll) ...

  3. Arrays.asList()后调用add,remove这些method时出现java.lang.UnsupportedOperationException异常

    String[] queryNames = request.getParameterValues("queryName"); List<String> queryNam ...

  4. SSM学习

    一.https://www.cnblogs.com/zyw-205520/p/4771253.html 二.https://blog.csdn.net/dwhdome/article/details/ ...

  5. 在阿里云上搭建nginx + ThinkPHP 的实践

    作为一个程序猿,理应用linux系统来作为平时的工作机环境,哎,之前倒是用过一段时间的linux,可惜后来换了本本,后来竟然没有保持,嗷嗷后悔中... 废话不多说,大家用windows的理由都一样,但 ...

  6. vscode前端开发软件配搭好用的插件

    使用方法,可以在官网中搜索需要的插件或者在VsCode的“”扩展“”中搜索需要的插件添加方法使用Ctrl+P, 输入 ext install xxxx ,搜索要安装的插件,点击安装按钮即可(各取所需插 ...

  7. leetcode_378. Kth Smallest Element in a Sorted Matrix_堆的应用

    Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...

  8. CREATE GROUP - 定义一个新的用户组

    SYNOPSIS CREATE GROUP name [ [ WITH ] option [ ... ] ] where option can be: SYSID gid | USER usernam ...

  9. shell高级用法

    参考链接: http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=218853&page=7#pid1628522

  10. make、makefile

    http://blog.csdn.net/wed110/article/details/34853475 什么是makefile?或许很多Winodws的程序员都不知道这个东西,因为那些Windows ...