UVA 11354 Bond (MST + LCA)

求出最小生成树, 然后在最小生成树上求LCA。

Time Limit: 8000MSMemory Limit: Unknown64bit IO Format: %lld & %llu

Submit Status

Description

Download as PDF

B

NEXT Generation Contest 4

Time Limit – 8 secs

Bond

 

Once again, James Bond is on his way to saving the world. Bond's latest mission requires him to travel between several pairs of cities in a certain country.

 

The country has N cities (numbered by 1, 2, . . ., N), connected by M bidirectional roads. Bond is going to steal a vehicle, and drive along the roads from city s to city t. The country's police will be patrolling the roads, looking for Bond, however, not all roads get the same degree of attention from the police.

 

More formally, for each road MI6 has estimated its dangerousness, the higher it is, the more likely Bond is going to be caught while driving on this road. Dangerousness of a path from s to t is defined as the maximum dangerousness of any road on this path.

 

Now, it's your job to help Bond succeed in saving the world by finding the least dangerous paths for his mission.

 

 

Input

There will be at most 5 cases in the input file.

The first line of each case contains two integers NM (2 ≤ N≤ 50000, 1≤ M ≤ 100000) – number of cities and roads. The next M lines describe the roads. The i-th of these lines contains three integers: xiyidi (1 ≤ xiyi ≤ N, 0 ≤ di ≤ 109) - the numbers of the cities connected by the ith road and its dangerousness.

 

Description of the roads is followed by a line containing an integer Q (1 ≤ Q ≤ 50000), followed by Q lines, the i-th of which contains two integers si and ti (1 ≤ siti  ≤ Nsi !=ti).

 

Consecutive input sets are separated by a blank line.

 

Output

For each case, output Q lines, the i-th of which contains the minimum dangerousness of a path between cities si and ti. Consecutive output blocks are separated by a blank line.

 

The input file will be such that there will always be at least one valid path.

 

Sample Input

Output for Sample Input

4 5

1 2 10

1 3 20

1 4 100

2 4 30

3 4 10

2

1 4

4 1

 

2 1

1 2 100

1

1 2

20

20

 

100

 

ProblemSetter: Ivan Krasilnikov 

Source

Root :: AOAPC I: Beginning Algorithm Contests -- Training Guide (Rujia Liu) :: Chapter 5. Graph Theory :: Minimal Spanning Tree and Variants :: Examples

Submit Status


#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
#define prt(k) cout<<#k" = "<<k<<endl;
typedef long long ll;
const int N = 100005;
const int M = N << 2;
int n, m, head[N], mm;
int len[N];
struct Edge {
    int to, next, w;
}e[N << 1];
struct EE {
    int u, v, w;
}E[M];
bool cmp(EE a, EE b) { return a.w < b.w; }
void add(int u, int v, int w)
{
    e[mm].to = v;
    e[mm].w = w;
    e[mm].next = head[u];
    head[u] = mm++;
}
int sz[N], dep[N];
int f[N][22]; /// f[i][j] 表示 i 的第 2^j 个祖先
int dp[N][22];
void dfs(int u, int fa) /// 点从 1 开始标号
{
    f[u][0] = fa;
    sz[u] = 1;
    for (int i=head[u];~i;i=e[i].next)
    {
        int v = e[i].to;
        if (v != fa)
        {
            dep[v] = dep[u] + 1;
            len[v] = len[u] + e[i].w;
            dp[v][0] = e[i].w;
            dfs(v, u);
            sz[u] += sz[v];
        }
    }
}
int maxh;
void gao()
{
    int j;
    for (j=1;(1<<j)<n;j++)
        for (int i=1;i<=n;i++)
    {
        int t = f[i][j-1];
        f[i][j] = f[f[i][j-1]][j-1];
        dp[i][j] = max(dp[i][j-1], dp[t][j-1]);
    }
    maxh = j - 1;
}
int swim(int x, int k, int &ma)   /// 返回 x 的第 k 个祖先
{
    ma = 0;
    for (int i=0;i<=maxh;i++)
        if (k >> i & 1)
    {
        ma = max(ma, dp[x][i]);
        x = f[x][i];
    }
    return x;
}
int LCA(int x, int y)
{
    if (dep[x] > dep[y]) swap(x, y); ///dep[x] <= dep[y];
    int ans = 0;
    y = swim(y, dep[y] - dep[x], ans);

    if (x == y) return ans;
    for (int i=maxh; i>=0; i--) {
        if (f[x][i] != f[y][i])
        {
            ans = max(ans, max(dp[x][i], dp[y][i]));
            x = f[x][i], y = f[y][i];
        }
    }
    return max(ans, max(dp[x][0], dp[y][0]) ); //f[x][0];
}

int fa[N];
int find(int x) { return x==fa[x] ? x : fa[x]=find(fa[x]); }
int main()
{
    int re; bool blank = false;
while (cin >> n >> m) {
    dep[1] = 0;     len[1] = 0;
    if (blank) putchar(10);
    blank = true;
    dp[1][0] = 0;
    for (int i=0;i<m;i++) {
        scanf("%d%d%d", &E[i].u, &E[i].v, &E[i].w);
    }
    sort(E, E+m, cmp);
    for (int i=1;i<=n;i++) fa[i] = i;
    mm = 0;
    memset(head, -1, sizeof head);
    int cnt = 0;
    for (int i=0;i<m;i++) {
        int a=find(E[i].u), b=find(E[i].v);
        if (a - b) {
            fa[a] = b;
            add(E[i].u, E[i].v, E[i].w);
            add(E[i].v, E[i].u, E[i].w);
           // printf("[%d %d %d]\n", E[i].u, E[i].v, E[i].w);
            if (++cnt == n-1) break;
        }
    }
    dfs(1, 0);
   // for (int i=1;i<=n;i++) printf("dep[%d] = %d\n",i,dep[i]);
    gao();
   /** for (int i=1;i<=n;i++)
        for (int j=0;j<=maxh;j++)
    {
        printf("dp[%d][%d] = %d\n", i, j, dp[i][j]);
    }  */
    int Q;
    scanf("%d", &Q);
    while (Q--) {
        int a, b;
        scanf("%d%d",&a,&b);
        int c = LCA(a, b);
        printf("%d\n", c);
    }
  //  putchar(10);
}
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值