[JZOJ3800] defeat the demon hemp sandals

topic

Topic links: https://jzoj.net/senior/#main/show/3800

Thinking

Bare title.
Binary \ (C \) , then only go right side is larger than \ (MID \) edges, each of the communication block staining. And then enumerate every inquiry, if it can not reach \ (mid \) is a legitimate program, not vice versa.
Time complexity \ (O ((n + m ) \ log maxdis) \)

Code

#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N=100010,M=600010;
int n,m,Q,maxn,tot,head[N],pos[N];

struct edge
{
    int next,to,dis;
}e[M];

struct Ask
{
    int x,y;
}ask[N];

void add(int from,int to,int dis)
{
    e[++tot].to=to;
    e[tot].dis=dis;
    e[tot].next=head[from];
    head[from]=tot;
}

void dfs(int x,int id,int mid)
{
    pos[x]=id;
    for (int i=head[x];~i;i=e[i].next)
        if (e[i].dis>mid && !pos[e[i].to])
            dfs(e[i].to,id,mid);
}

bool check(int mid)
{
    memset(pos,0,sizeof(pos)); tot=0;
    for (int i=1;i<=n;i++)
        if (!pos[i]) dfs(i,++tot,mid);
    for (int i=1;i<=Q;i++)
        if (pos[ask[i].x]==pos[ask[i].y]) return 0;
    return 1;
}

int main()
{
    memset(head,-1,sizeof(head));
    scanf("%d%d%d",&n,&m,&Q);
    for (int i=1,x,y,z;i<=m;i++)
    {
        scanf("%d%d%d",&x,&y,&z);
        add(x,y,z); add(y,x,z);
        maxn=max(maxn,z);
    }
    for (int i=1;i<=Q;i++)
    {
        scanf("%d%d",&ask[i].x,&ask[i].y);
        if (ask[i].x==ask[i].y) return !printf("-1");
    }
    int l=0,r=maxn,mid;
    while (l<=r)
    {
        mid=(l+r)>>1;
        if (check(mid)) r=mid-1;
            else l=mid+1;
    }
    printf("%d",r+1);
    return 0;
}

Guess you like

Origin www.cnblogs.com/stoorz/p/12111431.html