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 ...
随机推荐
- 数据结构之splay树
https://www.bilibili.com/video/av19879546 https://blog.csdn.net/u014634338/article/details/42465089 ...
- 2017 JUST Programming Contest 3.0 D. Dice Game
D. Dice Game time limit per test 1.0 s memory limit per test 256 MB input standard input output stan ...
- Arrays.asList()后调用add,remove这些method时出现java.lang.UnsupportedOperationException异常
String[] queryNames = request.getParameterValues("queryName"); List<String> queryNam ...
- Service官方教程(1)Started与Bound的区别、要实现的函数、声明service
Services 简介和分类 A Service is an application component that can perform long-running operations in the ...
- 支付宝-API接口解析-转账到银行
支付宝-API接口解析-转账到银行 扫码转账 测试地址 解析内容: alipays://platformapi/startapp?appId=09999988&actionType=toCar ...
- SOA测试之浏览器插件
1. Chrome HTTP Rest Client 插件: 1.1 Postman: https://chrome.google.com/webstore/detail/postman-rest-c ...
- 重构26-Remove Double Negative(去掉双重否定)
尽管我在很多代码中发现了这种严重降低可读性并往往传达错误意图的坏味道,但这种重构本身还是很容易实现的.这种毁灭性的代码所基于的假设导致了错误的代码编写习惯,并最终导致bug.如下例所示: public ...
- PHP到浏览器的缓存机制
参考地址:http://www.cnblogs.com/godok/p/6341300.html 所有的php程序员都知道在php脚本里面执行 echo “1”;访客的浏览器里面就会显示“1”. 但是 ...
- Linux修改ssh端口,减少暴力破解
版本centos7 注意:操作时请勿断开当前的ssh连接,以免发生情况登陆不了. 1.修改的是 /etc/ssh/sshd_config 文件 打开文件之后会发现Port是注释掉的,默认为22 ...
- Can't find bundle for base name messages.AndroidJpsBundle, locale zh_CN
从http://www.android-studio.org/网站上下载了一个Android Studio 3.0的非安装版本(android-studio-ide-171.4408382-wind ...