Codeforces Round #360 (Div. 1) 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.
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.
For each guess print the minimum possible hardness of the division in described scenario.
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
-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 并查集求奇偶元环的更多相关文章
- 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 ...
- Codeforces Round #181 (Div. 2) B. Coach 带权并查集
B. Coach 题目连接: http://www.codeforces.com/contest/300/problem/A Description A programming coach has n ...
- Codeforces Round #345 (Div. 1) C. Table Compression dp+并查集
题目链接: http://codeforces.com/problemset/problem/650/C C. Table Compression time limit per test4 secon ...
- 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 ...
- Codeforces Round #375 (Div. 2) D. Lakes in Berland 并查集
http://codeforces.com/contest/723/problem/D 这题是只能把小河填了,题目那里有写,其实如果读懂题这题是挺简单的,预处理出每一块的大小,排好序,从小到大填就行了 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 详细解析Java中抽象类和接口的区别
在Java语言中, abstract class 和interface 是支持抽象类定 义的两种机制.正是由于这两种机制的存在,才赋予了Java强大的 面向对象能力.abstract class和in ...
- javascript中parentNode,childNodes,children的应用详解
本篇文章是对javascript中parentNode,childNodes,children的应用进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助 "parentNode&qu ...
- require.async换这个方法的transport问题
这个方法是用于在模块中异步加载其他模块的,类似于在页面上的seajs.use. 比如需要在特定条件下才去加载a模块,不必每次都加载,类似于下面这样的代码 if({{some_condition}}){ ...
- .net环境下从PDF文档中抽取Text文本的一些方法汇总
1.PDFBox的IKVM版本:据我所知,目前只有PDFBox的IKVM版本能比较好地从PDF中提取文本,PDFBOX更多信息请访问http://www.pdbox.org,关于其应用实例,可以参考C ...
- linux服务器部署svn常见错误处理→转载
转载地址→http://blog.seweal.com/post/2013-02-04/svn-errors [开放svn端口] iptables -I INPUT -p tcp --dport 36 ...
- Power Network(网络流最大流 & dinic算法 + 优化)
Power Network Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 24019 Accepted: 12540 D ...
- Robberies
Robberies Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit S ...
- 复制”链接文件“到虚拟机(VirtualBox)的”共享文件夹“时报错:创建符号链接时报错:只读文件系统
问题描述: 1.Ubuntu 中的 /www/目录,是宿主主机 Windows 7 以“共享文件夹”的形式挂载的: 2./etc/php.ini 是 /opt/software/php/etc/php ...
- 将DataSet(DataTable)转换成JSON格式(生成JS文件存储)
public static string CreateJsonParameters(DataTable dt) { /**/ /**/ /**/ /* /*********************** ...
- Unity3D研究之Prefab里面的Prefab关联问题
Unity研究院之Prefab和GameObject的正向和逆向查找引用 我发现很多美工兄弟都爱问程序Unity3d为什么总丢材质? 我不排除U3d有BUG的情况下会丢材质?但是其实很多时候是人为操作 ...