bzoj3732 Network

Network

Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 3399 Solved: 1671
[Submit][Status][Discuss]
Description

给你N个点的无向图 (1 <= N <= 15,000),记为:1…N。
图中有M条边 (1 <= M <= 30,000) ,第j条边的长度为: d_j ( 1 < = d_j < = 1,000,000,000).
现在有 K个询问 (1 < = K < = 20,000)。
每个询问的格式是:A B,表示询问从A点走到B点的所有路径中,最长的边最小值是多少?

Input

第一行: N, M, K。
第2..M+1行: 三个正整数:X, Y, and D (1 <= X <=N; 1 <= Y <= N). 表示X与Y之间有一条长度为D的边。
第M+2..M+K+1行: 每行两个整数A B,表示询问从A点走到B点的所有路径中,最长的边最小值是多少?

Output

对每个询问,输出最长的边最小值是多少。

Sample Input

6 6 8
1 2 5
2 3 4
3 4 3
1 4 8
2 5 7
4 6 2
1 2
1 3
1 4
2 3
2 4
5 1
6 2
6 1
Sample Output

5
5
5
4
4
7
4
5
HINT
1 <= N <= 15,000
1 <= M <= 30,000
1 <= d_j <= 1,000,000,000
1 <= K <= 15,000

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define M 15100
using namespace std;
struct abcd{
    int x,y,f;
    bool operator < (const abcd &Y) const
    {
        return f < Y.f ;
    }
}edges[M<<1];
int n,m,k;
int belong[M],fa[M],size[M],dis[M],dpt[M];
int Find(int x)
{
    if(!belong[x])
        belong[x]=x,size[x]=1;
    if(belong[x]==x)
        return x;
    return belong[x]=Find(belong[x]);
}
int Get_Depth(int x)
{
    if(dpt[x]) return dpt[x];
    if(!fa[x]) return dpt[x]=1;
    return dpt[x]=Get_Depth(fa[x])+1;
}
void Kruskal()
{
    int i;
    sort(edges+1,edges+m+1);
    for(i=1;i<=m;i++)
    {
        int x=Find(edges[i].x);
        int y=Find(edges[i].y);
        if(x==y) continue ;
        if(size[x]>size[y])
            swap(x,y);
        belong[x]=y;
        size[y]=max(size[y],size[x]+1);
        fa[x]=y;
        dis[x]=edges[i].f;
    }
}
int Query(int x,int y)
{
    if(dpt[x]<dpt[y])
        swap(x,y);
    while(dpt[fa[x]]>dpt[y])
        x=fa[x];
    if(fa[x]==y)
        return dis[x];
    if(dpt[x]!=dpt[y])
        x=fa[x];
    while(fa[x]!=fa[y])
        x=fa[x],y=fa[y];
    return max(dis[x],dis[y]);
}
inline char Get_Char()
{
    static const int L=1<<15;
    static char buffer[L];
    static char *S,*T;
    if(S==T)
    {
        T=(S=buffer)+fread(buffer,1,L,stdin);
        if(S==T) return EOF;
    }
    return *S++;
}
inline int Get_Int()
{
    char c=Get_Char();
    while(c<'0'||c>'9')
        c=Get_Char();
    int re=0;
    while(c>='0'&&c<='9')
        re=(re<<3)+(re<<1)+(c-'0'),c=Get_Char();
    return re;
}
int main()
{
    int i,x,y;
    cin>>n>>m>>k;
    for(i=1;i<=m;i++)
    {
        edges[i].x=Get_Int();
        edges[i].y=Get_Int();
        edges[i].f=Get_Int();
    }
    Kruskal();
    for(i=1;i<=n;i++)
        Get_Depth(i);
    for(i=1;i<=k;i++)
    {
        x=Get_Int();y=Get_Int();
        printf("%d\n", Query(x,y) );
    }
}

  

posted @ 2019-10-24 22:01  我微笑不代表我快乐  阅读(112)  评论(0编辑  收藏  举报