Connections between cities(LCA + 并查集)

Connections between cities

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5067    Accepted Submission(s): 1410


Problem Description
After World War X, a lot of cities have been seriously damaged, and we need to rebuild those cities. However, some materials needed can only be produced in certain places. So we need to transport these materials from city to city. For most of roads had been totally destroyed during the war, there might be no path between two cities, no circle exists as well.
Now, your task comes. After giving you the condition of the roads, we want to know if there exists a path between any two cities. If the answer is yes, output the shortest path between them.
 

 

Input
Input consists of multiple problem instances.For each instance, first line contains three integers n, m and c, 2<=n<=10000, 0<=m<10000, 1<=c<=1000000. n represents the number of cities numbered from 1 to n. Following m lines, each line has three integers i, j and k, represent a road between city i and city j, with length k. Last c lines, two integers i, j each line, indicates a query of city i and city j.
 

 

Output
For each problem instance, one line for each query. If no path between two cities, output “Not connected”, otherwise output the length of the shortest path between them.
 

 

Sample Input
5 3 2
1 3 2
2 4 3
5 2 3
1 4
4 5
 

 

Sample Output

 

Not connected
6
Hint
Hint Huge input, scanf recommended.

 

      题意:

      给出 N 个结点,M 条边,K 个询问。输出每个询问间的距离,如果不能达到的话则输出 Not connected。

 

      思路:

      LCA + 并查集。用并查集判断是否在同一棵上,在同一棵树上则用 LCA 算出距离即可。

 

      AC:

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int NMAX = 10010;
const int EMAX = 10000 * 5;

int n, m, c;

int root[NMAX];

int ind;
int fir[NMAX], next[EMAX], w[EMAX], v[EMAX];

int ans;
int id[NMAX], vs[NMAX * 3], dep[NMAX * 3], dis[NMAX];
bool vis[NMAX];

int dp[NMAX * 3][50];

void init () {
    ind = ans = 0;
    memset(fir, -1, sizeof(fir));
    memset(vis, 0, sizeof(vis));
    for (int i = 1; i <= n; ++i)
        root[i] = i;
}

void add_edge (int f, int t, int val) {
    v[ind] = t;
    w[ind] = val;
    next[ind] = fir[f];
    fir[f] = ind;
    ++ind;
}

int Find (int x) {
    return x == root[x] ? x : root[x] = Find(root[x]);
}

void merge_tree (int a, int b) {
    int A = Find(a);
    int B = Find(b);
    if (A != B) root[A] = B;
}

bool que_tree (int a, int b) {
    if (Find(a) == Find(b)) return true;
    return false;
}

void dfs (int x, int d) {
    id[x] = ans;
    vs[ans] = x;
    dep[ans++] = d;
    vis[x] = 1;

    for (int e = fir[x]; e != -1; e = next[e]) {
        int V = v[e];
        if (!vis[V]) {
            dis[V] = dis[x] + w[e];
            dfs(V, d + 1);
            dep[ans] = d;
            vs[ans++] = x;
        }
    }
}

void RMQ_init () {
    for (int i = 0; i < ans; ++i) dp[i][0] = i;

    for (int j = 1; (1 << j) <= ans; ++j) {
        for (int i = 0; i + (1 << j) < ans; ++i) {
            int a = dp[i][j - 1];
            int b = dp[i + (1 << (j - 1))][j - 1];
            if (dep[a] < dep[b]) dp[i][j] = a;
            else dp[i][j] = b;
        }
    }
}

int RMQ (int L, int R) {
    int len = 0;
    while ((1 << (len + 1)) <= (R - L + 1)) ++len;

    int a = dp[L][len];
    int b = dp[R - (1 << len) + 1][len];

    if (dep[a] < dep[b]) return a;
    return b;
}

int LCA (int a, int b) {
    int L = min(id[a], id[b]);
    int R = max(id[a], id[b]);

    int node = RMQ(L, R);
    return vs[node];
}

int main() {

    while (~scanf("%d%d%d", &n, &m, &c)) {
        init();

        while (m--) {
            int a, b, val;
            scanf("%d%d%d", &a, &b, &val);
            add_edge(a, b, val);
            add_edge(b, a, val);
            merge_tree(a, b);
        }

        for (int i = 1; i <= n; ++i) {
            if (root[i] == i) {
                dis[i] = 0;
                dfs(i, 1);
            }
        }

        RMQ_init();

        while (c--) {
            int a, b;
            scanf("%d%d", &a, &b);
            if (que_tree(a, b)) {
                int c = LCA(a, b);
                printf("%d\n", dis[a] + dis[b] - 2 * dis[c]);
            } else puts("Not connected");
        }
    }

    return 0;
}

 

 

你可能感兴趣的:(Connection)