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

题意:给你一个n个点,m条边的图,q次询问,每次询问给出[L,R],用在[L,R]以内的边构成一个图,然后把这个图的点集分成两个部分,求连接同一个部分的点的最长边最小是多小

思路:直接mq暴力+并查集....


#include<bits\stdc++.h>
using namespace std;
const int maxn = 1005;
struct Node
{
	int u,v,w,id;
}e[maxn*maxn];
int fa[maxn<<1];
bool cmp(Node a,Node b)
{
	return a.w>b.w;
}
int Find(int x)
{
	return x==fa[x]?x:fa[x]=Find(fa[x]);
}
void uni(int u,int v)
{
	int uu = Find(u);
	int vv = Find(v);
	if(uu!=vv)
		fa[uu]=vv;
}
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].u,&e[i].v,&e[i].w);
		 e[i].id = i;
	}
	sort(e+1,e+1+m,cmp);
    while(q--)
	{
		for(int i = 0;i<=2*n;i++)
			fa[i]=i;
		int l,r,flag=0;
		scanf("%d%d",&l,&r);
        for(int i = 1;i<=m;i++)
		{
			if(e[i].id>=l && e[i].id<=r)
			{
				if(Find(e[i].u)==Find(e[i].v))
				{
					printf("%d\n",e[i].w);
					flag = 1;
					break;
				}
				else
				{
					uni(e[i].u+n,e[i].v);
					uni(e[i].u,e[i].v+n);
				}
            }
		}
		if(!flag)
			printf("-1\n");
	}
}


D. Dividing Kingdom II
time limit per test
6 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值