Luo Gu -1525 imprisoned criminals

Title Description
existing two S prison city, a total of N detained criminals, were No. 1-N. Their relationship naturally very discordant. Among many criminals and even long-standing grudges, if the objective conditions of the conflict could erupt at any time. We use the "grievances value" (a positive integer value) to indicate a degree of animosity between the two criminals, the greater the resentment value, the more rancor between the two criminals. If the value of c grievances two criminals being held in the same prison, friction occurs between the two of them, and the impact force of clashes c.
End of each year, all police stations will be clashes this year in prison by influential arranged in a descending list, and then reported to the Z S City mayor there. Z-busy Mayor influence will only see the first event in the list, if the effect is bad, he will be considered to replace the chief of police.
After detailed examination of the contradictory relationship between N criminals, the police chief felt enormous pressure. He was ready to redistribution of criminals in the two prisons, in order to influence conflicts generated are small, and thus preserve their official positions. Assume that as long as there is hatred in a two criminals in the same prison, then they will be friction at some time of the year.
So, how to assign offenders to minimize the impact of conflicts of Z mayor to see? This minimum value is how much?
Input and output format
input format:
There is a space between the two numbers in each row. The first two acts of positive integers N, M, respectively, and the number of criminal offenders presence hatred logarithm. Each row M rows next three positive integers aj, bj, CJ, indicating the presence of hatred between aj and bj number criminals number, which is anger cj. Data Assurance 1 <aj≤bj≤N, 0 <cj≤1,000,000,000, and each combination offenders appear only once.
Output formats:
a total of one line, the influence of the clashes is Z mayor to see. If any conflicts occurred in prisons during the year, please output 0.

Sample Input Output
Input Sample # 1:
. 4. 6
1. 4 2534
2. 3 3512
1 28351 2
1. 3 6618
2 1805. 4
. 3. 4 12884

Output Sample # 1:
3512

DESCRIPTION
[O] Example Description anger value between the offender left as shown below, as shown in the right criminals allocation method, see the mayor conflicts influence is 3512 (No. 2 and No. 3 by the criminals trigger). Other points system will not be any better than this points system.
[Data] range
to 100% of the data have N≤20000, M≤100000

Explanation: It is clear dichotomy may be the answer, then the trouble spot is larger than the mid sentence right side can do together, where we use a weighted disjoint-set to maintain, for each point increase in a variable W, said he and relations ancestor node, if the same set is a different set is 0, then you can determine whether or not a contradiction.

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#define N 20003
#define M 100005
using namespace std;
struct edge{
    int a,b,c;
};
inline int read()
{
    int p=0,f=1;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){p=p*10+c-'0';c=getchar();}
    return f*p;}
bool cmp(edge &x,edge &y){
    return x.c>y.c;
}
edge E[M];
int n=0,m=0;
int father[N]={0};
bool W[N]={0};
void init(){
    for(int i=0;i<N;i++) W[i]=1,father[i]=i;
}
int find(int x){
    if(father[x]==x) return x;
    int fa=father[x];
    int temp=find(fa);
    W[x]=!(W[x]^W[fa]);
    return father[x]=temp;
}
void merge(int x,int y){
    int wx=W[x],wy=W[y];
    int fax=find(x),fay=find(y);
    father[fax]=fay;
    W[fax]=wx^wy;
}
bool same(int x,int y){
    return find(x)==find(y);
}
bool ok(int x){
    init();
    for(int i=1;i<x;i++){
        if(same(E[i].a,E[i].b)&&W[E[i].a]==W[E[i].b]) return 0;
        else merge(E[i].a,E[i].b);
    }
    return 1;
}
int main(){
    ios::sync_with_stdio(false);
    n=read();m=read();
    for(int i=1;i<=m;i++){
        E[i].a=read();
        E[i].b=read();
        E[i].c=read();
    }
    sort(E+1,E+1+m,cmp);
    int l=1,r=m+1;
    int mid=0;
    while(l<r){
        mid=(l+r+1)/2;
        if(ok(mid)) l=mid;
        else r=mid-1;
    }
    cout<<E[l].c<<endl;
    return 0;
}


Guess you like

Origin blog.csdn.net/mkopvec/article/details/91814748