题意:给你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)的更多相关文章

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

  2. 【CF687D】Dividing Kingdom II 线段树+并查集

    [CF687D]Dividing Kingdom II 题意:给你一张n个点m条边的无向图,边有边权$w_i$.有q个询问,每次给出l r,问你:如果只保留编号在[l,r]中的边,你需要将所有点分成两 ...

  3. Codeforces 1499G - Graph Coloring(带权并查集+欧拉回路)

    Codeforces 题面传送门 & 洛谷题面传送门 一道非常神仙的题 %%%%%%%%%%%% 首先看到这样的设问,做题数量多一点的同学不难想到这个题.事实上对于此题而言,题面中那个&quo ...

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

  5. Codeforces Round #181 (Div. 2) B. Coach 带权并查集

    B. Coach 题目连接: http://www.codeforces.com/contest/300/problem/A Description A programming coach has n ...

  6. Codeforces 1156D 带权并查集

    题意:给你一颗树,树边的权值可能是0或1,问先走0边,再走1边,或者只走1边的路径有多少条? 思路:对于一个点,假设通过0边相连的点一共有x个(包括自己),通过1边相连的有y个(包括自己),那么对答案 ...

  7. hdu 5441 Travel 离线带权并查集

    Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...

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

  9. 石头剪刀布(2019Wannafly winter camp day3 i) 带权并查集+按秩合并 好题

    题目传送门 思路: 按照题意描述,所有y挑战x的关系最后会形成一棵树的结构,n个人的总方案数是 3n 种,假设一个人被挑战(主场作战)a次,挑战别人(客场)b次,那么这个人存活到最后的方案数就是3n* ...

随机推荐

  1. YWE复习

    1.怎样使用C#的[状态栏][StatusStrip]控件 http://jingyan.baidu.com/article/851fbc37e7004e3e1f15ab3d.html 2.toolt ...

  2. Netty 的Downstream 和 Upstream

    Netty的Downstream 和 Upstream 如果一个event从第一个handler传递直到最后一个handler就是 Upstream 相反的如果一个event从最后一个handler传 ...

  3. 李洪强漫谈iOS开发[C语言-010] - C语言简要复习

    // //  main.m //  05 - 简要复习 // //  Created by vic fan on 16/7/13. //  Copyright © 2016年 李洪强. All rig ...

  4. Orcle数据库查询练习复习:三

    一.题目 1.与“张三”同乡的男生姓名 select * from student where snativeplace=(select snativeplace from student where ...

  5. Qt 自定义model实现文件系统的文件名排序(重定义sort函数即可。忽然开窍了:其实捕捉点击Header事件,内部重排序,全部刷新显示即可)

    前段时间,需要做一个功能是要做文件系统的排序的功能.由于是自己写的model, 自己定义的数据结构.最初的想法只有一个自己去实现文件夹跟文件名的排序算法,不过感觉比较费时间.后来想到的是QFileSy ...

  6. WCF入门(七)---自托管消费WCF服务

    费自托管WCF服务的整个过程,一步步地解释以及充足的编码和屏幕截图是非常有必要. 第1步:服务托管,现在我们需要实现的代理类客户端.创建代理的方式不同. 使用svcutil.exe,我们可以创建代理类 ...

  7. 关于Linux的时间与时区

    转:http://linux.chinaunix.net/techdoc/beginner/2007/06/22/960790.shtml 首先要说明的是我的系统是fedora,其他系统可能不完全相同 ...

  8. Python基本程序结构

    条件判断: 计算机之所以能做很多自动化的任务,因为它可以自己做条件判断.比如,输入用户年龄,根据年龄打印不同的内容,在Python程序中,用if语句实现:

  9. Tomcat启动后访问首页报错 显示JSP 空指针异常

    HTTP Status 500 - type Exception report message description The server encountered an internal error ...

  10. poj 2411 Mondriaan's Dream(状态压缩dP)

    题目:http://poj.org/problem?id=2411 Input The input contains several test cases. Each test case is mad ...