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. Python框架之Tornado(概述)

    本系列博文计划: 1.剖析基于Python的Web框架Tornado的源码,为何要阅读源码? Tornado 由前 google 员工开发,代码非常精练,实现也很轻巧,加上清晰的注释和丰富的 demo ...

  2. 缓存:Memcached Redis

    一.Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的 ...

  3. POJ 3468 A Simple Problem with Integers 【线段树】

    题目链接 http://poj.org/problem?id=3468 思路 线段树 区间更新 模板题 在赋初始值的时候,按点更新区间就可以 AC代码 #include <cstdio> ...

  4. Safari通过JavaScript获取系统语言

      IE6 IE7 IE8 Firefox Chrome Safari Opera navigator.language undefined zh-CN zh-CN navigator.userLan ...

  5. 【leetcode刷题笔记】Text Justification

    Given an array of words and a length L, format the text such that each line has exactly L characters ...

  6. Apollo和分布式配置

    传统配置文件有什么缺点 如果修改了配置文件,需要重新打包发布,而且每个环境变量配置文件复杂. 分布式配置中心 将配置文件注册到配置中心平台上,可以使用分布式配置中心实时更新配置文件,统一管理,不需要重 ...

  7. java 项目中每个jar包的作用总结

    别人的总结 1.Struts2 1)commons-fileupload :2)common-io:文件上传 3)commons-lang:它扩展了标准 java.langAPI ArrayUtils ...

  8. [转载]spring security 的 logout 功能

    原文地址:security 的 logout 功能">spring security 的 logout 功能作者:sumnny 转载自:http://lengyun3566.iteye ...

  9. PAT1021. Deepest Root (25)

    之前不知道怎么判断是不是树,参考了 http://blog.csdn.net/eli850934234/article/details/8926263 但是最后有一个测试点有超时,在bfs里我用了数组 ...

  10. Spring初学之spring的事务管理注解

    spring的事务管理,本文的例子是:比如你需要网购一本书,卖书的那一方有库存量以及书的价格,你有账户余额.回想我们在编程中要实现买书这样的功能,由于你的账户表和书的库存量表肯定不是同一张数据库表,所 ...