【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算法,他是一种用来求解最小生成树问题的算法,首先把边按边权排序,然后贪心得从最小开始往大里取,只要那个边的两端点暂时还没有在一个联通块里,我们就把他相连,只要这个图里存在最 ...
随机推荐
- 七、cent OS下干净卸载mysql
使用以下命令查看当前安装mysql的情况rpm -qa | grep -i mysql显示之前安装的东西,示例:MySQL-client-5.5.25a-1.rhel5MySQL-server-5.5 ...
- iOS之nib、xib及storyboard的区别及storyboard的加载过程
先讲述下nib, nib是3.0版本以前的产物,在终端下我们可以看到,NIB其实是一个文件夹,里面有可执行的二进制文件: 区分xib和storyboard的区别? 不同点: 1> 无论nib也好 ...
- RegExp.prototype.exec()使用技巧
RegExp.prototype.exec() exec() 方法在一个指定字符串中执行一个搜索匹配.返回一个结果数组或 null. 如果你只是为了判断是否匹配(true或 false),可以使用 R ...
- CSS基础-CSS三大特性
继承性 层叠性 优先级 优先级权重 !important
- csharp:DropDownComboxTreeView
using System; using System.Collections.Generic; using System.Text; using System.Drawing; using Syste ...
- inline-block和float的区别,什么时候使用
文章转载于新浪博客http://blog.sina.com.cn/s/blog_5f39af320101qckt.html 只用于学习交流 什么时候使用inline-block,什么时候使用float ...
- Android 高速录像(2)
private void startRecordVideo() { if (index == VIDEO_1080) { if (!supported1080P120Fps) { showToast( ...
- 并发包CallableAndFuture
/** * * @描述: Callable Future * * 程序运行一个线程,线程运行结束后,我们可以获取另一个线程的结果 * * @作者: Wnj . * @创建时间: 2017年5月16日 ...
- css属性值语法解读
//margin 形式语法: [ <length> | <percentage> | auto ]{1,4} //合法实例: margin: style /*单值语法 */ 举 ...
- Windows(7)上不能启动MySQL服务(位于本地计算机上)错误1067 :进程意外终止
就这段时间,很多人在抱怨为什么自己的MySQL又打不开问题. 就“Windows(7)上不能启动MySQL服务(位于本地计算机上)错误1067 :进程意外终止”这个问题,我想到了几种方案解决: 一.首 ...