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 二分图 线段树分治、带权并查集
传送门 如果边不会消失,那么显然可以带权并查集做(然后发现自己不会写带权并查集) 但是每条边有消失时间.这样每一条边产生贡献的时间对应一段区间,故对时间轴建立线段树,将每一条边扔到线段树对应的点上. ...
随机推荐
- 十位用户唯一ID生成策略
新浪微博和twitter 等系统都有一窜数字ID来标示一个唯一的用户,这篇文章就是记录如何实现这种唯一数字ID 原理:使用MYSQL 自增ID 拼接任意字符..然后使用进制转换打乱规则 一般来说实现唯 ...
- Python学习进程(2)Python环境的搭建
本节主要介绍在windows和Linux平台上如何搭建Python编程环境. (1)查看Python版本: windows: C:\Users\JMSun>python 'pyt ...
- Struts2笔记04——Hello World Example(转)
原文地址:https://www.tutorialspoint.com/struts_2/ [注释]项目结构,次序估计有误 通过学习Struts2的架构,我们可以知道,在Struts2 web应用中, ...
- 跨平台移动开发_Windows 8平台使用 PhoneGap 方法
原文地址: Using PhoneGap in Windows 8 Store Applications 下载phonegap 2.9.1 下载地址: https://codeload.github. ...
- java实现文件的压缩和解压
java实现文件的压缩和解压 代码压缩实现 package com.hjh.demo.zip; import java.io.BufferedInputStream; import java.io.F ...
- ==、equals与hashCode
== 首先,得说明java数据类型分为基本数据类型和引用数据类型, 基本数据类型有8种: 浮点型:float(4 byte), double(8 byte) 整型:byte(1 byte), sho ...
- 8.14比赛j题 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87813#overview
就我个人来说我觉得这道题其实不用写题解,只是因为做的时候错了一次,如果不是队友细心,我根本会错下去,所以我感觉自己必须强大#include<stdio.h> #include<str ...
- 【转】Android中的IOC框架,完全注解方式就可以进行UI绑定和事件绑定
转载请注明出处:http://blog.csdn.net/blog_wang/article/details/38468547 相信很多使用过Afinal和Xutils的朋友会发现框架中自带View控 ...
- PAT1028. List Sorting (25)
id用int,避免了id的strcmp,不然用string就超时. #include <iostream> #include <vector> #include <alg ...
- Vue 什么是组件
Vue.js组件 组件的作用:组件是自定义元素,可扩展html元素,封装可复用的代码. 组件的注册一定要在初始化根实例之前,负责组件是不起作用的. 全局组件在初始化实例的时候被使用 局部组件仅在实例/ ...