题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3624

题意:

  给你一个无向图,n个点,m条边。

  有两种边,种类分别用0和1表示。

  让你求一棵生成树,使得这棵树中恰好有k条0种类的边。输出每一条边的两端点和种类。

  若无解,则输出"no solution"。

题解:

  让0和1分别作两种边的边权。

  步骤:

    (1)先找出必须选的0边。(优先选1,最大生成树)

    (2)再将0边个数加至k。

    (3)补上1边。

AC Code:

 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#define MAX_N 20005 using namespace std; struct Edge
{
int sour;
int dest;
int len;
Edge(int _sour,int _dest,int _len)
{
sour=_sour;
dest=_dest;
len=_len;
}
Edge(){}
friend bool operator < (const Edge &a,const Edge &b)
{
return a.len<b.len;
}
}; int n,m,k;
int par[MAX_N];
bool failed=false;
vector<Edge> edge;
vector<Edge> est;
vector<Edge> ans; void read()
{
cin>>n>>m>>k;
int a,b,c;
for(int i=;i<m;i++)
{
cin>>a>>b>>c;
edge.push_back(Edge(a,b,c));
}
} void init_union_find()
{
for(int i=;i<=n;i++)
{
par[i]=i;
}
} int find(int x)
{
return par[x]==x?x:par[x]=find(par[x]);
} void unite(int x,int y)
{
int px=find(x);
int py=find(y);
if(px==py) return;
par[px]=py;
} bool same(int x,int y)
{
return find(x)==find(y);
} void max_tree()
{
init_union_find();
sort(edge.begin(),edge.end());
int cnt=;
for(int i=edge.size()-;i>=;i--)
{
Edge temp=edge[i];
if(!same(temp.sour,temp.dest))
{
cnt++;
unite(temp.sour,temp.dest);
if(temp.len==) est.push_back(temp);
}
}
if(cnt!=n- || est.size()>k) failed=true;
} void min_tree()
{
init_union_find();
int cnt=;
int spe=;
for(int i=;i<est.size() && spe<k;i++)
{
Edge temp=est[i];
cnt++;
spe++;
ans.push_back(temp);
unite(temp.sour,temp.dest);
}
for(int i=;i<edge.size();i++)
{
Edge temp=edge[i];
if(!same(temp.sour,temp.dest))
{
if(temp.len==)
{
if(spe<k)
{
cnt++;
spe++;
ans.push_back(temp);
unite(temp.sour,temp.dest);
}
}
else
{
cnt++;
ans.push_back(temp);
unite(temp.sour,temp.dest);
}
}
}
if(cnt!=n- || spe!=k) failed=true;
} void solve()
{
max_tree();
min_tree();
} void print()
{
if(failed)
{
cout<<"no solution"<<endl;
return;
}
for(int i=;i<ans.size();i++)
{
Edge temp=ans[i];
cout<<temp.sour<<" "<<temp.dest<<" "<<temp.len<<endl;
}
} int main()
{
read();
solve();
print();
}

BZOJ 3624 [Apio2008]免费道路:并查集 + 生成树 + 贪心【恰有k条特殊路径】的更多相关文章

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

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

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

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

  3. bzoj 3624: [Apio2008]免费道路【生成树+贪心】

    先把水泥路建生成树,然后加鹅卵石路,这里加的鹅卵石路是一定要用的(连接各个联通块),然后初始化并查集,先把必需的鹅卵石路加进去,然后随便加鹅卵石路直到k条,然后加水泥路即可. 注意判断无解 #incl ...

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

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

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

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

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

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

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

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

  9. P3623 [APIO2008]免费道路

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

随机推荐

  1. 微信小程序 - 关闭当前页面无法再通过左上角返回

    考试的时候不可能答完以后,得到成绩后再通过左上角返回再重新答吧? 可以通过:open-type='redirectTo'实现

  2. 【Python】读取cvs文件报错:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb1 in position 6: invalid start byte

    现在有文件data.csv 文件编码格式为:ANSI data.csv 1|1|1|北京市 2|1|2|天津市 3|1|3|上海市 4|1|4|重庆市 5|1|5|石家庄市 6|2|5|唐山市 7|3 ...

  3. java集合归纳

    学习自: http://android.blog.51cto.com/268543/400557/ MAP Collection 堆栈队列操作尽可能考虑 linkedlist 多线程同步操作尽可能考虑 ...

  4. difference between VM, Docker and Vagrant区别

    VM:  VirtualBox, VMware Docker Vagrant using which you can create VMs or container. It interacts wit ...

  5. wpf自定义colorpicker

    自定义colorpicker <ObjectDataProvider MethodName="GetType" ObjectType="{x:Type system ...

  6. EasyNVR无插件IPC摄像机直播方案前端构建之:区分页面是自跳转还是分享依据

    区分分享还是跳转 对于前端一些页面的展示,通常有两种方式:通过入口链接一步步进入,或是通过分享链接直接进入:对于这两种方式的区别是什么?在进行前端书写时又应该如何处理? 以EasyNVR为例来进行说明 ...

  7. CDH使用Solr实现HBase二级索引

      一.为什么要使用Solr做二级索引二.实时查询方案三.部署流程3.1 安装HBase.Solr3.2 增加HBase复制功能3.3创建相应的 SolrCloud 集合3.4 创建 Lily HBa ...

  8. 搭建SVN服务器详细教程

    搭建SVN服务器详细教程 本教程会从最基本的下载安装到上传代码,下载代码这条线来详细讲述如何完成SVN服务器的搭建 下载并安装VisualSVN server 下载并安装TortoiseSVN 导入项 ...

  9. html学习笔记(1)--处理特殊字符以及其他的一些小细节

    冬日深夜,照着一本html的书籍练习,忍不住将一些常识记下来.书名是:<HTML与CSS入门经典> by [美] Julie Meloni(这是一位女士) 1.在大多数浏览器中,<s ...

  10. Git with SVN

    1)GIT是分布式的,SVN不是: 这 是GIT和其它非分布式的版本控制系统,例如SVN,CVS等,最核心的区别.好处是跟其他同事不会有太多的冲突,自己写的代码放在自己电脑上,一段时间后再提交.合并, ...