题意:给你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. struts2配置文件中action的name属性

    struts2配置文件中action的name属性的第一个字符不要加斜杠 <action name="see" class="baoxiuManage_seeAct ...

  2. [2-sat]HDOJ3062 Party

    中文题 题意略 学2-sat啦啦啦 2-sat就是    矛盾的 ($x.x’$不能同时取) m对人 相互也有限制条件 取出其中n个人 也有可能是把一件东西分成 取/不取 相矛盾的两种情况 (那就要拆 ...

  3. 李洪强iOS开发之OC语言description方法和sel

    OC语言description方法和sel 一.description方法 Description方法包括类方法和对象方法.(NSObject类所包含) (一)基本知识 -description(对象 ...

  4. .Net MVC API初试

    新建.net mvc api项目后,直接运行,默认会访问http://localhost:xxxx/Home/Index页面,这个页面不是要访问的API页面. 从项目的目录可以看出,默认的API页面访 ...

  5. struts2中token防止重复提交表单

    struts2中token防止重复提交表单 >>>>>>>>>>>>>>>>>>>&g ...

  6. R: count number of distinct values in a vector

    numbers <- c(4,23,4,23,5,43,54,56,657,67,67,435,         453,435,324,34,456,56,567,65,34,435) a & ...

  7. sendmessage()模拟鼠标点击

    {鼠标软模拟:好处就是不会真的移动鼠标 开始按钮 坐标 x=386y=387 }sendmessage(hookHwnd,messages.WM_LBUTTONDOWN ,0,$0180017A); ...

  8. Oracle存储过程格式

    create or replace procedure sp_test ( -- 此地写传入的值 v_tjfs varchar2, --不用申明长度 v_kssj varchar2, v_ret ou ...

  9. ASP.NET MVC 学习1、新增Controller,了解MVC运行机制

    1,turorial ,根据链接教程新建一个MVC项目 http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/ ...

  10. Dapper使用在WCF上总是说Service找不到

    原因是用Console Application 做宿主的时候,创建的时候默认是Client Profile 4 ,坑爹啊.改成Net framework 4 即可.