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 nm 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

Input
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
Output
-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 (二分图&带权并查集)的更多相关文章

  1. codeforces 687D Dividing Kingdom II 带权并查集(dsu)

    题意:给你m条边,每条边有一个权值,每次询问只保留编号l到r的边,让你把这个图分成两部分 一个方案的耗费是当前符合条件的边的最大权值(符合条件的边指两段点都在一个部分),问你如何分,可以让耗费最小 分 ...

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

  3. BZOJ4025 二分图 分治 并查集 二分图 带权并查集按秩合并

    原文链接http://www.cnblogs.com/zhouzhendong/p/8683831.html 题目传送门 - BZOJ4025 题意 有$n$个点,有$m$条边.有$T$个时间段.其中 ...

  4. CodeForces - 688C:NP-Hard Problem (二分图&带权并查集)

    Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex c ...

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

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

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

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

  7. hdu 1829 &amp;poj 2492 A Bug&#39;s Life(推断二分图、带权并查集)

    A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  8. UVA - 10004 Bicoloring(判断二分图——交叉染色法 / 带权并查集)

    d.给定一个图,判断是不是二分图. s.可以交叉染色,就是二分图:否则,不是. 另外,此题中的图是强连通图,即任意两点可达,从而dfs方法从一个点出发就能遍历整个图了. 如果不能保证从一个点出发可以遍 ...

  9. BZOJ4025 二分图 线段树分治、带权并查集

    传送门 如果边不会消失,那么显然可以带权并查集做(然后发现自己不会写带权并查集) 但是每条边有消失时间.这样每一条边产生贡献的时间对应一段区间,故对时间轴建立线段树,将每一条边扔到线段树对应的点上. ...

随机推荐

  1. oracle数据向历史表数据迁移————procedure

    create or replace procedure remove_refund_his_pro isbegin declare cursor refund_query_cur is select ...

  2. Yii2.0数据库查询实例(三)

    常用查询: // WHERE admin_id >= 10 LIMIT 0,10 User::find()->])->offset()->limit()->all() / ...

  3. 【FAQ系列】Relay log 导致复制启动失败

    今天在使用冷备份文件重做从库时遇到一个报错,值得研究一下. 版本:MySQL5.6.27 一.报错现象 dba:(none)> start slave; ERROR (HY000): Slave ...

  4. 常用模块-----configparser & subprocess

    configparser 模块 功能:操作模块类的文件,configparser类型文件的操作类似于字典,大多数用法和字典相同. 新建文件: import configparser cfg=confi ...

  5. DNS 缓存机制原理

    DNS 缓存机制原理 简单来说,一条域名的DNS记录会在本地有两种缓存:浏览器缓存和操作系统(OS)缓存.在浏览器中访问的时候,会优先访问浏览器缓存, 如果未命中则访问OS缓存,最后再访问DNS服务器 ...

  6. Go HelloWorld 网络版和并发版

    网络版 package main import ( "net/http" "fmt" ) func main() { http.HandleFunc(" ...

  7. JMeter学习(九)分布式部署

    Jmeter 是java 应用,对于CPU和内存的消耗比较大,因此,当需要模拟数以千计的并发用户时,使用单台机器模拟所有的并发用户就有些力不从心,甚至会引起JAVA内存溢出错误.为了让jmeter工具 ...

  8. win7 加载 usb3.0驱动

    1.去微软官网下一个 usb3.0 驱动 https://downloadcenter.intel.com/zh-cn/download/26254/-NUC-NUC6i7kyk-USB3-0-Win ...

  9. 如何在myEclipse中创建配置文件,比如:XXX.properties

    myEclipse是没有直接生成配置文件的方法,除非去配置某些插件. 目前通用的方法是:随便新建一个文件(比如:XXX.xml),然后对该文件重命名,改成XXX.properties即可. 很简单有没 ...

  10. 【jsoi】第一季 [略]精简题解

    UPD:好像有两道题的代码逃跑了?= =就先不找了,反正都是水题. 精简题解系列第四弹.(其实也不是那么精简啊= =) [JSOI2008]最大数maxnumber 单点修改,区间最大值查询,裸线段树 ...