先把水泥路建生成树,然后加鹅卵石路,这里加的鹅卵石路是一定要用的(连接各个联通块),然后初始化并查集,先把必需的鹅卵石路加进去,然后随便加鹅卵石路直到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]免费道路【生成树+贪心】的更多相关文章

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

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

  2. 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 这 ...

  3. BZOJ 3624: [Apio2008]免费道路 [生成树 并查集]

    题意: 一张图0,1两种边,构造一个恰有k条0边的生成树 优先选择1边构造生成树,看看0边是否小于k 然后保留这些0边,补齐k条,再加1边一定能构成生成树 类似kruskal的证明 #include ...

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

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

  5. BZOJ 3624 [Apio2008]免费道路:并查集 + 生成树 + 贪心【恰有k条特殊路径】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3624 题意: 给你一个无向图,n个点,m条边. 有两种边,种类分别用0和1表示. 让你求一 ...

  6. BZOJ.3624.[APIO2008]免费道路(Kruskal)

    题目链接 我们发现有些白边是必须加的,有些是多余的. 那么我们先把所有黑边加进去,然后把必须要加的白边找出来. 然后Kruskal,把必须要加的白边先加进去,小于K的话再加能加的白边.然后加黑边. 要 ...

  7. 【BZOJ3624】【APIO2008】免费道路 [生成树][贪心]

    免费道路 Time Limit: 2 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description Input Output Sampl ...

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

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

  9. 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 ...

随机推荐

  1. poj1308+HOJ1325,判断是否为树

    POJ 应该是判断是否为简单无环连通图,用并查集直接秒杀即可,而HOJ的是有向树,还需判断所有点的入度必需小于2,用一个类似hash[]数组判断一下即可, ////判断树之一:入度<=1:三:点 ...

  2. 收集的一些Redis操作技巧教程

    redis(1).redis入门 redis(2).redis数据类型 redis(3).基于jedis.spring-data-redis 连接操作redis redis(4).基于redis 构建 ...

  3. Java中@SuppressWarnings注解用法(转)

    背景: J2SE提供的最后一个注解是@SuppressWarnings.该批注的作用是给编译器一条指令,告诉它对被批注的代码元素内部的某些警告保持静默. @SuppressWarnings注解允许您选 ...

  4. Spring Boot项目错误:Error parsing lifecycle processing instructions

    pom.xml文件错误:Error parsing lifecycle processing instructions 解决方法:清空.m2/repository下的所有依赖文件,重新下载即可解决该问 ...

  5. 转: eclipse 快捷键列表(功能清晰版本)

    转自: http://www.uml.org.cn/mobiledev/201110092.asp Eclipse 在开发中使用到的快捷键很实用噢 Ctrl+1 快速修复(最经典的快捷键,就不用多说了 ...

  6. 磁盘显示为GPT(保护分区)

    问题描述:PE进入系统,在计算机管理里面磁盘显示为GPT(保护分区).此时硬盘是不能重新分区或者格式化的. 解决思路:低版本的WIndows(PE)是不支持GPT分区的,我们需要使用系统自带的Disk ...

  7. [Angular] Communicate Between Components Using Angular Dependency Injection

    Allow more than one child component of the same type. Allow child components to be placed within the ...

  8. Deepin-添加path

    以管理员权限添加path(Debian系列) sudo gedit /etc/profile 添加path路径格式是: export PATH=”$PATH:your path1:your path2 ...

  9. GLib-GObject-WARNING **: Two different plugins tried to register

    使用 Ubuntu vivid 的 glib-2.44.1 的程序总是打印类似这种警告: GLib-GObject-WARNING **: Two different plugins tried to ...

  10. 关于MySQL的TPS和QPS

    TPS - Transactions Per Second(每秒传输的事物处理个数),这是指server每秒处理的事务数,支持事务的存储引擎如InnoDB等特有的一个性能指标. 计算方法: TPS = ...