HDU 2874 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
 
 
 
 
 
注意:这道题不一定是树,有可能是森林,
只要学树链剖分,加一个top[i] 表示节点i的根是top[i] 就好啦。
 
 
length=siz[u]+siz[v]-2*siz[lca(u,v)]
 
 
 
 
  1 #include<cstdio>
  2 #include<algorithm>
  3 #include<cstring>
  4 
  5 using namespace std;
  6 
  7 #define LL long long
  8 
  9 const int maxn=10000+5;
 10 
 11 struct edge
 12 {
 13     int to,w,next;
 14 }edge[maxn<<1];
 15 int head[maxn];
 16 int tot;
 17 
 18 int siz[maxn];
 19 int dep[maxn];
 20 int p[maxn][25];
 21 int top[maxn];
 22 
 23 void init(int n)
 24 {
 25     tot=0;
 26     memset(head,-1,sizeof(head));
 27     memset(siz,-1,sizeof(siz));
 28     memset(dep,0,sizeof(dep));
 29 
 30     for(int i=1;i<=n;i++)
 31         top[i]=i;
 32 
 33     for(int i=1;i<=n;i++)
 34         for(int j=0;j<25;j++)
 35             p[i][j]=-1;
 36 }
 37 
 38 void addedge(int u,int v,int w)
 39 {
 40     edge[tot].to=v;
 41     edge[tot].w=w;
 42     edge[tot].next=head[u];
 43     head[u]=tot++;
 44 }
 45 
 46 void dfs(int u)
 47 {
 48     for(int i=head[u];~i;i=edge[i].next)
 49     {
 50         int v=edge[i].to;
 51         int w=edge[i].w;
 52         if(!dep[v])
 53         {
 54             siz[v]=siz[u]+w;
 55             dep[v]=dep[u]+1;
 56             p[v][0]=u;
 57             top[v]=top[u];
 58             dfs(v);
 59         }
 60     }
 61 }
 62 
 63 void init_lca(int n)
 64 {
 65     for(int j=1;(1<<j)<=n;j++)
 66     {
 67         for(int i=1;i<=n;i++)
 68         {
 69             if(p[i][j-1]!=-1)
 70             {
 71                 p[i][j]=p[p[i][j-1]][j-1];
 72             }
 73         }
 74     }
 75 }
 76 
 77 LL solve(int n,int u,int v)
 78 {
 79     if(dep[u]<dep[v])
 80         swap(u,v);
 81 
 82     int init_u=u;
 83     int init_v=v;
 84 
 85     int cnt;
 86     for(cnt=0;(1<<cnt)<=dep[u];cnt++)
 87         ;
 88     cnt--;
 89 
 90     for(int j=cnt;j>=0;j--)
 91     {
 92         if(dep[u]-(1<<j)>=dep[v])
 93             u=p[u][j];
 94     }
 95     if(u==v)
 96         return (LL)(siz[init_u]-siz[v]);
 97     else
 98     {
 99         for(int j=cnt;j>=0;j--)
100         {
101             if(p[u][j]!=-1&&p[u][j]!=p[v][j])
102             {
103                 u=p[u][j];
104                 v=p[v][j];
105             }
106         }
107         return (LL)(siz[init_u]+siz[init_v]-2*siz[p[u][0]]);
108     }
109 }
110 
111 int main()
112 {
113     int n;
114     while(scanf("%d",&n)!=EOF)
115     {
116         int m,c;
117         scanf("%d%d",&m,&c);
118 
119         init(n);
120 
121         for(int i=1;i<=m;i++)
122         {
123             int u,v,w;
124             scanf("%d%d%d",&u,&v,&w);
125             addedge(u,v,w);
126             addedge(v,u,w);
127         }
128 
129         for(int i=1;i<=n;i++)
130         {
131             if(siz[i]==-1)
132             {
133                 siz[i]=0;
134                 dfs(i);
135             }
136         }
137 
138         init_lca(n);
139 
140         for(int i=0;i<c;i++)
141         {
142             int u,v;
143             scanf("%d%d",&u,&v);
144 
145             if(top[u]!=top[v])
146             {
147                 printf("Not connected
");
148             }
149             else
150             {
151                 printf("%lld
",solve(n,u,v));
152             }
153         }
154 
155     }
156 
157     return 0;
158 }
1700ms
 
 
 
 
 
 
 
原文地址:https://www.cnblogs.com/-maybe/p/4500156.html