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. These two had some problems about the numbers they like, so they decided to divide the great kingdom between themselves.
The great kingdom consisted of n cities numbered from 1 to n and m bidirectional roads between these cities, numbered from 1 to m. The i-th road had length equal to wi. The Great Arya and Pari The Great were discussing about destructing some prefix (all road with numbers less than some x) and suffix (all roads with numbers greater than some x) of the roads so there will remain only the roads with numbers l, l + 1, ..., r - 1 and r.
After that they will divide the great kingdom into two pieces (with each city belonging to exactly one piece) such that the hardness of the division is minimized. The hardness of a division is the maximum length of a road such that its both endpoints are in the same piece of the kingdom. In case there is no such road, the hardness of the division is considered to be equal to - 1.
Historians found the map of the great kingdom, and they have q guesses about the l and r chosen by those great rulers. Given these data, for each guess li and ri print the minimum possible hardness of the division of the kingdom.
Input
The first line of the input contains three integers n, m and q (1 ≤ n, q ≤ 1000, ) — the number of cities and roads in the great kingdom, and the number of guesses, respectively.
The i-th line of the following m lines contains three integers ui, vi and wi (1 ≤ ui, vi ≤ n, 0 ≤ wi ≤ 109), denoting the road number i connects cities ui and vi and its length is equal wi. It's guaranteed that no road connects the city to itself and no pair of cities is connected by more than one road.
Each of the next q lines contains a pair of integers li and ri (1 ≤ li ≤ ri ≤ m) — a guess from the historians about the remaining roads in the kingdom.
Output
For each guess print the minimum possible hardness of the division in described scenario.
Example
5 6 5
5 4 86
5 1 0
1 3 38
2 1 33
2 4 28
2 3 40
3 5
2 6
1 3
2 3
1 6
-1
33
-1
-1
33
题意:给定N点,M无向边。Q次询问,每次询问给出区间[L,R],现在需要把点集分为两个集合,求最小化集合内的最大边权。
思路:对于这个区间[L,R],从大到小处理,那么就是求二分图,如果假如长度为X的边染色失败,则答案就是X,因为是一条一条的加边,用2-sat不方便,我们用并查集来解决二分图判定。
(不过整体复杂度还是有些玄学)
1<=x<=N表示一色,N+1<=x<=N+N表示二色,4320ms:
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
struct in{
int u,v,w,id;
bool operator <(const in &x) const { return w>x.w;}
};
in a[maxn*maxn]; int fa[maxn<<],p[maxn*maxn];
int find(int x){
if(x!=fa[x]) fa[x]=find(fa[x]); return fa[x];
}
int un(int x,int y){ int fx=find(x),fy=find(y); fa[fx]=fy; }
int main()
{
int N,M,Q,L,R;
scanf("%d%d%d",&N,&M,&Q);
rep(i,,M) scanf("%d%d%d",&a[i].u,&a[i].v,&a[i].w),a[i].id=i;
sort(a+,a+M+);
rep(i,,Q){
int ans=-; scanf("%d%d",&L,&R);
rep(j,,N+N) fa[j]=j;
rep(j,,M){
if(a[j].id<L||a[j].id>R) continue;
int f1=find(a[j].u),f2=find(a[j].v);
if(f1==f2){ ans=a[j].w; break; }
else un(a[j].u+N,a[j].v),un(a[j].u,a[j].v+N);
}
printf("%d\n",ans);
}
return ;
}
用带权并查集优化,点全部在1<=x<=N范围内,所以理论上复杂度会降低一半,dis表示点到根的距离奇偶性。2932ms。
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
struct in{
int u,v,w,id;
bool operator <(const in &x) const { return w>x.w;}
};
in a[maxn*maxn]; int fa[maxn<<],dis[maxn];
int find(int x){
if(x!=fa[x]){
int fx=find(fa[x]);
dis[x]^=dis[fa[x]];
fa[x]=fx;
}
return fa[x];
}
bool Union(int x,int y){
int fx=find(x),fy=find(y);
if(fx==fy){
if(dis[x]==dis[y]) return false;
return true;
}
fa[fx]=fy; dis[fx]=dis[x]^dis[y]^;
return true;
}
int main()
{
int N,M,Q,L,R;
scanf("%d%d%d",&N,&M,&Q);
rep(i,,M) scanf("%d%d%d",&a[i].u,&a[i].v,&a[i].w),a[i].id=i;
sort(a+,a+M+);
rep(i,,Q){
int ans=-; scanf("%d%d",&L,&R);
rep(j,,N) fa[j]=j,dis[j]=;
rep(j,,M){
if(a[j].id<L||a[j].id>R) continue;
if(!Union(a[j].u,a[j].v)) { ans=a[j].w; break; }
}
printf("%d\n",ans);
}
return ;
}
CodeForces - 687D: Dividing Kingdom II (二分图&带权并查集)的更多相关文章
- codeforces 687D Dividing Kingdom II 带权并查集(dsu)
题意:给你m条边,每条边有一个权值,每次询问只保留编号l到r的边,让你把这个图分成两部分 一个方案的耗费是当前符合条件的边的最大权值(符合条件的边指两段点都在一个部分),问你如何分,可以让耗费最小 分 ...
- 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 ...
- BZOJ4025 二分图 分治 并查集 二分图 带权并查集按秩合并
原文链接http://www.cnblogs.com/zhouzhendong/p/8683831.html 题目传送门 - BZOJ4025 题意 有$n$个点,有$m$条边.有$T$个时间段.其中 ...
- CodeForces - 688C:NP-Hard Problem (二分图&带权并查集)
Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex c ...
- Codeforces 1499G - Graph Coloring(带权并查集+欧拉回路)
Codeforces 题面传送门 & 洛谷题面传送门 一道非常神仙的题 %%%%%%%%%%%% 首先看到这样的设问,做题数量多一点的同学不难想到这个题.事实上对于此题而言,题面中那个&quo ...
- Codeforces Round #181 (Div. 2) B. Coach 带权并查集
B. Coach 题目连接: http://www.codeforces.com/contest/300/problem/A Description A programming coach has n ...
- hdu 1829 &poj 2492 A Bug's Life(推断二分图、带权并查集)
A Bug's Life Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- UVA - 10004 Bicoloring(判断二分图——交叉染色法 / 带权并查集)
d.给定一个图,判断是不是二分图. s.可以交叉染色,就是二分图:否则,不是. 另外,此题中的图是强连通图,即任意两点可达,从而dfs方法从一个点出发就能遍历整个图了. 如果不能保证从一个点出发可以遍 ...
- BZOJ4025 二分图 线段树分治、带权并查集
传送门 如果边不会消失,那么显然可以带权并查集做(然后发现自己不会写带权并查集) 但是每条边有消失时间.这样每一条边产生贡献的时间对应一段区间,故对时间轴建立线段树,将每一条边扔到线段树对应的点上. ...
随机推荐
- java eclipse 监视选择指定变量
http://3y.uu456.com/bp_8tzmk3zobb7k6x46aj28_1.html 有时一个Java程序有许多变量,但你仅对其中一个或几个感兴趣,为了监视选择的变量和表达式,你可以将 ...
- substring splice
返回start到end之前 不包括end stringObject.substring(start,end) (不接受负数) stringObject.slice(start,end) start起始 ...
- 面对 to B 业务该如何构建研发管理体系?
未来离我们越来越近,而过去并未走远,我们发现科技公司2B业务兴起,腾讯认为互联网下半场属于产业互联网,需要进行一次重要的战略升级.它们在国庆节最后一天进行新一轮组织架构调整,最亮眼的就是新成立云与智慧 ...
- VC:res协议——从模块中获取资源
你可以从模块中获取一个资源.通过在文件名之前加上res://,你就可以引用一个嵌入在动态链接库资源文件中的HTML页面.
- RHEL 5 安装gcc
rpm -ivh kernel-headers... rpm -ivh glibc-headers... rpm -ivh glibc-devel... rpm -ivh libgomp.. rpm ...
- jQuery中的部分方法
1.empty() – jQuery 文档操作 从被选元素移除所有内容,包括所有文本和子节点. 用法:$(selector).empty(); 其中,selector可以是"#id" ...
- EGLImage与纹理
http://blog.csdn.net/sunnytina/article/details/51895406 Android使用Direct Textures提高glReadPixels.glTex ...
- this 机制的四种规则
江湖人称,谁调用 this,this 就指向谁. 那么 this 到底绑定或者引用的是哪个对象环境呢,以下便是四种规则 1. 默认绑定全局变量 function fn() { console.log( ...
- Windos Server 2008 Tomcat 安装
web服务:apache-tomcat-7.0.75环境:jdk-7u80-windows-i586 1.安装jdk环境包 2.配置环境变量--> 环境变量--> 新建W --> 变 ...
- CCNA 课程 四
Vlan基础: Vlan的作用:把物理上分割的用户,让他们逻辑上在一起. Vlan 范围: 0- 4095 0 4095 是保留的 不可以使用 1 cisco 本证vlan 标准vlan 1 -10 ...