BNUOJ 26229 Red/Blue Spanning Tree
Red/Blue Spanning Tree
This problem will be judged on HDU. Original ID: 4263
64-bit integer IO format: %I64d Java class name: Main
Input
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,t≤n, t≠f) 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
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
#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的更多相关文章
- CF1208H Red Blue Tree
CF1208H Red Blue Tree 原本应该放在这里但是这题过于毒瘤..单独开了篇blog 首先考虑如果 $ k $ 无限小,那么显然整个树都是蓝色的.随着 $ k $ 逐渐增大,每个点都会有 ...
- 【HDU 4408】Minimum Spanning Tree(最小生成树计数)
Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Krusk ...
- 数据结构与算法分析–Minimum Spanning Tree(最小生成树)
给定一个无向图,如果他的某个子图中,任意两个顶点都能互相连通并且是一棵树,那么这棵树就叫做生成树(spanning tree). 如果边上有权值,那么使得边权和最小的生成树叫做最小生成树(MST,Mi ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- HDU 4408 Minimum Spanning Tree 最小生成树计数
Minimum Spanning Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
随机推荐
- C语言经典程序1
//打印2-200的所有素数 (除了1和它本身,不能被小于它的其它数整除的数称为素数) ; ;i<;i++) //i遍历2-200 { ; //先默认这个数为素数 ;j<i-;j++) / ...
- Windows查杀端口
Windows环境下当某个端口被占用时,通过netstat命令进行查询pid,然后通过taskkill命令杀进程. 一.查询占用端口号的进程信息 netstat -an|findstr 二.杀掉占用端 ...
- Latex排版工具的使用(二) 分类: Latex 2014-06-14 23:01 389人阅读 评论(0) 收藏
Latex可以支持中文排版,如何实现中文支持可以到网上查找教程. 下面编写一段对中文排版的Latex源文档: 新建文件second.tex: \documentclass{article} \usep ...
- Android SQLite(1)简单示例-增,删,改,查
1.主要核心类,Sqlite编程要继承SQLiteOpenHelper import android.content.Context; import android.database.sqlite.S ...
- ORA-28002错误原因及解决办法
在oracle database 11g中,默认在default概要文件中设置了“PASSWORD_LIFE_TIME=180天”所导致.密码过期后,业务进程连接数据库异常,影响业务使用.数据库密码过 ...
- CentOS系统里如何正确取消或者延长屏幕保护自动锁屏功能(图文详解)
不多说,直接上干货! 对于我这里想说的是,分别从CentOS6.X 和 CentOS7.X来谈及. 1. 问题:默认启动屏幕保护 问题描述: CentOS系统在用户闲置一段时间(默认为5分钟)后, ...
- rest_framework基于generics.CreateAPIView创建用户
最近在写新版的devops3.0,被generics.CreateAPIView创建用户密码序列化的问题折磨的欲仙欲死.反复看源码测试,得出下面的流程,这也是做generics.CreateAPIVi ...
- RegisterClientScriptBlock和RegisterStartupScript的区别
RegisterClientScriptBlock在 Page 对象的 元素的开始标记后立即发出客户端脚本,RegisterStartupScript则是在Page 对象的 元素的结束标记之前发出该脚 ...
- ES6扩展运算符的使用
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Windos无法验证文件数组签名
参考链接:https://jingyan.baidu.com/article/09ea3ede6982c4c0aede39e6.html Windows无法验证文件数字签名而无法启动,照以下去做,可以 ...