先把水泥路建生成树,然后加鹅卵石路,这里加的鹅卵石路是一定要用的(连接各个联通块),然后初始化并查集,先把必需的鹅卵石路加进去,然后随便加鹅卵石路直到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. excludepathpatterns 无效

    踩坑了,调了好久才调出来. 原因:  访问的API /XXX 已经转换为 /error 了.  把“/error” 也加入 excludepathpatterns 里面即可.

  2. response对象学习

    import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpSer ...

  3. Maven使用tomcat7-maven-plugin插件run时出现错误: A child container failed during start java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component

    错误如下: A child container failed during startjava.util.concurrent.ExecutionException: org.apache.catal ...

  4. dnsmasq possible DNS-rebind attack detected错误

    最近在做openwrt的平台,dns使用的是dnsmasq,但是通过板子上网,将PC的dns设置成板子的时候,发现百度等都可以,但是公司邮箱打不开.公司邮箱的域名 xx-xx-notes.xxx.co ...

  5. IE将開始屏蔽旧版ActiveX控件

    微软IE团队上周宣布将在IE中屏蔽旧版本号的ActiveX控件以加强IE的安全性.首先会被禁用的旧版本号ActiveX控件包括: J2SE 1.4, 低于update 43 的版本号 J2SE 5.0 ...

  6. Scrum 常见错误实践 之 形式化的站会

    站会作为一个团队最容易实施的敏捷实践,为广大team leader和老板们所喜欢,但大部分程序员却很抵触.其主要原因就是很多时候站会都流于形式,没能帮助团队成员解决问题.改进效率. 一种常见的情况就是 ...

  7. oracle技术总结

    http://www.cnblogs.com/jimeper/ http://blog.csdn.net/dragonxiangfu http://www.boobooke.com/bbs/threa ...

  8. Use Local Or Global Index?

    常常我们须要将大表依据分区键进行分区,当建立索引的时候.我们究竟使用local 还是global 索引呢 先看看两种索引的特点: 本地索引特点: 1. 本地索引一定是分区索引.分区键等同于表的分区键. ...

  9. 深度学习笔记之关于基本思想、浅层学习、Neural Network和训练过程(三)

    不多说,直接上干货! 五.Deep Learning的基本思想 假设我们有一个系统S,它有n层(S1,…Sn),它的输入是I,输出是O,形象地表示为: I =>S1=>S2=>….. ...

  10. onDestroy强制退出后,process crash的处理

    from http://bbs.9ria.com/thread-248722-1-1.html     一般情况,我们在执行测试的过程中都会调用tearDwon方法,以Robotium为例,我们在te ...