杭电4263——Red/Blue Spanning Tree

该博客介绍了一种解决图论问题的方法,即如何确定在一个无权、连通的染色图中是否存在恰好含有k条蓝色边的生成树。通过两次构建最小生成树,首先找到包含最少蓝边的生成树,然后找到包含最多蓝边的生成树,比较蓝边数量是否在k的范围内,从而判断目标生成树是否存在。算法思路清晰,适用于解决特定的图论挑战。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Red/Blue Spanning Tree

链接:http://acm.hdu.edu.cn/showproblem.php?pid=4263

Time Limit: 10000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1283 Accepted Submission(s): 514

Problem Description

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, and k (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

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

思路:两次最小生成树

正确代码:

#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
int s[2000];
int n,m,k;
struct node{
	char str[5];//一定要是个字符数组不然过不了 
	int x;
	int y;
	int f;
} sj[1000005];
void init(){
	for(int i=0;i<=n;i++){
		s[i]=i;
	}
}
int find(int x){
	if(s[x]!=x){
		s[x]=find(s[x]);
	}
	return s[x];
}
void merge(int x,int y){
	int x1=find(x);
	int y1=find(y);
	if(x1!=y1){
		s[x1]=y1;
	}
}
bool cmp1(node a,node b){
	return a.f<b.f;
}
bool cmp2(node a,node b){
	return a.f>b.f;
}
int main(){
	/*ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);*/
	//一定要用scanf和printf,不要用cin,cout会超时 
	while(scanf("%d%d%d",&n,&m,&k)!=EOF){ 
		if(!n&&!m&&!k){
			break;
		}
		init();
		int c1=0;
		int c2=0;
		for(int i=0;i<m;i++){
			scanf("%s %d%d",&sj[i].str,&sj[i].x,&sj[i].y);
			//cin>>sj[i].str>>sj[i].x>>sj[i].y;
			if(sj[i].str[0]=='B'){
				sj[i].f=1;
			}
			else{
				sj[i].f=0;
			}
		}
		sort(sj,sj+m,cmp1);//蓝边最少的时候 
		for(int i=0;i<m;i++){
			if(find(sj[i].x)!=find(sj[i].y)){
				merge(sj[i].x,sj[i].y);
				if(sj[i].f==1){
					c1++;
				}
			}
		}
		sort(sj,sj+m,cmp2);//蓝边最多的时候 
		init();
		for(int i=0;i<m;i++){
			if(find(sj[i].x)!=find(sj[i].y)){
				merge(sj[i].x,sj[i].y);
				if(sj[i].f==1){
					c2++;
				}
			}
		}
		if(c1<=k&&k<=c2){ 
			printf("1\n");
		}
		else{
			printf("0\n");
		}	
	}
	return 0;
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值