codeforces 687D Dividing Kingdom II 带权并查集(dsu)
题意:给你m条边,每条边有一个权值,每次询问只保留编号l到r的边,让你把这个图分成两部分
一个方案的耗费是当前符合条件的边的最大权值(符合条件的边指两段点都在一个部分),问你如何分,可以让耗费最小
分析:把当前l到r的边进行排序,从大到小,从大的开始不断加边,判断当前能否形成二分图,如果能形成二分图,继续加边
如果不能形成二分图,那当前边的权值就是最小耗费(是不是很眼熟)
思路很清晰,现在我们要解决的是如何判断可以形成二分图,有两种,一个是2染色当前图(肯定超时)
所以只剩一种方法,带权并查集
带权并查集三步走
1:设计权值数组relation[i],代表i节点和它的根的关系,0代表属于一个部分,1代表不属于一个部分
2:路径压缩,relation[i]=relation[i]^relation[fa[i]],递归得到和根的关系
3:合并根节点,relation[i]=relation[u]^relation[v]^1;
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=5e5+;
int fa[N],relation[N];
struct Edge{
int u,v,w,id;
bool operator<(const Edge &rhs)const{
return w>rhs.w;
}
}p[N];
int find(int x){
if(x==fa[x])return x;
int fx=find(fa[x]);
relation[x]^=relation[fa[x]];
return fa[x]=fx;
}
bool Union(int u,int v){
int fx=find(u),fy=find(v);
if(fx==fy){
if(relation[u]==relation[v])
return false;
return true;
}
fa[fx]=fy;
relation[fx]=relation[u]^relation[v]^;
return true;
}
int main(){
int n,m,q;
scanf("%d%d%d",&n,&m,&q);
for(int i=;i<=m;++i){
scanf("%d%d%d",&p[i].u,&p[i].v,&p[i].w);
p[i].id=i;
}
sort(p+,p++m);
while(q--){
int l,r,ret=-;
scanf("%d%d",&l,&r);
for(int i=;i<=n;++i)fa[i]=i,relation[i]=;
for(int i=;i<=m;++i){
if(p[i].id<l||p[i].id>r)continue;
if(!Union(p[i].u,p[i].v)){
ret=p[i].w;break;}
}
printf("%d\n",ret);
}
return ;
}
codeforces 687D Dividing Kingdom II 带权并查集(dsu)的更多相关文章
- CodeForces - 687D: Dividing Kingdom II (二分图&带权并查集)
Long time ago, there was a great kingdom and it was being ruled by The Great Arya and Pari The Great ...
- 【CF687D】Dividing Kingdom II 线段树+并查集
[CF687D]Dividing Kingdom II 题意:给你一张n个点m条边的无向图,边有边权$w_i$.有q个询问,每次给出l r,问你:如果只保留编号在[l,r]中的边,你需要将所有点分成两 ...
- Codeforces 1499G - Graph Coloring(带权并查集+欧拉回路)
Codeforces 题面传送门 & 洛谷题面传送门 一道非常神仙的题 %%%%%%%%%%%% 首先看到这样的设问,做题数量多一点的同学不难想到这个题.事实上对于此题而言,题面中那个&quo ...
- Codeforces Educational Codeforces Round 5 C. The Labyrinth 带权并查集
C. The Labyrinth 题目连接: http://www.codeforces.com/contest/616/problem/C Description You are given a r ...
- Codeforces Round #181 (Div. 2) B. Coach 带权并查集
B. Coach 题目连接: http://www.codeforces.com/contest/300/problem/A Description A programming coach has n ...
- Codeforces 1156D 带权并查集
题意:给你一颗树,树边的权值可能是0或1,问先走0边,再走1边,或者只走1边的路径有多少条? 思路:对于一个点,假设通过0边相连的点一共有x个(包括自己),通过1边相连的有y个(包括自己),那么对答案 ...
- hdu 5441 Travel 离线带权并查集
Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array 带权并查集
C. Destroying Array 题目连接: http://codeforces.com/contest/722/problem/C Description You are given an a ...
- 石头剪刀布(2019Wannafly winter camp day3 i) 带权并查集+按秩合并 好题
题目传送门 思路: 按照题意描述,所有y挑战x的关系最后会形成一棵树的结构,n个人的总方案数是 3n 种,假设一个人被挑战(主场作战)a次,挑战别人(客场)b次,那么这个人存活到最后的方案数就是3n* ...
随机推荐
- Thread的第三天学习
线程互斥 public void method1() { synchronized(this) { ... } } 等同于 public synchronized void method1() { ...
- Linqer工具
这些天写Linq挺烦人的,就上网搜搜可有什么好的sql转Linq的工具,咦,马上就看上了Linqer. 哈哈,介绍一下使用方法吧: 官方下载网站:http://sqltolinq.com/downlo ...
- 获取SqlDataReader的列名
SqlConnection thisConnection = new SqlConnection(ConfigurationManager.AppSettings["ConnectionSt ...
- .net与linux
Mono是什么? http://blog.sina.com.cn/s/blog_693ffe760100k5uh.html Mono是一个开放源代码,Linux版的Microsfot.NET开发平台 ...
- 搜索之BM25和BM25F模型
www.netfoucs.com/article/wdxin1322/94603.html#
- java中什么时候该用static修饰方法?有什么好处或者坏处?
当一个方法或者变量需要初始化加载,或者是经常被调用的时候可以加上static.用static修饰的方法可以用类名直接调用,不用的一定要先实例化一个对象然后才可以调用比如 person这个类里面有一个方 ...
- Apache HTTP Server安装
http://blog.csdn.net/wqmain/article/details/8941759 很清楚,很详细,配置连行号都有.下载的时候直接点击链接即可.
- shell编程基础(2)---&&与||
shell 编程重要的应用就是管理系统,对于管理系统中成千上万的程序而言,查询某个文件名是否存在,并且获取该文件名所指代文件基本信息是系统管理员的基本任务.shell命令可以很轻松的完成这项任务. # ...
- SimpleDateFormat日期格式化
public class T { /** * @param args */ public static void main(String[] args) { // TODO Auto-generate ...
- 加密解密(11)HMAC-在sha1,md5基础上加密
HMAC: Hash-based Message Authentication Code http://baike.sogou.com/v10977193.htm http://www.baike.c ...