【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算法,他是一种用来求解最小生成树问题的算法,首先把边按边权排序,然后贪心得从最小开始往大里取,只要那个边的两端点暂时还没有在一个联通块里,我们就把他相连,只要这个图里存在最 ...
随机推荐
- JavaScript数组循环遍历之forEach
1. js 数组循环遍历. 数组循环变量,最先想到的就是 for(var i=0;i<count;i++)这样的方式了. 除此之外,也可以使用较简便的forEach 方式 2. forEac ...
- struts2 国际化语言转换
学习struts2,了解了使用struts2的配置文件可以走向国际化,实现页面的语言转换.我已中文和英文为例,简单的实现登录页面的国际化 废话不多说,上代码 一,login.jsp页面 使用s标签&l ...
- Hadoop序列化程序报错
这个错误指向序列化类,注意实现的Read和Wirte顺序一定要一致,并且要使用基本类型,不要使用包装类. java.lang.Exception: java.io.EOFException at or ...
- JS判断是否到达页面底部
<script type="text/javascript">//判断整个文档到底部$(window).scroll(function(){ //滚动条所在位置的 ...
- Android setTag()/getTag()
View中的setTag(Onbect)表示给View添加一个格外的数据,以后可以用getTag()将这个数据取出来. 可以用在多个Button添加一个监听器,每个Button都设置不同的setTag ...
- svn环境搭建
(一)svn介绍 项目管理中的版本控制问题 通常软件开发由多人协作开发,如果对代码文件.文档等没有进行版本控制,将会出现很多问题: 备份多个版本,占用磁盘空间大 解决代码冲突困难 容易引发BUG ...
- div浮层,滚动条移动,保持位置不变的4种方法
div浮层,滚动条移动,保持位置不变的4种方法 div在顶部不变.滚动条滚动,div还是在顶部! 直接上传源码 了: 方法一: <!DOCTYPE html PUBLIC "-//W3 ...
- CentOS7 安装tomcat为系统服务器 Systemctl管理Tomcat,并设置开机启动
本文转载:http://blog.chinaunix.net/uid-24648266-id-5729891.html CentOS7开始,从/etc/init.d脚本改为了systemctl管理服务 ...
- Newtonsoft.Json code
序列化 Product product = new Product(); product.ExpiryDate = new DateTime(2008, 12, 28); JsonSerializer ...
- 【Leetcode】【Medium】Two Sum
Given an array of integers, find two numbers such that they add up to a specific target number. The ...