Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 并查集求奇偶元环
2016-07-01 15:28
357 查看
[b]D. Dividing Kingdom II[/b]
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.
[b]Input[/b]
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 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.
[b]Example[/b]
[b]input[/b]
[b]output[/b]
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.
[b]Input[/b]
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 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.
[b]Example[/b]
[b]input[/b]
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
[b]output[/b]
-1 33 -1 -1 33 [b]题意:[/b] 给你一个m边n点的无向有权图,q个询问,每次询问给你l,r求序号问l到r的边构成的图的hardness值 hardness值定义为:你可以任意选图中点划分为两点集合,取一个集合中任意相连两点的边权值的最大,取两个集合的max输出 当任意集合没有相连的两点为-1 [b]题解:[/b] 画图. 奇元环必有一个非-1值 偶元环-1; 询问一次的话,按权值从大到小排序,按照并查集求奇偶环就好了 多次询问,q*m还是可以接受
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1e6+10, inf = 2e9, mod = 1e9+7; int n,m,q,fa ; struct ss{int u,v,w,id;}p ; bool cmp(ss s1,ss s2){return s1.w>s2.w;} void init(){for(int i=1;i<=n*3;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=1;i<=m;i++) { scanf("%d%d%d",&p[i].u,&p[i].v,&p[i].w); p[i].id=i; } sort(p+1,p+m+1,cmp); while(q--) { int f=0,l,r; scanf("%d%d",&l,&r); init(); for(int i=1;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=1; 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",-1); } return 0; }
相关文章推荐
- Spring 基于xml配置方式的事务
- js Array数组的使用
- 发布Android Library项目到JCenter的便捷方法
- 对比PHP对MySQL的缓冲查询和无缓冲查询
- php实现多文件上传
- sql server-Convert DateTime 格式的转换
- Java中的数组和方法
- 腾讯信鸽推送基本流程和数据的处理流程
- 数据结构之【排序】--复习
- 互联网2.0大事记
- iOS图片转base64字符串和base64字符串转为图片
- Extjs--checkbox,选中与不选中
- AngularJS内建服务$location及其功能详解
- android 极细线
- 关于java UTF-8中文与Unicode编码之间转换,以及将浏览器地址栏编码过的中文转换UTF-8形式
- 安卓4.0开发平台搭建 图文详细 教程
- Altium Designer高级功能初探之:匹配线长
- 关于常用的网页富文本编辑器
- PMI 变更管理和 WWPMM 变更管理之比较
- Android studio 集合百度推送