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 ...
随机推荐
- python学习之 -- 数据序列化
json / pickle 数据序列化 序列化定义:把变量从内存中变成可存储或传输的过程称为序列化.反序列化:把变量内容从序列化的对象重新读到内存里称为反序列胡. 序列化模块之--pickle使用注意 ...
- D. Little Artem and Dance---cf669D
http://codeforces.com/problemset/problem/669/D 题目大意: 有n对人 男生和女生 开始时 每个人的标号是(1,2,3,...n) 女生们围成一个圈 男 ...
- T1992 聚会 codevs
http://codevs.cn/problem/1992/ 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 小S 想要从某地 ...
- 运行mapreduce - java.lang.InterruptedException
错误日志: 2018-11-19 05:23:51,686 WARN [main] util.NativeCodeLoader (NativeCodeLoader.java:<clinit> ...
- HDU OJ Max sum 题目1003
#include <iostream> #include<stdio.h> #include<stdlib.h> using namespace std; i ...
- Structual设计--Bridge模式
1.意图 将抽象部分与它的实现部分分离.使他们都能够独立地变化. 2.别名 Handle/Body 3.动机 当一个抽象对象可能有多个实现时,通经常使用继承来协调它们.抽象类定义对该抽象的接口.而详细 ...
- vux-uploader 图片上传组件
1.网址:https://github.com/greedying/vux-uploader 2.安装 npm install vux-uploader --save npm install --sa ...
- sql跟踪及tkprof使用
简述 在oracle数据库中,awr是关于数据库系统总体的负载情况和运行情况的报告.而当系统负载都显示正常,而client运行某些动作响应非常慢,或者某些终端连接的会话运行缓慢或异常时,就须要用到会话 ...
- Qt实现一个简单的TextEditor
使用QT实现简单的TextEditor: 首先在窗口添加部件TextEditor,并设置中文字符 MainWindow::MainWindow(QWidget *parent) : QMainWind ...
- Ubuntu 14.04正式公布,一个不眠之夜
请看下图: 这就是Ubuntu 14.04 LTS桌面版本号的一份视图.感觉既亲切,又寻常,可是,没有什么大的变化.注意:这个Ubuntu桌面版本号要陪伴我们长达5年之久! 直到4月18日(北京时间) ...