Codeforces Round #360 (Div. 1) D 並查集判奇環

CrossDolphin發表於2016-07-06



連結:戳這裡


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;
}


相關文章