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 的边,若未形成一个连通块,则得到必须加入 ...
随机推荐
- HDU5008 Boring String Problem(后缀数组)
练习一下字符串,做一下这道题. 首先是关于一个字符串有多少不同子串的问题,串由小到大排起序来应该是按照sa[i]的顺序排出来的产生的. 好像abbacd,排序出来的后缀是这样的 1---abbacd ...
- LA 3713
The Bandulu Space Agency (BSA) has plans for the following three space missions: Mission A: Landing ...
- 真机模拟器.a文件编译报错
- Python下载Yahoo!Finance数据
Python下载Yahoo!Finance数据的三种工具: (1)yahoo-finance package. (2)ystockquote. (3)pandas.
- .NET Framework 框架简述01
NET技术可以以规范和实现两部分来划分. 规范: 公共语言架构(Common Language Infrastructure, CLI),主要包括 1.通用类型系统(Common Type S ...
- sizeof学习理解
以下内容转自: http://www.cnblogs.com/ComputerG/archive/2012/02/02/2335611.html 博问 闪存 首页 新随笔 联系 管理 随笔- 72 ...
- C# DES 加密 解密
//注意:密钥必须为8位 private const string m_strEncryptKey = "abcd1234"; /// <summary> /// 加密 ...
- 在.NET中使用Newtonsoft.Json转换,读取,写入的方法介绍
全局引用 using Newtonsoft.Json; using Newtonsoft.Json.Converters; //把Json字符串反序列化为对象 目标对象 = JavaScriptCon ...
- QT5.3无法自动调用incomingConnection函数的问题(4.7没有这个问题)
最近将qt4.7的一个工程移到5.3,遇到了几个麻烦事,主要是这个incomingConnection监听后无法自动调用的问题,在4.7上是完全没有问题的,到了5.3就不行,网上也查了下,网友们都是放 ...
- java socket实现全双工通信
java socket实现全双工通信 单工.半双工和全双工的定义 如果在通信过程的任意时刻,信息只能由一方A传到另一方B,则称为单工. 如果在任意时刻,信息既可由A传到B,又能由B传A,但只能由一个方 ...