BZOJ 3624 免费道路
第一反应:这不先0后1做并查集就行了吗?
然后WA了。。。
哦。。。。啊?哦。。。233
如果按顺序做并查集,有些0的边可能很重要(只能由它作为0连起两个联通块),但并没有被选。
于是先按1做并查集,选出这些边,再按0,1做并查集。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxv 20050
#define maxe 100500
using namespace std;
int n,m,k,x,y,z,cnt0=,cnt1=,fath[maxv];
struct edge
{
int u,v;
}e0[maxe],e1[maxe];
bool vis[maxe],vis0[maxe],vis1[maxe];
void reset()
{
for (int i=;i<=n;i++)
fath[i]=i;
}
int getfather(int x)
{
if (x!=fath[x])
fath[x]=getfather(fath[x]);
return fath[x];
}
void focus()
{
reset();
for (int i=;i<=cnt1;i++)
{
int u=e1[i].u,v=e1[i].v;
int f1=getfather(u),f2=getfather(v);
if (f1!=f2) fath[f1]=f2;
}
for (int i=;i<=cnt0;i++)
{
int u=e0[i].u,v=e0[i].v;
int f1=getfather(u),f2=getfather(v);
if (f1!=f2) vis[i]=true;
}
}
bool kruskal()
{
reset();
int ret=;
for (int i=;i<=cnt0;i++)
{
if (vis[i])
{
int u=e0[i].u,v=e0[i].v;
int f1=getfather(u),f2=getfather(v);
if ((f1!=f2) && (ret<k))
{
ret++;vis0[i]=true;
fath[f1]=f2;
}
}
}
for (int i=;i<=cnt0;i++)
{
if (!vis[i])
{
int u=e0[i].u,v=e0[i].v;
int f1=getfather(u),f2=getfather(v);
if ((f1!=f2) && (ret<k))
{
ret++;vis0[i]=true;
fath[f1]=f2;
}
}
}
if (ret!=k) return false;
for (int i=;i<=cnt1;i++)
{
int u=e1[i].u,v=e1[i].v;
int f1=getfather(u),f2=getfather(v);
if (f1!=f2)
{
fath[f1]=f2;
ret++;vis1[i]=true;
}
}
if (ret!=n-) return false;
return true;
}
void print_e()
{
for (int i=;i<=cnt0;i++)
{
if (vis0[i])
printf("%d %d 0\n",e0[i].u,e0[i].v);
}
for (int i=;i<=cnt1;i++)
{
if (vis1[i])
printf("%d %d 1\n",e1[i].u,e1[i].v);
}
}
int main()
{
scanf("%d%d%d",&n,&m,&k);
for (int i=;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&z);
if (!z)
{
cnt0++;
e0[cnt0].u=x;e0[cnt0].v=y;
}
else
{
cnt1++;
e1[cnt1].u=x;e1[cnt1].v=y;
}
}
focus();
if (!kruskal()) printf("no solution\n");
else print_e();
return ;
}
BZOJ 3624 免费道路的更多相关文章
- Kruskal算法及其类似原理的应用——【BZOJ 3654】tree&&【BZOJ 3624】[Apio2008]免费道路
首先让我们来介绍Krukal算法,他是一种用来求解最小生成树问题的算法,首先把边按边权排序,然后贪心得从最小开始往大里取,只要那个边的两端点暂时还没有在一个联通块里,我们就把他相连,只要这个图里存在最 ...
- bzoj 3624: [Apio2008]免费道路 生成树的构造
3624: [Apio2008]免费道路 Time Limit: 2 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 111 Solved: 4 ...
- BZOJ 3624: [Apio2008]免费道路
3624: [Apio2008]免费道路 Time Limit: 2 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 1201 Solved: ...
- 免费道路 bzoj 3624
免费道路(1s 128MB)roads [输入样例] 5 7 21 3 04 5 13 2 05 3 14 3 01 2 14 2 1 [输出样例] 3 2 04 3 05 3 11 2 1 题解: ...
- [Apio2008]免费道路[Kruscal]
3624: [Apio2008]免费道路 Time Limit: 2 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 1292 Solved: ...
- P3623 [APIO2008]免费道路
3624: [Apio2008]免费道路 Time Limit: 2 Sec Memory Limit: 128 MBSec Special Judge Submit: 2143 Solved: 88 ...
- [APIO2008]免费道路
[APIO2008]免费道路 BZOJ luogu 先把必须连的鹅卵石路连上,大于k条no solution 什么样的鹅卵石路(u,v)必须连?所有水泥路都连上仍然不能使u,v连通的必须连 补全到k条 ...
- 【bzoj3624】【apio2008】免费道路
2016/06/25 诸老师讲的图论,听了这道题很想写一下,但是看来要留到期末考后了. 07/01 有的标记是说生成树,有的是并查集...然而我只是觉得这棵奇怪的生成树蛮精妙的... 题目比较难过的只 ...
- [BZOJ3624][Apio2008]免费道路
[BZOJ3624][Apio2008]免费道路 试题描述 输入 输出 输入示例 输出示例 数据规模及约定 见“输入”. 题解 第一步,先尽量加入 c = 1 的边,若未形成一个连通块,则得到必须加入 ...
随机推荐
- linux 全自动提权 exp perl脚本
linux 全自动提权 exp perl脚本 作者: admin 日期: 2013/01/19发表评论 (0) 查看评论 国外流传过来的 地址 http://dl.packetstormsecur ...
- Rake::TestTask 介绍
Rake::TestTask 介绍 通常我们创建一个新的项目的时候,会建立一个test或者spec的文件夹来存放测试的文件,运行这些测试需要单独的命令,比如在项目目录下执行rspec .或者ruby ...
- Random的用法
import java.util.Random; public class RandomTest { public static void main(String[] args) { Random r ...
- php接收数据
http://lpladdyy.blog.163.com/blog/static/133999664201010264264585/ PHP默认只识别application/x-www.form-ur ...
- Jmeter之Bean shell使用(二)
上一篇Jmeter之Bean shell使用(一)简单介绍了下Jmeter中的Bean shell,本文是对上文的一个补充,主要总结下常用的几种场景和方法,相信这些基本可以涵盖大部分的需求.本节内容如 ...
- Java:Comparator接口
public interface Comparator<T> 接口里面的方法 int compare(T o1, T o2) o1 > o2 返回 1 o1 = o2 返回 0 o1 ...
- Project Euler 107:Minimal network 最小网络
Minimal network The following undirected network consists of seven vertices and twelve edges with a ...
- JAVA字符串格式化String.format()的使用
JAVA字符串格式化-String.format()的使用常规类型的格式化 String类的format()方法用于创建格式化的字符串以及连接多个字符串对象.熟悉C语言的同学应该记得C语言的sprin ...
- JSP的设计模式
1.JSP模型1(JSP+JavaBean) JSP页面负责处理请求,并将响应发送给客户端.JSP页面文件包括:login.html.loginchk.jsp.welcome.jsp.JavaBean ...
- Line Search and Quasi-Newton Methods 线性搜索与拟牛顿法
Gradient Descent 机器学习中很多模型的参数估计都要用到优化算法,梯度下降是其中最简单也用得最多的优化算法之一.梯度下降(Gradient Descent)[3]也被称之为最快梯度(St ...