bzoj 3624: [Apio2008]免费道路【生成树+贪心】
先把水泥路建生成树,然后加鹅卵石路,这里加的鹅卵石路是一定要用的(连接各个联通块),然后初始化并查集,先把必需的鹅卵石路加进去,然后随便加鹅卵石路直到k条,然后加水泥路即可。
注意判断无解
#include<iostream>
#include<cstdio>
using namespace std;
const int N=20005,M=100005;
int n,m,k,f[N],ta,tb,con;
bool v[M];
struct qwe
{
int u,v;
qwe(int U=0,int V=0)
{
u=U,v=V;
}
}a[M],b[M],ans[N];
int read()
{
int r=0,f=1;
char p=getchar();
while(p>'9'||p<'0')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
int zhao(int x)
{
return f[x]==x?x:f[x]=zhao(f[x]);
}
int main()
{
n=read(),m=read(),k=read();
for(int i=1;i<=m;i++)
{
int x=read(),y=read(),z=read();
if(z)
a[++ta]=qwe(x,y);
else
b[++tb]=qwe(x,y);
}
if(tb<k)
{
printf("no solution\n");
return 0;
}
for(int i=1;i<=n;i++)
f[i]=i;
for(int i=1;i<=ta;i++)
{
int fu=zhao(a[i].u),fv=zhao(a[i].v);
if(fu!=fv)
f[fu]=fv;
}
for(int i=1;i<=tb;i++)
{
int fu=zhao(b[i].u),fv=zhao(b[i].v);
if(fu!=fv)
f[fu]=fv,v[i]=1,k--;
}
if(k<0)
{
printf("no solution\n");
return 0;
}
for(int i=1;i<=n;i++)
f[i]=i;
for(int i=1;i<=tb;i++)
if(v[i])
{
f[zhao(b[i].u)]=zhao(b[i].v);
ans[++con]=qwe(b[i].u,b[i].v);
}
for(int i=1;i<=tb&&k;i++)
{
int fu=zhao(b[i].u),fv=zhao(b[i].v);
if(fu!=fv)
{
f[fu]=fv,k--;
ans[++con]=qwe(b[i].u,b[i].v);
}
}
if(k)
{
printf("no solution\n");
return 0;
}
for(int i=1;i<=con;i++)
printf("%d %d 0\n",ans[i].u,ans[i].v);
for(int i=1;i<=ta;i++)
{
int fu=zhao(a[i].u),fv=zhao(a[i].v);
if(fu!=fv)
{
f[fu]=fv;
printf("%d %d 1\n",a[i].u,a[i].v);
}
}
return 0;
}
bzoj 3624: [Apio2008]免费道路【生成树+贪心】的更多相关文章
- bzoj 3624: [Apio2008]免费道路 生成树的构造
3624: [Apio2008]免费道路 Time Limit: 2 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 111 Solved: 4 ...
- Bzoj 3624: [Apio2008]免费道路 (贪心+生成树)
Sample Input 5 7 2 1 3 0 4 5 1 3 2 0 5 3 1 4 3 0 1 2 1 4 2 1 Sample Output 3 2 0 4 3 0 5 3 1 1 2 1 这 ...
- BZOJ 3624: [Apio2008]免费道路 [生成树 并查集]
题意: 一张图0,1两种边,构造一个恰有k条0边的生成树 优先选择1边构造生成树,看看0边是否小于k 然后保留这些0边,补齐k条,再加1边一定能构成生成树 类似kruskal的证明 #include ...
- BZOJ 3624: [Apio2008]免费道路
3624: [Apio2008]免费道路 Time Limit: 2 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 1201 Solved: ...
- BZOJ 3624 [Apio2008]免费道路:并查集 + 生成树 + 贪心【恰有k条特殊路径】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3624 题意: 给你一个无向图,n个点,m条边. 有两种边,种类分别用0和1表示. 让你求一 ...
- BZOJ.3624.[APIO2008]免费道路(Kruskal)
题目链接 我们发现有些白边是必须加的,有些是多余的. 那么我们先把所有黑边加进去,然后把必须要加的白边找出来. 然后Kruskal,把必须要加的白边先加进去,小于K的话再加能加的白边.然后加黑边. 要 ...
- 【BZOJ3624】【APIO2008】免费道路 [生成树][贪心]
免费道路 Time Limit: 2 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description Input Output Sampl ...
- [APIO2008]免费道路(生成树)
新亚(New Asia)王国有 N 个村庄,由 M 条道路连接.其中一些道路是鹅卵石路,而其它道路是水泥路.保持道路免费运行需要一大笔费用,并且看上去 王国不可能保持所有道路免费.为此亟待制定一个新的 ...
- 3624: [Apio2008]免费道路
Description Input Output Sample Input 5 7 2 1 3 0 4 5 1 3 2 0 5 3 1 4 3 0 1 2 1 4 2 1 Sample Output ...
随机推荐
- 移动web页面字体大小二
按设计稿中的字体大小设置页面字体大小(px),在手机端,因为不同设备会有不同,换算成rem单位.rem是“font size of the root element”,可以通过这篇文章了解http:/ ...
- foobar2000 iOS使用,并连接PC的歌曲进行播放
foobar2000移动版下载地址:http://mobile.foobar2000.com/ 要实现歌曲互通有两种方法,使用iOS客户端的FTP Server用PC上传歌曲到iOS手机,和在PC上使 ...
- Java读取文件时中文路径处理
读取文件路径时可能存在以下情况: 1.空格,如果出现空格会转变成“%20” 2.中文路径,如果出现中文路径会变成URI编码“%e5%bc%80%e5%8f%91%e5%b7%a5%e7%a8%8b” ...
- 对CSS尺寸单位'em'的长期误解
一直以来认为'em'是相对于父元素的字体大小. 直到今天学习移动WEB开发,重新复习css的尺寸大小时,惊奇发现:对em深深的误解了!!! 在CSS官网对em的解释实例是: a. h1{line-he ...
- java中可以通过调用ping命令来判断网络是否连接正常
原文:http://www.open-open.com/code/view/1446382328960 import java.io.BufferedReader; import java.io.IO ...
- java.sql.SQLException: [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称而且未指定默认驱动程序解决方法
开发程序须要登录功能 .就不想用大数据库.直接用java连接access. 在自己机器上一切正常, url直连 和配置数据源都没有问题. 公布到windows server2008 上 , ...
- CentOS 5.11安装配置LAMP服务器(Apache+PHP5+MySQL)
http://www.osyunwei.com/archives/8880.html 准备篇: CentOS 5.x系统安装配置图解教程 http://www.osyunwei.com/archive ...
- Hadoop-异常-Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/avro/io/DatumReader
//maven org.apache.avr 下载不完全 ,去maven If you are using maven to build your jar, you need to add the ...
- 【Facebook的UI开发框架React入门之八】Image的使用简单介绍(iOS平台)-goodmao
--------------------------------------------------------------------------------------------------- ...
- Python中解决中文乱码问题
乱码原因:因为你的文件声明为utf-8,并且也应该是用utf-8的编码保存的源文件.但是windows的本地默认编码是cp936,也就是gbk编码,所以在控制台直接打印utf-8的字符串当然是乱码了. ...