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;
}
相關文章
- Codeforces Round #360 (Div. 2) D 數學題
- Codeforces Round #360 (Div. 2) C DFS判斷二分圖
- Codeforces Round #452 (Div. 2) D
- Codeforces Round 941 (Div. 2) D
- Codeforces Round 960 (Div. 2)(A - D)
- Codeforces Round 934 (Div. 1)
- Codeforces Round #325 (Div. 2) D bfs
- Codeforces Round #250 (Div. 2) A-D
- Codeforces Round #251 (Div. 2) A/B/D
- Codeforces Round #283 (Div. 2) D,E
- Codeforces Round #256 (Div. 2)A-D
- Codeforces Round 947 (Div. 1 + Div. 2)
- Codeforces Round 709 (Div. 1)
- Codeforces Round #358 (Div. 2) D dp
- Codeforces Round #359 (Div. 2) D DFS
- Educational Codeforces Round 34 (Rated for Div. 2) D
- Codeforces Round #290 (Div. 2) A,B,C,D
- Codeforces Round #263 (Div. 2) A-D
- Codeforces Round 720 (Div. 2) A-D
- Codeforces Round 963 (Div. 2) 補題記錄(A~D,F1)
- Codeforces Round 969 (Div. 1) 記錄
- Codeforces Round #844 (Div. 1 + Div. 2, based on VK Cup 2022 - Elimination Round) A-D
- Codeforces Round #673 (Div. 2)(A-D)題解
- Codeforces Round #401 (Div. 2)(C,D,E)
- Codeforces Round #321 (Div. 2) D 狀壓dp
- Codeforces Round #288 (Div. 2) A,B,C,D,E
- Codeforces Round #287 (Div. 2)A,B,C,D,E
- Codeforces Round #253 (Div. 2) D. Andrey and Problem
- Codeforces Round #280 (Div. 2 A,B,C,D,E)
- Educational Codeforces Round 170 (Rated for Div. 2) A-D
- Codeforces Round 959 sponsored by NEAR (Div. 1 + Div. 2)
- Codeforces Round 965 (Div. 2) 補題記錄(A,B,D,E1)
- 【Codeforces Round #499 (Div. 1) B】Rocket
- Educational Codeforces Round 142 (Rated for Div. 2) A-D
- Codeforces Round #336 (Div. 2) D 區間dp
- Codeforces Round 960 (Div. 2) 補題記錄(A~D)
- Codeforces Round 962 (Div. 3) D. Fun 題解
- CodeForces Round #951(Div. 2) 補題記錄(A~D)