CodeForces - 687D: 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
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
Input5 6 5Output
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
題意:給定N點,M無向邊。Q次詢問,每次詢問給出區間[L,R],現在需要把點集分為兩個集合,求最小化集合內的最大邊權。
思路:對於這個區間[L,R],從大到小處理,那麽就是求二分圖,如果假如長度為X的邊染色失敗,則答案就是X,因為是一條一條的加邊,用2-sat不方便,我們用並查集來解決二分圖判定。
(不過整體復雜度還是有些玄學)
1<=x<=N表示一色,N+1<=x<=N+N表示二色,4320ms:
#include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<=b;i++) using namespace std; const int maxn=1010; struct in{ int u,v,w,id; bool operator <(const in &x) const { return w>x.w;} }; in a[maxn*maxn]; int fa[maxn<<1],p[maxn*maxn]; int find(int x){ if(x!=fa[x]) fa[x]=find(fa[x]); return fa[x]; } int un(int x,int y){ int fx=find(x),fy=find(y); fa[fx]=fy; } int main() { int N,M,Q,L,R; scanf("%d%d%d",&N,&M,&Q); rep(i,1,M) scanf("%d%d%d",&a[i].u,&a[i].v,&a[i].w),a[i].id=i; sort(a+1,a+M+1); rep(i,1,Q){ int ans=-1; scanf("%d%d",&L,&R); rep(j,1,N+N) fa[j]=j; rep(j,1,M){ if(a[j].id<L||a[j].id>R) continue; int f1=find(a[j].u),f2=find(a[j].v); if(f1==f2){ ans=a[j].w; break; } else un(a[j].u+N,a[j].v),un(a[j].u,a[j].v+N); } printf("%d\n",ans); } return 0; }
用帶權並查集優化,點全部在1<=x<=N範圍內,所以理論上復雜度會降低一半,dis表示點到根的距離奇偶性。2932ms。
#include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<=b;i++) using namespace std; const int maxn=1010; struct in{ int u,v,w,id; bool operator <(const in &x) const { return w>x.w;} }; in a[maxn*maxn]; int fa[maxn<<1],dis[maxn]; int find(int x){ if(x!=fa[x]){ int fx=find(fa[x]); dis[x]^=dis[fa[x]]; fa[x]=fx; } return fa[x]; } bool Union(int x,int y){ int fx=find(x),fy=find(y); if(fx==fy){ if(dis[x]==dis[y]) return false; return true; } fa[fx]=fy; dis[fx]=dis[x]^dis[y]^1; return true; } int main() { int N,M,Q,L,R; scanf("%d%d%d",&N,&M,&Q); rep(i,1,M) scanf("%d%d%d",&a[i].u,&a[i].v,&a[i].w),a[i].id=i; sort(a+1,a+M+1); rep(i,1,Q){ int ans=-1; scanf("%d%d",&L,&R); rep(j,1,N) fa[j]=j,dis[j]=0; rep(j,1,M){ if(a[j].id<L||a[j].id>R) continue; if(!Union(a[j].u,a[j].v)) { ans=a[j].w; break; } } printf("%d\n",ans); } return 0; }
CodeForces - 687D: Dividing Kingdom II (二分圖&帶權並查集)