• poj 1986 Distance Queries(LCA)


    Description

    Farmer John's cows refused to run in his marathon since he chose a path much too long for their leisurely lifestyle. He therefore wants to find a path of a more reasonable length. The input to this problem consists of the same input as in "Navigation Nightmare",followed by a line containing a single integer K, followed by K "distance queries". Each distance query is a line of input containing two integers, giving the numbers of two farms between which FJ is interested in computing distance (measured in the length of the roads along the path between the two farms). Please answer FJ's distance queries as quickly as possible! 

    Input

    * Lines 1..1+M: Same format as "Navigation Nightmare" 
    
    * Line 2+M: A single integer, K. 1 <= K <= 10,000 
    
    * Lines 3+M..2+M+K: Each line corresponds to a distance query and contains the indices of two farms. 

    Output

     

    * Lines 1..K: For each distance query, output on a single line an integer giving the appropriate distance. 

     

     

    Sample Input
    7 6
    1 6 13 E
    6 3 9 E
    3 5 7 S
    4 1 3 N
    2 4 20 W
    4 7 2 S
    3
    1 6
    1 4
    2 6

    Sample Output

    13
    3
    36

    Hint

    Farms 2 and 6 are 20+3+13=36 apart. 

    Source

     

    题意:给一棵带权重的树,共有k个查询,每次查询树中2个结点的距离。结点数n最大为40000,k最大10000

    分析:首先我们将无根树转为有根树,可以在O(n)时间内得到每个结点到根结点的距离。由于在树中从一个结点走到另一个结点的路径是唯一的,所以a到b的路径一定经过lca(a,b),设lca(a,b)=c。此时不难发现d(a,b)=d(a,root)+d(b,root)-2*d(c,root)。

    这里用的是并查集的方法查找LCA,时间1000+ms,感觉有点慢

      1 #pragma comment(linker, "/STACK:1024000000,1024000000")
      2 #include<iostream>
      3 #include<cstdio>
      4 #include<cstring>
      5 #include<cmath>
      6 #include<math.h>
      7 #include<algorithm>
      8 #include<queue>
      9 #include<set>
     10 #include<bitset>
     11 #include<map>
     12 #include<vector>
     13 #include<stdlib.h>
     14 #include <stack>
     15 using namespace std;
     16 #define PI acos(-1.0)
     17 #define max(a,b) (a) > (b) ? (a) : (b)  
     18 #define min(a,b) (a) < (b) ? (a) : (b)
     19 #define ll long long
     20 #define eps 1e-10
     21 #define MOD 1000000007
     22 #define N 100006
     23 #define inf 1e12
     24 int n,m;
     25 
     26 int tot,qsize;
     27 int head[N],qhead[N];
     28 int vis[N];//标记 
     29 int dis[N];//距离
     30 int fa[N]; 
     31 
     32 struct Node
     33 {
     34     int from;
     35     int to;
     36     int next;
     37     int cost;
     38 }edge[N<<2],qe[N];
     39 void init()
     40 {
     41     tot=0; qsize=0;
     42    
     43     memset(head,-1,sizeof(head));
     44     memset(qhead,-1,sizeof(qhead));
     45     memset(vis,0,sizeof(vis));
     46     memset(fa,0,sizeof(fa));
     47     memset(dis,0,sizeof(dis));
     48     memset(edge,0,sizeof(edge));
     49     memset(qe,0,sizeof(qe));
     50 }
     51 void addEdge(int s,int u,int c)//邻接矩阵函数
     52 {
     53     edge[tot].from=s;
     54     edge[tot].to=u;
     55     edge[tot].cost=c;
     56     edge[tot].next=head[s];
     57     head[s]=tot++;
     58 }
     59 void addQedge(int s,int u){
     60     qe[qsize].from=s;
     61     qe[qsize].to=u;
     62     qe[qsize].next=qhead[s];
     63     qhead[s]=qsize++;
     64 }
     65 
     66 /////////////////////////////////////////////////////////////
     67 int find(int x){
     68     return fa[x]==x?x:fa[x]=find(fa[x]);
     69 }
     70 /////////////////////////////////////////////////////////////
     71 void tarjan(int u)//tarjan算法找出图中的所有强连通分支
     72 {
     73     fa[u]=u;
     74     vis[u]=1;
     75     for(int i=head[u];i!=-1;i=edge[i].next){
     76            int v=edge[i].to;
     77            if(!vis[v]){
     78                 dis[v]=dis[u]+edge[i].cost;
     79           tarjan(v);
     80          fa[v]=u; 
     81         }
     82     }
     83     for(int i=qhead[u];i!=-1;i=qe[i].next){
     84            int v=qe[i].to;
     85            if(vis[v]){
     86              qe[i].cost=dis[u]+dis[v]-2*dis[find(v)];
     87          qe[i^1].cost=qe[i].cost;    
     88         }
     89     }
     90 }
     91 int main()
     92 {
     93     while(scanf("%d%d",&n,&m)==2){
     94         init();
     95         int a,b,c;
     96         char s[3];
     97         for(int i=0;i<m;i++){
     98             scanf("%d%d%d%s",&a,&b,&c,s);
     99             addEdge(a,b,c);
    100             addEdge(b,a,c);
    101         }
    102         int q;
    103         scanf("%d",&q);
    104         while(q--){
    105             scanf("%d%d",&a,&b);
    106             addQedge(a,b);
    107             addQedge(b,a);
    108         }
    109         tarjan(1);
    110         for(int i=0;i<qsize;i+=2){
    111             printf("%d
    ",qe[i].cost);
    112         }
    113     }
    114     return 0;
    115 }
    View Code
     1 #pragma comment(linker, "/STACK:1024000000,1024000000")
     2 #include<iostream>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<math.h>
     7 #include<algorithm>
     8 #include<queue>
     9 #include<set>
    10 #include<bitset>
    11 #include<map>
    12 #include<vector>
    13 #include<stdlib.h>
    14 #include <stack>
    15 using namespace std;
    16 #define PI acos(-1.0)
    17 #define max(a,b) (a) > (b) ? (a) : (b)  
    18 #define min(a,b) (a) < (b) ? (a) : (b)
    19 #define ll long long
    20 #define eps 1e-10
    21 #define MOD 1000000007
    22 #define N 200000
    23 #define inf 1e12
    24 vector<pair<int,int> >edge[N];
    25 vector<pair<int,int> >que[N];
    26 int n,m,q; 
    27 int ans[N];
    28 int dis[N];
    29 int fa[N];
    30 int vis[N];
    31 int sum;
    32 int find(int x){
    33     return fa[x]==x?x:fa[x]=find(fa[x]);
    34 }
    35 void LCA(int u,int p){
    36     fa[u]=u;
    37     for(int i=0;i<edge[u].size();i++){
    38         int v=edge[u][i].first;
    39         if(v==p) continue;
    40         dis[v]=dis[u]+edge[u][i].second;
    41         LCA(v,u);
    42         fa[v]=u;
    43     }
    44     vis[u]=1;
    45     if(sum==q) return;
    46     for(int i=0;i<que[u].size();i++){
    47         int v=que[u][i].first;
    48         if(vis[v]){
    49             ans[que[u][i].second]=dis[u]+dis[v]-2*dis[find(v)];
    50         }
    51     }
    52 }
    53 int main()
    54 {
    55     while(scanf("%d%d",&n,&m)==2){
    56         
    57         for(int i=0;i<N;i++){
    58             edge[i].clear();
    59             que[i].clear();
    60         }
    61         sum=0;
    62         memset(vis,0,sizeof(vis));
    63         
    64         char s[3];
    65         for(int i=0;i<m;i++){
    66             int a,b,c;
    67             scanf("%d%d%d%s",&a,&b,&c,s);
    68             edge[a].push_back(make_pair(b,c));
    69             edge[b].push_back(make_pair(a,c));
    70         }
    71         
    72         scanf("%d",&q);
    73         for(int i=0;i<q;i++){
    74             int x,y;
    75             scanf("%d%d",&x,&y);
    76             que[x].push_back(make_pair(y,i));
    77             que[y].push_back(make_pair(x,i));
    78             ans[i]=0;
    79         }
    80         dis[1]=0;
    81         LCA(1,0);
    82         
    83         for(int i=0;i<q;i++){
    84             printf("%d
    ",ans[i]);
    85         }
    86     }
    87     return 0;
    88 }
    View Code
  • 相关阅读:
    centos7源以及相关的一些命令
    创建Vue实例的三种方法
    github 钩子管理工具 overcommit
    npm管理registry 【转】
    两个字典增量部分
    celery (二) task调用
    shell编程
    linux 环境变量 转
    celery (二) task
    celery(一) application
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/4817607.html
Copyright © 2020-2023  润新知