hdu 2874 Connections between cities

本文从WordPress迁移而来, 查看全部WordPress迁移文章

LCA

题意:询问LCA,不过图不连通,如果两点不连通输出那串英文否则输出两点间的距离,模板题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define N 10010
#define M 10010
#define Q 1000010

int head[N];
struct edge{
int u,v,w,next;
}e[2*M];
int __head[N];
struct ask{
int u,v,lca,next;
}ea[2*Q];
int dir[N],fa[N],vis[N];

inline void add_edge(int u ,int v ,int w ,int &k)
{
e[k].u = u; e[k].v = v; e[k].w = w;
e[k].next = head[u]; head[u] = k++;
}
inline void add_ask(int u ,int v ,int &k)
{
ea[k].u = u; ea[k].v = v; ea[k].lca = -1;
ea[k].next = __head[u]; __head[u] = k++;
}

int find(int x){
return x == fa[x] ? x : fa[x] = find(fa[x]);
}

void Tarjan(int u ,int c)
{
vis[u] = c; fa[u] = u;
for(int k=head[u]; k!=-1; k=e[k].next)
if(!vis[e[k].v])
{
int v = e[k].v , w = e[k].w;
dir[v] = dir[u] + w;
Tarjan(v,c);
fa[v] = u;
}
for(int k=__head[u]; k!=-1; k=ea[k].next)
if(vis[ea[k].v] == c)
ea[k].lca = ea[k^1].lca = find(ea[k].v);
}

int main()
{
int n,m,q,k;
while(scanf("%d%d%d",&n,&m,&q)!=EOF)
{
memset(head,-1,sizeof(head));
memset(__head,-1,sizeof(__head));
memset(vis,0,sizeof(vis));
k = 0;
for(int i=0; i<m; i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add_edge(u,v,w,k);
add_edge(v,u,w,k);
}
k = 0;
for(int i=0; i<q; i++)
{
int u,v;
scanf("%d%d",&u,&v);
add_ask(u,v,k);
add_ask(v,u,k);
}
k = 0;
for(int i=1; i<=n; i++)
if(!vis[i])
{
dir[i] = 0;
Tarjan(i,++k);
}
for(int i=0; i<q; i++)
{
int s = i*2 , u = ea[s].u , v = ea[s].v , lca = ea[s].lca;
if(lca == -1) printf("Not connected\n");
else printf("%d\n",dir[u] + dir[v] - 2*dir[lca]);
}
}
return 0;
}