[APIO2008]免费道路

BZOJ

luogu

先把必须连的鹅卵石路连上,大于k条no solution

什么样的鹅卵石路(u,v)必须连?所有水泥路都连上仍然不能使u,v连通的必须连

补全到k条鹅卵石路,补不全no solution

最后用水泥路构出生成树,构不出no solution

#include<bits/stdc++.h>
using namespace std;
const int _=1e5+5;
int re(){
int x=0,w=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*w;
}
bool vis[_];
int n,m,k,cnt,fa[_],ans[_];
struct edge{int u,v,c;}e[_];
int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
void unite(int x,int y){
x=find(x);y=find(y);
if(x^y)fa[x]=y;
}
int main(){
n=re(),m=re(),k=re();
for(int i=1;i<=n;i++)fa[i]=i;
for(int i=1;i<=m;i++){
int u=re(),v=re(),c=re();
e[i]=(edge){u,v,c};
if(c)unite(u,v);
}
for(int i=1;i<=m;i++){
if(find(e[i].u)^find(e[i].v)){
unite(e[i].u,e[i].v);
ans[++cnt]=i;vis[i]=1;
}
}
if(cnt>k){puts("no solution");return 0;}
for(int i=1;i<=n;i++)fa[i]=i;
for(int i=1;i<=cnt;i++)
unite(e[ans[i]].u,e[ans[i]].v);
for(int i=1;i<=m;i++){
if(e[i].c||vis[i])continue;
if(find(e[i].u)^find(e[i].v)){
unite(e[i].u,e[i].v);
ans[++cnt]=i;
if(cnt==k)break;
}
}
if(cnt<k){puts("no solution");return 0;}
for(int i=1;i<=m;i++){
if(!e[i].c)continue;
if(find(e[i].u)^find(e[i].v)){
unite(e[i].u,e[i].v);
ans[++cnt]=i;
if(cnt==n-1)break;
}
}
if(cnt<n-1){puts("no solution");return 0;}
for(int i=1;i<n;i++)
printf("%d %d %d\n",e[ans[i]].u,e[ans[i]].v,e[ans[i]].c);
return 0;
}

[APIO2008]免费道路的更多相关文章

  1. [BZOJ3624][Apio2008]免费道路

    [BZOJ3624][Apio2008]免费道路 试题描述 输入 输出 输入示例 输出示例 数据规模及约定 见“输入”. 题解 第一步,先尽量加入 c = 1 的边,若未形成一个连通块,则得到必须加入 ...

  2. bzoj 3624: [Apio2008]免费道路 生成树的构造

    3624: [Apio2008]免费道路 Time Limit: 2 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 111  Solved: 4 ...

  3. 题解 Luogu P3623 [APIO2008]免费道路

    [APIO2008]免费道路 题目描述 新亚(New Asia)王国有 N 个村庄,由 M 条道路连接.其中一些道路是鹅卵石路,而其它道路是水泥路.保持道路免费运行需要一大笔费用,并且看上去 王国不可 ...

  4. BZOJ 3624: [Apio2008]免费道路

    3624: [Apio2008]免费道路 Time Limit: 2 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 1201  Solved:  ...

  5. [Apio2008]免费道路[Kruscal]

    3624: [Apio2008]免费道路 Time Limit: 2 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 1292  Solved:  ...

  6. P3623 [APIO2008]免费道路

    3624: [Apio2008]免费道路 Time Limit: 2 Sec Memory Limit: 128 MBSec Special Judge Submit: 2143 Solved: 88 ...

  7. Kruskal算法及其类似原理的应用——【BZOJ 3654】tree&&【BZOJ 3624】[Apio2008]免费道路

    首先让我们来介绍Krukal算法,他是一种用来求解最小生成树问题的算法,首先把边按边权排序,然后贪心得从最小开始往大里取,只要那个边的两端点暂时还没有在一个联通块里,我们就把他相连,只要这个图里存在最 ...

  8. [APIO2008]免费道路(生成树)

    新亚(New Asia)王国有 N 个村庄,由 M 条道路连接.其中一些道路是鹅卵石路,而其它道路是水泥路.保持道路免费运行需要一大笔费用,并且看上去 王国不可能保持所有道路免费.为此亟待制定一个新的 ...

  9. 【bzoj3624】Apio2008—免费道路

    http://www.lydsy.com/JudgeOnline/problem.php?id=3624 (题目链接) 题意 给出一张无向图,其中有0类边和1类边.问能否构成正好有K条0类边的生成树, ...

随机推荐

  1. 转:MyBatis学习总结(Mybatis总结精华文章)

    http://www.cnblogs.com/xdp-gacl/tag/MyBatis%E5%AD%A6%E4%B9%A0%E6%80%BB%E7%BB%93/ 当前标签: MyBatis学习总结   ...

  2. Gentoo:startx出现Failed to load module问题

    安装完xorg-server后,startx启动桌面环境,出现缺少模块错误. 查看log: cat /var/log/Xorg.0.log | grep EE [75.403] (EE) Failed ...

  3. elasticsearch 插入数据

    1.单条插入(推荐设定主键id防止重复) public static String addIndex(String index,String type,HashMap<String, Objec ...

  4. Centos 通过yum的方式升级内核

    在安装某些软件时,可能对我们的系统内核版本有要求. 比如在安装docker要满足一定的条件,对于centos系统,要求必须是64位,并且内核版本是3.10以上. 如果你的centos操作系统内核低于3 ...

  5. Laravel一些功能的简单总结

    一.事件/广播 1.命令(事件在目录Events中, 监听器在Listeners中)artisan make:event 或 artisan event:generate前者创建一个事件,后者根据pr ...

  6. axios 设置超时时间 timeout

    this.$ajax.post('', {operate: type, ids: this.data.id.toString(), data_type: 'ips'}, {timeout: 60000 ...

  7. sed `grep` 查找并替换

    sed "s/libletvwatermark/libletv_watermark/" `grep -rl libletvwatermark` grep [options] 3.主 ...

  8. jquery字符串转json

    var data; var json='[{"CityId":18,"CityName":"西安","ProvinceId&quo ...

  9. SRIO常用缩写

    HELLO:Header Encoded Logical Layer Optimized (HELLO) format FTYPE:format type TTYPE:transaction type ...

  10. Socket编程之Tomcat模拟_采坑汇总

    用java.net.Socket来模拟实现Tomcat,碰到了一些坑,大部分是没有想到的,记录下来自查. 直接上代码, public class TomcatDemo { private static ...