APIO2008免费道路 Bsoj2714 并查集+两次最小生成树
来源:互联网 发布:淘宝网广西金源马蹄粉 编辑:程序博客网 时间:2024/06/13 04:07
哎。。。
好几天没有A题了。。
真的感觉很蛋疼,抽了7天去复习。。后来发现其实都玩了。。
这个题还算是比较水
首先我们用并查集先随便的求一个最小生成树
然后把在最小生成树上的0边拿出来,如果大于k那么无解
然后第二次处理的时候把上一步求出来的一定在生成树里的边先加进去
然后再加一些0边,加到k为止,最后再加些1边保证是树就可以了
本题最重要的收获是我学会了新的编程风格啊感觉不错。。!!!!
真的感觉 这种很美观啊。。。但是在调用函数的时候要打空格么很奇怪?求解释。。。。
//begin new type style.....#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>#define MAX 100009#define get_father find#define rep(i,j,k) for (int i=j; i<=k; i++)using namespace std;int num = 0;int n, m, L=0, R=0, father[MAX], k;struct wbysr{int from,to,done;}l[MAX], r[MAX];inline int find (int x){return father[x]==x ? x : father[x] = get_father(father[x]);}inline void Union (int x,int y){int fx = get_father(x), fy = get_father(y);if (fx != fy)father[fy] = fx;}int main(){scanf ("%d%d%d", &n, &m, &k);rep (i, 1, m){int a1,a2,a3;scanf ("%d%d%d", &a1, &a2, &a3);if (!a3)l[++L].from = a1, l[L].to = a2;elser[++R].from = a1, r[R].to = a2;}rep (i, 1, n)father[i] = i;rep (i, 1, R)if (find(r[i].from) != find(r[i].to))Union(r[i].from, r[i].to);rep (i, 1, L)if (find(l[i].from) != find(l[i].to))Union(l[i].from, l[i].to), num++, l[i].done = 1;if (num > k){printf ("no solution\n");return 0;}rep (i, 1, n)father[i] = i;rep (i, 1, L)if (l[i].done)Union(l[i].from,l[i].to);rep (i, 1, L)if (find(l[i].from) != find(l[i].to) && num<k)Union(l[i].from,l[i].to), num++, l[i].done = 1;rep (i, 1, R)if(find(r[i].from) != find(r[i].to))Union(r[i].from,r[i].to), r[i].done = 1;rep (i, 1, L)if(l[i].done)printf ("%d %d 0\n",l[i].from,l[i].to);rep (i, 1, R)if(r[i].done)printf ("%d %d 1\n",r[i].from,r[i].to);return 0;}
0 0
- APIO2008免费道路 Bsoj2714 并查集+两次最小生成树
- bzoj 3624: [Apio2008]免费道路(并查集+生成树+乱搞)
- [BZOJ3624][Apio2008]免费道路(贪心+并查集)
- 【bzoj3624】[Apio2008]免费道路 贪心+并查集
- [BZOJ3624][Apio2008]免费道路(并查集)
- 【BZOJ3624】【APIO2008】免费道路(贪心+并查集)
- [BZOJ 3624][Apio2008]免费道路:最小生成树
- [BZOJ]3624: [Apio2008]免费道路 最小生成树
- 【APIO2008T2】免费道路-并查集+生成树
- 【BZOJ】【P3624】【Apio2008】【免费道路】【题解】【生成树】
- BZOJ3624 [Apio2008]免费道路
- 3624: [Apio2008]免费道路
- 3624: [Apio2008]免费道路
- bzoj3624 [Apio2008]免费道路
- 【并查集】【生成树】【APIO 2008】【bzoj 3624】免费道路
- 【bzoj 3624】: [Apio2008]免费道路
- BZOJ 3264 [Apio2008]免费道路
- BZOJ P3624[Apio2008]免费道路
- qq不定时掉线的对策
- spring test---restful与文件上传
- python不是内部命令或外部命令,也不是可执行程序解决方案
- -----------------委托的使用场合 (使用方式)----------------
- 南阳 686 An Interesting Sequence
- APIO2008免费道路 Bsoj2714 并查集+两次最小生成树
- 落水飞天java算法大全
- 新浪开发平台应用storage和ueditor
- 爱摘苹果的小明
- 委托和事件
- code strike round 1 B
- xe5-xe6 安装容易失败和耗时总结
- windows平台Emacs单实例原理、设置及右键菜单的添加
- spring基础学习-简单实例(一)