D. 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 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 iconnects 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 题意:
  给你一个m边n点的无向有权图,q个询问,每次询问给你l,r求序号问l到r的边构成的图的hardness值
  hardness值定义为:你可以任意选图中点划分为两点集合,取一个集合中任意相连两点的边权值的最大,取两个集合的max输出
  当任意集合没有相连的两点为-1
题解:
  画图.
  奇元环必有一个非-1值
  偶元环-1;
  询问一次的话,按权值从大到小排序,按照并查集求奇偶环就好了
  多次询问,q*m还是可以接受   
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6+, inf = 2e9, mod = 1e9+; int n,m,q,fa[N];
struct ss{int u,v,w,id;}p[N];
bool cmp(ss s1,ss s2){return s1.w>s2.w;}
void init(){for(int i=;i<=n*;i++)fa[i]=i;}
int finds(int x) {return x==fa[x]?x:fa[x]=finds(fa[x]);} int main()
{
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+,cmp);
while(q--)
{
int f=,l,r;
scanf("%d%d",&l,&r);
init();
for(int i=;i<=m;i++)
{
if(p[i].id>=l&&p[i].id<=r)
{
int fx=finds(p[i].u),fy=finds(p[i].v);
if(fx==fy)
{
f=;
printf("%d\n",p[i].w);
break;
}
else
{
fa[fx]=finds(p[i].v+n);
fa[fy)]=finds(p[i].u+n);
}
}
}
if(!f) printf("%d\n",-);
}
return ;
}

Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 并查集求奇偶元环的更多相关文章

  1. Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 暴力并查集

    D. Dividing Kingdom II 题目连接: http://www.codeforces.com/contest/687/problem/D Description Long time a ...

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

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

  3. Codeforces Round #345 (Div. 1) C. Table Compression dp+并查集

    题目链接: http://codeforces.com/problemset/problem/650/C C. Table Compression time limit per test4 secon ...

  4. Codeforces Round #346 (Div. 2) F. Polycarp and Hay 并查集 bfs

    F. Polycarp and Hay 题目连接: http://www.codeforces.com/contest/659/problem/F Description The farmer Pol ...

  5. Codeforces Round #375 (Div. 2) D. Lakes in Berland 并查集

    http://codeforces.com/contest/723/problem/D 这题是只能把小河填了,题目那里有写,其实如果读懂题这题是挺简单的,预处理出每一块的大小,排好序,从小到大填就行了 ...

  6. Codeforces Round #363 (Div. 2) D. Fix a Tree —— 并查集

    题目链接:http://codeforces.com/contest/699/problem/D D. Fix a Tree time limit per test 2 seconds memory ...

  7. Codeforces Round #603 (Div. 2) D. Secret Passwords(并查集)

    链接: https://codeforces.com/contest/1263/problem/D 题意: One unknown hacker wants to get the admin's pa ...

  8. Codeforces Round #346 (Div. 2) F. Polycarp and Hay 并查集

    题目链接: 题目 F. Polycarp and Hay time limit per test: 4 seconds memory limit per test: 512 megabytes inp ...

  9. codeforces Codeforces Round #345 (Div. 1) C. Table Compression 排序+并查集

    C. Table Compression Little Petya is now fond of data compression algorithms. He has already studied ...

随机推荐

  1. MySql 创建只读账号

    GRANT Select ON *.* TO reader@192.168.1.123  IDENTIFIED BY "123456" GRANT  可以立刻生效 在mysql 5 ...

  2. (转)win32Application和win32ApplicationConsole

    这几天在创建MFC项目时,常常遇到一下两个连接错误,例如: 1. LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _ma ...

  3. CSS - 如何实现强制不换行、自动换行、强制换行 以及 chrom默认焦点 IE下 Input 默认出现叉

    *:focus {outline: none;} input::-ms-clear {display:none;} 一般的文字截断(适用于内联与块): .text-overflow { display ...

  4. 解决win8下chrome浏览器打开提示没有注册类的方法

    今天又把win8装回来了,继续装了个chrome浏览器,但是发现只能从安装的文件打开,从快捷方式或者快速启动栏都会提示没有注册类.找到一种解决的办法是删除注册表中的相关键值,具体如下: 1.打开注册表 ...

  5. NGUI之scroll view制作,以及踩的坑总结

    http://blog.csdn.net/monzart7an/article/details/23878505 链接: http://game.ceeger.com/forum/read.php?t ...

  6. HDU3345广搜 (P,E,T,#)

    War chess is hh's favorite game:In this game, there is an N * M battle map, and every player has his ...

  7. set_include_path详细解释

    zendframework的示例index.php里有这样一句 set_include_path('.' . PATH_SEPARATOR . '../library/'. PATH_SEPARATO ...

  8. 【Hibernate】Hibernate系列4之配置文件详解

    映射文件详解 4.1.概述 4.2.主键生成策略 4.3.属性配置 准确映射: 4.4.映射组成关系 4.5.单向多对一映射 4.6.双向多对一关系 4.7.一对一关联关系-基于外键映射 一对一联合m ...

  9. 【转】mybatis实战教程(mybatis in action)之八:mybatis 动态sql语句

    转自:除非申明,文章均为一号门原创,转载请注明本文地址,谢谢! 转载地址:http://blog.csdn.net/kutejava/article/details/9164353#t5 1. if ...

  10. 27.二元树的深度[BinaryTreeDepth]

    [题目] 输入一棵二元树的根结点,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度. 例如 10                          ...