BNUOJ 26229 Red/Blue Spanning Tree

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

The University of Chicago Invitational Programming Contest 2012

解题:Kruskal最小生成树模型!先把红边放在前面,做一次Kruskal,再把蓝边放在前面,做一次Kruskal,记录两次蓝边的选用情况分布为x,y!if x <= k <= y那么输出1,否则输出0.

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define pii pair<int,int>
15 #define INF 0x3f3f3f3f
16 using namespace std;
17 const int maxn = 1010;
18 struct arc{
19     int u,v;
20     char color;
21     arc(int x = 0,int y = 0,char ch = ‘#‘):u(x),v(y),color(ch){}
22 };
23 bool cmp1(const arc &x,const arc &y){
24     return x.color < y.color;
25 }
26 bool cmp2(const arc &x,const arc &y){
27     return x.color > y.color;
28 }
29 arc e[1100010];
30 int n,m,k,uf[maxn];
31 int Find(int x){
32     if(x != uf[x]){
33         uf[x] = Find(uf[x]);
34     }
35     return uf[x];
36 }
37 void kruskal(int &cnt,char ch){
38     if(ch == ‘B‘) sort(e,e+m,cmp1);
39     else sort(e,e+m,cmp2);
40     int i,j,tx,ty;
41     for(i = 0; i <= n; i++) uf[i] = i;
42     for(cnt = i = 0; i < m; i++){
43         tx = Find(e[i].u);
44         ty = Find(e[i].v);
45         if(tx != ty){
46             uf[tx] = ty;
47             if(e[i].color == ‘B‘) cnt++;
48         }
49     }
50 }
51 int main() {
52     int i,j,u,v,x,y;
53     char s[5];
54     while(scanf("%d %d %d",&n,&m,&k),n||m||k){
55         for(i = 0; i < m; i++){
56             scanf("%s %d %d",s,&u,&v);
57             e[i] = arc(u,v,s[0]);
58         }
59         kruskal(x,‘R‘);
60         kruskal(y,‘B‘);
61         if(k >= x && k <= y) puts("1");
62         else puts("0");
63     }
64     return 0;
65 }

比较另类但是比较好写的写法,摘自。。。

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<iostream>
 4 #include<map>
 5 #include<queue>
 6 #include<algorithm>
 7 using namespace std;
 8
 9 const int maxN = 2100;
10 bool vis[maxN];
11
12 struct node{
13     int x, y;
14 }p[maxN * 100];
15
16 int find(int u,int *f) {
17     if(f[u] == u)
18         return u;
19     return f[u] = find(f[u], f);
20 }
21 bool fun(int u,int v,int *f) {
22     int px = find(u, f), py = find(v, f);
23     if(px != py) {
24         f[px] = py;
25         return true;
26     }
27     return false;
28 }
29
30 int main() {
31     int N, M, K;
32     while(scanf("%d%d%d", &N, &M, &K) && (N + M + K)) {
33         int f1[maxN], f2[maxN], f3[maxN], num = 0;
34         for(int i = 0;i <= N;++ i)
35             f1[i] = f2[i] = f3[i] = i;
36         for(int i = 0;i < M; ++ i) {
37             char s[10];
38             int u, v;
39             scanf("%s%d%d", s, &u, &v);
40             if(s[0] == ‘R‘)
41                 fun(u, v, f2);
42             else {
43                 p[num].x = u;
44                 p[num ++].y = v;
45             }
46         }
47         memset(vis, 0, sizeof(vis));
48         int sum = 0, ans = 0;
49         for(int i = 1;i <= N; ++ i) {
50             int px = find(i, f2);
51             if(!vis[px]) {
52                 sum ++;
53                 vis[px] = true;
54             }
55         }
56         for(int i = 0;i < num; ++ i)
57             if(fun(p[i].x, p[i].y, f2)) {
58                 sum --;
59                 K --;
60                 fun(p[i].x, p[i].y, f3);
61                 p[i].x = p[i].y = -1;
62             }
63         int flag = 0;
64         if(sum == 1 && K >= 0) {
65             for(int i = 0;i < num && K > 0; ++ i)
66                 if(p[i].x > 0 && fun(p[i].x, p[i].y, f3)) {
67                     K --;
68                     fun(p[i].x, p[i].y, f2);
69                 }
70         }
71         if(sum == 1 && K == 0)flag = 1;
72         printf("%d\n", flag);
73     }
74 }

BNUOJ 26229 Red/Blue Spanning Tree,布布扣,bubuko.com

时间: 2024-10-25 16:25:30

BNUOJ 26229 Red/Blue Spanning Tree的相关文章

HDU 4263(Red/Blue Spanning Tree-取边贪心)

Red/Blue Spanning Tree Time Limit: 10000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 979    Accepted Submission(s): 368 Problem Description Given an undirected, unweighted, connected graph, where each edg

CF1208H Red Blue Tree

CF1208H Red Blue Tree 原本应该放在这里但是这题过于毒瘤..单独开了篇blog 首先考虑如果 $ k $ 无限小,那么显然整个树都是蓝色的.随着 $ k $ 逐渐增大,每个点都会有且仅有一次变色,我们考虑维护这个变色的时间 $ t $ .如果每个点的变色时间都已经被算出来,那么我们可以轻易解决题目的查询操作和修改 $ k $ , 也就是说修改 $ k $ 本身就是个假操作..只需要考虑的是修改单点颜色. 修改单点颜色,看起来就很 $ ddp $ .树链剖分后,用$ f(x)

HDU 4896 Minimal Spanning Tree(矩阵快速幂)

题意: 给你一幅这样子生成的图,求最小生成树的边权和. 思路:对于i >= 6的点连回去的5条边,打表知907^53 mod 2333333 = 1,所以x的循环节长度为54,所以9个点为一个循环,接下来的9个点连回去的边都是一样的.预处理出5个点的所有连通状态,总共只有52种,然后对于新增加一个点和前面点的连边状态可以处理出所有状态的转移.然后转移矩阵可以处理出来了,快速幂一下就可以了,对于普通的矩阵乘法是sigma( a(i, k) * b(k, j) ) (1<=k<=N), 现在

【HDU 4408】Minimum Spanning Tree(最小生成树计数)

Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Kruskal algorithm of minimum spanning tree, XXX finds that there might be multiple solutions. Given an undirected weighted graph with n (1<=n<=100) vertex

HDOJ 题目4408 Minimum Spanning Tree(Kruskal+Matrix_Tree)

Minimum Spanning Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1408    Accepted Submission(s): 450 Problem Description XXX is very interested in algorithm. After learning the Prim algori

Lab - Modify Default Spanning Tree Behavior

Modify Default Spanning Tree Behavior Topology Objective observe what happens when the default spanning tree behavior is modified. Background Four switches have just been installed. The distribution layer switches are Catalyst 3560s, and the access l

Lab - Per-VLAN Spanning Tree Behavior

Per-VLAN Spanning Tree Behavior Topology Objectives: Observe the behavior of a separate spanning tree instance per VLAN. Change spanning tree mode to rapid spanning tree. Background: Four switches have just been installed. The distribution layer swit

Lab - Multiple Spanning Tree

Multiple Spanning Tree Topology Objective Obsrve te behavior of Multiple Spanning Tree(MST) Background Four switches have just been installed. The distribution Layer switches are Catalyst 3660s, and the access layer switches are Catalyst 2960. There

第四届华中区程序设计邀请赛暨武汉大学第十三届校赛 网络预选赛 Problem 1566 - C - Spanning Tree

Description You are given a graph with N nodes and M edges. Then every time you are required to add an additional edge with weight Wi connecting the node Ai and Bi in the graph, and then calculate the sum of the edges' weight of the Minimum Spanning