【luogu P3623 [APIO2008]免费道路】 题解
题目链接:https://www.luogu.org/problemnew/show/P3623
说是对克鲁斯卡尔的透彻性理解
正解:
先考虑加入水泥路,然后再考虑加入剩下必须要加入的最少鹅卵石路。
之后对原图再跑最小生成树
先跑鹅卵石路到k条。
再从所有水泥路中直到成为最小生成树。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 100000 + 10;
int n, m, k, fa[maxn], cnt, shuini, eluan, tot;
bool f[maxn];
struct edge{
int u, v, w;
}e[maxn<<2], ans[maxn<<2];
bool cmp(edge a, edge b)
{
return a.w < b.w;
}
int find(int x)
{
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
int main()
{
scanf("%d%d%d",&n,&m,&k);
for(int i = 1; i <= n; i++) fa[i] = i;
for(int i = 1; i <= m; i++)
{
int opt;
scanf("%d%d%d",&e[i].u, &e[i].v, &opt);
if(opt == 0) e[i].w = 1, eluan++;
else e[i].w = 0, shuini++;
}
if(eluan < k)
{
cout<<"no solution\n";
return 0;
}
sort(e+1, e+1+m, cmp);
for(int i = 1; i <= shuini; i++)
{
if(cnt == n-1) break;
int rx, ry;
rx = find(e[i].u), ry = find(e[i].v);
if(rx != ry)
{
fa[ry] = rx;
cnt++;
}
}
int eluanmust = 0;
for(int i = shuini+1; i <= m; i++)
{
if(cnt == n-1) break;
int rx, ry;
rx = find(e[i].u), ry = find(e[i].v);
if(rx != ry)
{
fa[ry] = rx;
f[i] = 1, eluanmust++;
cnt++;
}
}
if(cnt != n-1 || eluanmust > k)
{
cout<<"no solution\n";
return 0;
}
for(int i = 1; i <= n; i++) fa[i] = i;
cnt = 0;
for(int i = shuini+1; i <= m; i++)
{
if(cnt == n-1) break;
if(f[i] == 1)
{
int rx, ry;
rx = find(e[i].u), ry = find(e[i].v);
fa[ry] = rx;
cnt++;
ans[++tot].u = e[i].u;
ans[tot].v = e[i].v;
ans[tot].w = e[i].w^1;
//printf("%d %d %d\n",e[i].u, e[i].v, e[i].w^1);
}
}
for(int i = shuini+1; i <= m; i++)
{
if(cnt == k) break;
int rx, ry;
rx = find(e[i].u), ry = find(e[i].v);
if(rx != ry)
{
fa[ry] = rx;
cnt++;
ans[++tot].u = e[i].u;
ans[tot].v = e[i].v;
ans[tot].w = e[i].w^1;
//printf("%d %d %d\n",e[i].u, e[i].v, e[i].w^1);
}
}
if(cnt < k)
{
cout<<"no solution\n";
return 0;
}
for(int i = 1; i <= shuini; i++)
{
if(cnt == n-1) break;
int rx, ry;
rx = find(e[i].u), ry = find(e[i].v);
if(rx != ry)
{
fa[ry] = rx;
cnt++;
ans[++tot].u = e[i].u;
ans[tot].v = e[i].v;
ans[tot].w = e[i].w^1;
//printf("%d %d %d\n",e[i].u, e[i].v, e[i].w^1);
}
}
if(cnt != n-1)
{
cout<<"no solution\n";
return 0;
}
for(int i = 1; i <= tot; i++)
printf("%d %d %d\n",ans[i].u, ans[i].v, ans[i].w);
return 0;
}
【luogu P3623 [APIO2008]免费道路】 题解的更多相关文章
- [火星补锅] 水题大战Vol.2 T2 && luogu P3623 [APIO2008]免费道路 题解
前言: 如果我自己写的话,或许能想出来正解,但是多半会因为整不出正确性而弃掉. 解析: 这题算是对Kruskal的熟练运用吧. 要求一颗生成树.也就是说,最后的边数是确定的. 首先我们容易想到一个策略 ...
- 题解 Luogu P3623 [APIO2008]免费道路
[APIO2008]免费道路 题目描述 新亚(New Asia)王国有 N 个村庄,由 M 条道路连接.其中一些道路是鹅卵石路,而其它道路是水泥路.保持道路免费运行需要一大笔费用,并且看上去 王国不可 ...
- P3623 [APIO2008]免费道路
3624: [Apio2008]免费道路 Time Limit: 2 Sec Memory Limit: 128 MBSec Special Judge Submit: 2143 Solved: 88 ...
- [BZOJ3624][Apio2008]免费道路
[BZOJ3624][Apio2008]免费道路 试题描述 输入 输出 输入示例 输出示例 数据规模及约定 见“输入”. 题解 第一步,先尽量加入 c = 1 的边,若未形成一个连通块,则得到必须加入 ...
- [APIO2008]免费道路
[APIO2008]免费道路 BZOJ luogu 先把必须连的鹅卵石路连上,大于k条no solution 什么样的鹅卵石路(u,v)必须连?所有水泥路都连上仍然不能使u,v连通的必须连 补全到k条 ...
- bzoj 3624: [Apio2008]免费道路 生成树的构造
3624: [Apio2008]免费道路 Time Limit: 2 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 111 Solved: 4 ...
- BZOJ 3624: [Apio2008]免费道路
3624: [Apio2008]免费道路 Time Limit: 2 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 1201 Solved: ...
- [Apio2008]免费道路[Kruscal]
3624: [Apio2008]免费道路 Time Limit: 2 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 1292 Solved: ...
- Kruskal算法及其类似原理的应用——【BZOJ 3654】tree&&【BZOJ 3624】[Apio2008]免费道路
首先让我们来介绍Krukal算法,他是一种用来求解最小生成树问题的算法,首先把边按边权排序,然后贪心得从最小开始往大里取,只要那个边的两端点暂时还没有在一个联通块里,我们就把他相连,只要这个图里存在最 ...
随机推荐
- 一:HttpClient知识整理
一:httpclient 简介 HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支 ...
- AndroidVideoCache 框架源码分析
1.简析: 在客户端播放视频的使用,容易出现这样的一个问题.在网络状况不好的情况下,视频流很容易卡顿或者中断,即使播放软件本身有一点的缓存能力,但是这个往往不够,造成播放失败,卡顿. AndroidV ...
- java设计模式-观察者模式学习
最近学习了设计模式中的观察者模式,在这里记录下学习成果. 观察者模式,个人理解:就是一个一对多模型,一个主体做了事情,其余多个主体都可以观察到.只不过这个主体可以决定谁去观察他,以及做什么事情可以给别 ...
- java.sql.SQLException: Incorrect string value: '\xF0\x9F\x9A\x80\xF0\x9F...' for column 'name' at row 1
1.异常提示: 12:59:10.000 [http-nio-8080-exec-40] DEBUG o.s.j.s.SQLStateSQLExceptionTranslator - Extracte ...
- js权威指南学习笔记(二)表达式与运算符
1.数组初始化表达式 数组直接量中的列表逗号之间的元素可以省略,这时省略的空位会填充undefined.如: 2 2 1 var arr = [1,,,,,6]; 2 ...
- Hibernate (ORM)
1 框架体系结构 2 hibernate入门 2.1 ORM框架 Hibernate是一个数据持久化层的ORM框架. Object:对象,java对象,此处特指JavaBean Relational: ...
- Windows 8 Metro风格颜色表-Metro colours
http://huaban.com/pins/538986818
- Scrum----学习心得
Scrum学习心得 什么是敏捷开发? 敏捷开发(Agile Development)是一种以人为核心.迭代.循序渐进的 开发方法.它不是一门技术,它是一种开发方法,也就是一种软件开发的流程,它会指导我 ...
- QML Delegate中访问该持有者的方式 附加属性(转载)
http://blog.csdn.net/yuxiaohen/article/details/17226971 用法很奇葩记录一下,实测可以,用于弱化delegate与持有者的依赖 delegat ...
- angular自定义指令解决IE89不支持input的placeholder属性
下面代码实测通过,直接copy到本地运行即可. <!DOCTYPE html> <html> <head> <meta charset="UTF-8 ...