Codeforces Round #360 (Div. 1) D 并查集判奇环



链接:戳这里


D. Dividing Kingdom II
time limit per test6 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 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.

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


题意+思路:

(我感觉看懂题意就很简单了

给出n,m,q 表示n个城市m条无向带权边,q个询问[l,r]

选编号为[l,r]的边做成一个图

把图分成两块,就是把点分成两块

从分成的两个图中任意选取一条边权最大边,使得这个边最小。

再取两个图中的min值

好吧,我直接说清楚,就是给你一些点集,分成两部分(可以一部分的点集大小为0)

从一部分中选取两个点组成的一条边,这个边的边权要最大。但是答案是最大边权最小。

所以是尽可能的分开这些边权大的边,当出现环的时候怎么办,那就不好分了吧

但是偶数环还可以分,因为必须这条边的两个端点都在一个集合里

比如:

1-2

2-3

3-4

4-1

这是偶数环  那么我把(1,3)一组(2,4)一组,这样就没有相连的边了

但是奇数环就分不了了,所以就是找出奇数环里面最小的边权,没有输出-1

比如:

1-2

2-3

3-4

4-5

5-1

我们先把(1,3)分一组,(2,4)分一组,但是5放那一组都没用了,因为边1-5 与边 4-5冲突, 这个就是答案了

(妈的我在写代码他们居然再聊跑步!!!

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int n,m,q;
struct edge{
    int u,v,w,id;
    bool operator < (const edge &a) const{
        return w>a.w;
    }
}e[1000100];
int fa[2010];
int Find(int x){
    if(x!=fa[x])
        fa[x]=Find(fa[x]);
    return fa[x];
}
void Union(int u,int v){
    fa[Find(u)]=Find(v);
}
int main(){
    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+m+1);
    for(int i=1;i<=q;i++){
        int l,r,flag=0;
        for(int j=1;j<=2*n;j++) fa[j]=j;
        scanf("%d%d",&l,&r);
        for(int j=1;j<=m;j++){
            if(e[j].id<l || e[j].id>r) continue;
            int X=Find(e[j].u);
            int Y=Find(e[j].v);
            if(X==Y){
                printf("%d\n",e[j].w);
                flag=1;
                break;
            } else {
                Union(e[j].u,e[j].v+n);
                Union(e[j].v,e[j].u+n);
            }
        }
        if(!flag) printf("-1\n");
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值