zoukankan      html  css  js  c++  java
  • Connections between cities LCA

    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.

     

     

    题意是说给你一个森林,让你求两点之间的最近距离。
    lca求最近公共祖先,如果不是在同一棵树上,则输出Not connected。

    用并查集来判断是否在同一颗树上面

      1 #include <cstdio>
      2 #include <cstring>
      3 #include <queue>
      4 #include <cmath>
      5 #include <algorithm>
      6 #include <set>
      7 #include <iostream>
      8 #include <map>
      9 #include <stack>
     10 #include <string>
     11 #include <vector>
     12 #define  pi acos(-1.0)
     13 #define  eps 1e-6
     14 #define  fi first
     15 #define  se second
     16 #define  lson l,m,rt<<1
     17 #define  rson m+1,r,rt<<1|1
     18 #define  bug         printf("******
    ")
     19 #define  mem(a,b)    memset(a,b,sizeof(a))
     20 #define  fuck(x)     cout<<"["<<x<<"]"<<endl
     21 #define  f(a)        a*a
     22 #define  sf(n)       scanf("%d", &n)
     23 #define  sff(a,b)    scanf("%d %d", &a, &b)
     24 #define  sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
     25 #define  sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
     26 #define  pf          printf
     27 #define  FRE(i,a,b)  for(i = a; i <= b; i++)
     28 #define  FREE(i,a,b) for(i = a; i >= b; i--)
     29 #define  FRL(i,a,b)  for(i = a; i < b; i++)
     30 #define  FRLL(i,a,b) for(i = a; i > b; i--)
     31 #define  FIN         freopen("DATA.txt","r",stdin)
     32 #define  gcd(a,b)    __gcd(a,b)
     33 #define  lowbit(x)   x&-x
     34 #pragma  comment (linker,"/STACK:102400000,102400000")
     35 using namespace std;
     36 typedef long long LL;
     37 typedef unsigned long long ULL;
     38 const int maxn = 1e5 + 10;
     39 int _pow[maxn], dep[maxn], dis[maxn], vis[maxn], ver[maxn];
     40 int tot, head[maxn], dp[maxn * 2][25], k, first[maxn], fa[maxn];
     41 struct node {
     42     int u, v, w, nxt;
     43 } edge[maxn << 2];
     44 void init() {
     45     tot = 0;
     46     mem(head, -1);
     47     for (int i = 0 ; i < maxn ; i++) fa[i] = i;
     48 }
     49 int Find(int x) {
     50     return x == fa[x] ? fa[x] : fa[x] = Find(fa[x]);
     51 }
     52 void combine(int x, int y) {
     53     int nx = Find(x), ny = Find(y);
     54     if(nx != ny) fa[nx] = ny;
     55     return ;
     56 }
     57 void add(int u, int v, int w) {
     58     edge[tot].v = v, edge[tot].u = u;
     59     edge[tot].w = w, edge[tot].nxt = head[u];
     60     head[u] = tot++;
     61 }
     62 void dfs(int u, int DEP) {
     63     vis[u] = 1;
     64     ver[++k] = u;
     65     first[u] = k;
     66     dep[k] = DEP;
     67     for (int i = head[u]; ~i; i = edge[i].nxt) {
     68         if (vis[edge[i].v]) continue;
     69         int v = edge[i].v, w = edge[i].w;
     70         dis[v] = dis[u] + w;
     71         dfs(v, DEP + 1);
     72         ver[++k] = u;
     73         dep[k] = DEP;
     74     }
     75 }
     76 void ST(int len) {
     77     int K = (int)(log((double)len) / log(2.0));
     78     for (int i = 1 ; i <= len ; i++) dp[i][0] = i;
     79     for (int j = 1 ; j <= K ; j++) {
     80         for (int i = 1 ; i + _pow[j] - 1 <= len ; i++) {
     81             int a = dp[i][j - 1], b = dp[i + _pow[j - 1]][j - 1];
     82             if (dep[a] < dep[b]) dp[i][j] = a;
     83             else dp[i][j] = b;
     84         }
     85     }
     86 }
     87 int RMQ(int x, int y) {
     88     int K = (int)(log((double)(y - x + 1)) / log(2.0));
     89     int a = dp[x][K], b = dp[y - _pow[K] + 1][K];
     90     if (dep[a] < dep[b]) return a;
     91     else return b;
     92 }
     93 int LCA(int u, int v) {
     94     int x = first[u], y = first[v];
     95     if (x > y) swap(x, y);
     96     int ret = RMQ(x, y);
     97     return ver[ret];
     98 }
     99 int main() {
    100     for (int i = 0 ; i < 40 ; i++) _pow[i] = (1 << i);
    101     int n, m, q;
    102     while(~sfff(n, m, q)) {
    103         init();
    104         mem(vis, 0);
    105         for (int i = 0 ; i < m ; i++) {
    106             int u, v, w;
    107             sfff(u, v, w);
    108             add(u, v, w);
    109             add(v, u, w);
    110             combine(u, v);
    111         }
    112         k = 0;
    113         for (int i = 1 ; i <= n ; i++) {
    114             if (fa[i] == i) {
    115                 dis[i] = 0;
    116                 dfs(i, 1);
    117             }
    118         }
    119         ST(2 * n - 1);
    120         while(q--) {
    121             int u, v;
    122             sff(u, v);
    123             int lca = LCA(u, v);
    124             if (Find(u) == Find(v)) printf("%d
    ", dis[u] + dis[v] - 2 * dis[lca]);
    125             else printf("Not connected
    ");
    126         }
    127     }
    128     return  0;
    129 }
  • 相关阅读:
    Mysql:为什么用limit时,offset很大会影响性能
    [解决方案]未能找到路径“~in oslyncsc.exe”的一部分
    [经验分享]NuGet发布自己的Dll(类库包)
    [解决方案]使用百度富文本编辑器,编辑显示不了内容
    [解决方案]未能加载文件或程序集
    [经验分享]WebApi+SwaggerUI 完美展示接口
    [经验分享]Linux网络连接-VMware+CentOS 7
    [经验分享]WebAPI中返回类型JsonMessage的应用
    [解决方案]WebAPI+SwaggerUI部署服务器后,访问一直报错的问题
    [解决方案] 当 IDENTITY_INSERT 设置为 OFF 时
  • 原文地址:https://www.cnblogs.com/qldabiaoge/p/9447264.html
Copyright © 2011-2022 走看看