Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 暴力并查集
D. Dividing Kingdom II
题目连接:
http://www.codeforces.com/contest/687/problem/D
Description
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.
Sample 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
Sample Output
-1
33
-1
-1
33
Hint
题意
给你一个图
然后给你n,m,q
表示这个图有n个点,m条边,一共q次询问
每次询问给你l,r
然后用l,r以内的边,去构成的一个图集合。
把这个集合的点分成两个部分,然后这个图的hardness,定义为这个图里面的最长边,且这个边左右连的城市相同
你想让这个边的大小尽量小,问是多少。
题解
如果只有一个询问的话,很容易想到是把边从大到小排序,然后贪心去扔到并查集看一看就好了,这样就是一个傻逼题。
然后他有Q次询问,感觉上来说好像是个莫队。
但是CF机器很快,所以就没必要写莫队了,直接NQ暴力去写这个就好了。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e3+6;
struct node
{
int a,b,c,d;
}e[maxn*maxn];
bool cmp(node a,node b){
return a.c>b.c;
}
int fa[maxn*2];
int fi(int x){
if(fa[x]==x)return x;
return fa[x]=fi(fa[x]);
}
int uni(int x,int y){
fa[fi(x)]=fi(y);
}
int main(){
int n,m,q;
scanf("%d%d%d",&n,&m,&q);
for(int i=1;i<=m;i++){
scanf("%d%d%d",&e[i].a,&e[i].b,&e[i].c);
e[i].d=i;
}
sort(e+1,e+1+m,cmp);
while(q--){
int l,r;
scanf("%d%d",&l,&r);
for(int i=1;i<=2*n;i++)
fa[i]=i;
int flag=0;
for(int i=1;i<=m;i++){
if(e[i].d<=r&&e[i].d>=l){
if(fi(e[i].a)==fi(e[i].b)){
flag=1;
printf("%d\n",e[i].c);
break;
}
else{
uni(e[i].a,e[i].b+n);
uni(e[i].b,e[i].a+n);
}
}
}
if(!flag)printf("-1\n");
}
}
Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 暴力并查集的更多相关文章
- Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 并查集求奇偶元环
D. Dividing Kingdom II Long time ago, there was a great kingdom and it was being ruled by The Grea ...
- Codeforces Round #396 (Div. 2) D. Mahmoud and a Dictionary 并查集
D. Mahmoud and a Dictionary 题目连接: http://codeforces.com/contest/766/problem/D Description Mahmoud wa ...
- Codeforces Round #250 (Div. 1) B. The Child and Zoo 并查集
B. The Child and Zoo Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/438/ ...
- Codeforces Round #212 (Div. 2) D. Fools and Foolproof Roads 并查集+优先队列
D. Fools and Foolproof Roads You must have heard all about the Foolland on your Geography lessons. ...
- Codeforces Round #376 (Div. 2) A B C 水 模拟 并查集
A. Night at the Museum time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Codeforces Round #254 (Div. 2) B. DZY Loves Chemistry (并查集)
题目链接 昨天晚上没有做出来,刚看题目的时候还把题意理解错了,当时想着以什么样的顺序倒,想着就饶进去了, 也被题目下面的示例分析给误导了. 题意: 有1-n种化学药剂 总共有m对试剂能反应,按不同的 ...
- Codeforces Round #260 (Div. 1) C. Civilization 树的中心+并查集
题目链接: 题目 C. Civilization time limit per test1 second memory limit per test256 megabytes inputstandar ...
- Codeforces Round #385 (Div. 2)A B C 模拟 水 并查集
A. Hongcow Learns the Cyclic Shift time limit per test 2 seconds memory limit per test 256 megabytes ...
- Codeforces Round #250 (Div. 2) D. The Child and Zoo 并查集
D. The Child and Zoo time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
随机推荐
- python基础之命名空间
前言 命名空间通俗的理解就是对象或变量的作用范围,在python中分为局部命令空间.模块命名空间和build-in全局命名空间. 局部命名空间 局部命名空间即在一个函数或一个类中起作用的变量或引用的字 ...
- Linux 2440 LCD 控制器【转】
转自:http://www.cnblogs.com/armlinux/archive/2011/01/14/2396864.html 嵌入式Linux之我行,主要讲述和总结了本人在学习嵌入式linux ...
- python基础-类的属性(类属性,实例属性,私有属性)
一:类的属性 类的属性分为:类属性(公有属性),实例属性和私有属性. 1)类属性(公有属性(静态字段): 类定义时直接指定的属性(不是在__init__方法中),可以通过类名直接访问属性,并且保存 ...
- MYSQL-重做系统恢复MYSQL过程
记笔记是好习惯,记笔记是好习惯,记笔记是好习惯! 重要的事情说三遍. 说多了都是泪.第一次装MYSQL时候就遇到了很多问题,当时解决了忘记记录了.家里硬盘满了,于是买了个4T的硬盘重装系统.重装系统后 ...
- No.1 selenium学习之路之浏览器操作
selenium基础,首先就是浏览器的相关操作 下面描述几种浏览器的常用操作 1.打开浏览器 webdriver后面添加想要打开的浏览器 Ie或者Chrome 2.打开指定页面(百度) 3.休眠时间 ...
- OpenCV与Python之图像的读入与显示以及利用Numpy的图像转换
1:读入图像,显示图像与保存图像 代码: import cv2 img=cv2.imread('lena.jpg',cv2.IMREAD_COLOR) cv2.namedWindow('lena',c ...
- thinkphp5 IIS7.5 隐藏index.php的方法
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.we ...
- javaweb笔记一
内连接(自然连接): 只有两个表相匹配的行才能在结果集中出现 外连接: 包括 (1)左外连接(左边的表不加限制) (2)右外连接(右边的表不加限制) (3)全外连接(左右两表都不加限制 一个空的构造器 ...
- kafka基本版与kafka acl版性能对比(单机版)
一.场景 线上已经有kafka集群,服务运行稳定.但是因为产品升级,需要对kakfa做安全测试,也就是权限验证. 但是增加权限验证,会不会对性能有影响呢?影响大吗?不知道呀! 因此,本文就此来做一下对 ...
- java小爬虫
爬取煎蛋网 1.找出页面网址的规律 2.设计页面图片网址的正则 代码: import java.io.BufferedInputStream; import java.io.BufferedOutpu ...