D. 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 from1 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 somex) and suffix (all roads with numbers greater than somex) of the roads so there will remain only the roads with numbersl, l + 1, ..., r - 1 andr.
After that they will divide the great kingdom into two pieces (with each city belonging to exactly one piece) such that thehardness 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 haveq guesses about the l and r chosen by those great rulers. Given these data, for each guessli andri print the minimum possible hardness of the division of the kingdom.
The first line of the input contains three integersn, m andq (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 followingm lines contains three integers ui, vi andwi (1 ≤ ui, vi ≤ n, 0 ≤ wi ≤ 109), denoting the road number i connects citiesui andvi and its length is equalwi. 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 integersli and ri (1 ≤ li ≤ ri ≤ m) — a guess from the historians about the remaining roads in the kingdom.
For each guess print the minimum possible hardness of the division in described scenario.
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
-1
33
-1
-1
33
题意:给了一些点和边,要求把点分成两部分,求最小的hardness。hardness指连接相同部分的两个结点的边长的最大值。要求询问q次,每次询问l,r之间的hardness值。
思路:可以看成二分图,然后拿并茶几写。复杂度O(mq)。好像标程O(nqlog(n))的。
从卿学姐那学习了一波并茶几的写法,做做笔记,以备以后用。
详细见代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
const int maxn = 2*1e3+100;
typedef long long LL;
struct node
{
int fr,to,val;
int id;
};
bool operator < (const node &a,const node &b) {
return a.val<b.val;
}
node A[maxn*1000];
int fa[maxn];
int n,m,q;
void init()
{
for(int i=0;i<maxn;i++)
fa[i]=i;
}
int fi(int a)
{
return a==fa[a] ? a:fa[a]=fi(fa[a]);
}
void un(int a,int b)
{
a=fi(a);
b=fi(b);
fa[a]=b;
}
int read()
{
int ans=0,flag=1;
char ch=getchar();
while(ch<'0'||ch >'9')
{
if(ch=='-') flag=-1*flag;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
ans=ans*10+ch-'0';
ch=getchar();
}
return ans*flag;
}
int main()
{
int n,m,q;
int i;
scanf("%d%d%d",&n,&m,&q);
for(i=1;i<=m;i++)
{
A[i].fr=read();
A[i].to=read();
A[i].val=read();
A[i].id=i;
}
sort(A+1,A+1+m);
while(q--)
{
init();
int l,r;
l=read();
r=read();
int ans=-1;
for(i=m;i>=1;i--)
{
if(A[i].id<l||A[i].id>r) continue;
if(fi(A[i].fr)==fi(A[i].to))
{
ans=A[i].val;
break;
}
else
{
un(A[i].fr,A[i].to+n);
un(A[i].fr+n,A[i].to);
}
}
printf("%d\n",ans);
}
return 0;
}