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 的边,若未形成一个连通块,则得到必须加入 ...
随机推荐
- SPOJ375 Query on a tree(LCT边权)
之前做了两道点权的LCT,这次做一下边权的LCT.上网找了一下资料,发现对于边权的LCT有这么两种处理方法,一种是每条边建一个点,于是边权就转成点权了.另外一种则是每个边权对应到点权上,也就是每个点对 ...
- C# Socket 入门2(转)
现在来传一个图片看看, 改改程序, 看看服务端 图片为 140K, 1.jgp 1. 服务端 1 using System; 2 using System.Collections.Generic; ...
- 546B. Soldier and Badges
题目链接 题意: n个数,要保证这n个数完全不相同,求需要把原来的数增加多少,求这个值得最小值 Java 程序 import java.io.PrintStream; import java.ut ...
- 屏幕实战效果解析:IPS/TFT/AMOLED/SLCD
现在手机市场上,智能手机种类繁多,手机屏幕材质也是五花八门.对于一般消费者来说,一款手机是否值得购买,除了关心它的硬件参数以外,更重要的一点就是看它的屏幕.除了屏幕尺寸以外,影响着大家对该手机的第一感 ...
- [iOS]SourceTree+oschina实现代码远程托管
在iOS开发, 涉及到多人协同开发的时候, 这个时候, 我们就得利用版本控制系统(例如GIT), 来合并和管理代码了, 今天我们来讲一下, 利用 SourceTree+oschina进行版本控制 先来 ...
- POJ3176——Cow Bowling(动态规划)
Cow Bowling DescriptionThe cows don't use actual bowling balls when they go bowling. They each take ...
- Lua的协程(coroutine)
-------------------------------------------------------------------------------- -- 不携带参数 ---------- ...
- 关于MySQL
DBMS - 数据库管理系统(Database Management System) RDBMS - 关系数据库管理系统(Relational Database Management System) ...
- Docker基础技术:Linux Namespace(上)
时下最热的技术莫过于Docker了,很多人都觉得Docker是个新技术,其实不然,Docker除了其编程语言用go比较新外,其实它还真不是个新东西,也就是个新瓶装旧酒的东西,所谓的The New “O ...
- C++ const && 二叉树合集
话说昨天因为校园网的问题导致现在才发博文~唉,想吐槽~ 这个是昨天写的,觉得,用来回顾还是很不错的,比较具体的都在笔记中,尤其我觉得里面经验性的东西还是不错的. 2013-8-26 今天在回顾我以前写 ...