# 2017.10.24：lca专题系列

在输出文件中输出该商人旅行的最短时间。

5
1 2
1 5
3 5
4 5
4
1
3
2
5

7

 1 #include<cstdio>
2 #include<algorithm>
3 #include<cstring>
4 #define maxn 30005
5 using namespace std;
7 struct edge{
8     int v,next;
9 }E[maxn<<1];
11 {
12     E[++ecnt].v=v;
15 }
16 void dfs(int x)
17 {
18     siz[x]=1;
19     for(int i=head[x] ; i ; i=E[i].next )
20     {
21         int v=E[i].v;
22         if(fa[x]==v)continue;
23         fa[v]=x;deep[v]=deep[x]+1;
24         dfs(v);
25         siz[x]+=siz[v];
26         if(siz[son[x]]<siz[v])son[x]=v;
27     }
28 }
29 void dfs2(int x,int tp)
30 {
31     top[x]=tp;
32     if(son[x])dfs2(son[x],tp);
33     for(int i=head[x]; i ; i=E[i].next )
34     {
35         int v=E[i].v;
36         if(son[x]==v||fa[x]==v)continue;
37         dfs2(v,v);
38     }
39 }
40 int lca(int x,int y)
41 {
42     while(top[x]!=top[y])
43     {
44         if(deep[top[x]]<deep[top[y]])swap(x,y);
45         x=fa[top[x]];
46     }
47     return deep[x]<deep[y]?x:y;
48 }
49
50 int main()
51 {
52     int u,v,tmp,a;
53     scanf("%d",&n);
54     for(int i=1 ; i<n ; ++i )
55     {
56         scanf("%d%d",&u,&v);
58     }
59     dfs(1);dfs2(1,1);
60     scanf("%d",&m);
61     scanf("%d",&tmp);
62     for(int i=2 ; i<=m ; ++i )
63     {
64         scanf("%d",&a);
65         ans+=deep[tmp]+deep[a]-deep[lca(tmp,a)]*2;
66         tmp=a;
67     }
68     printf("%d",ans);
69     return 0;
70 }

bzoj1787：[Ahoi2008]Meet 紧急集合

## Output

 1 #include<cstdio>
2 #include<algorithm>
3 #include<cstring>
4 #define maxn 500005
5 using namespace std;
7 struct edge{
8     int v,next;
9 }E[maxn<<1];
11 {
12     E[++ecnt].v=v;
15 }
16 void dfs(int x)
17 {
18     siz[x]=1;
19     for(int i=head[x] ; i ; i=E[i].next )
20     {
21         int v=E[i].v;
22         if(fa[x]==v)continue;
23         deep[v]=deep[x]+1;fa[v]=x;
24         dfs(v);
25         siz[x]+=siz[v];
26         if(siz[son[x]]<siz[v])son[x]=v;
27     }
28 }
29 void dfs2(int x,int tp)
30 {
31     top[x]=tp;
32     if(son[x])dfs2(son[x],tp);
33     for(int i=head[x] ; i ; i=E[i].next )
34     {
35         int v=E[i].v;
36         if(son[x]==v||fa[x]==v)continue;
37         dfs2(v,v);
38     }
39 }
40 int lca(int x,int y)
41 {
42     while(top[x]!=top[y])
43     {
44         if(deep[top[x]]<deep[top[y]])swap(x,y);
45         x=fa[top[x]];
46     }
47     return deep[x]<deep[y]?x:y;
48 }
50 {
51     int ret(0);
52     char ch=getchar();
53     while(ch>'9'||ch<'0')ch=getchar();
54     while(ch>='0'&&ch<='9')
55     {
56         ret=(ret<<1)+(ret<<3)+ch-'0';
57         ch=getchar();
58     }
59     return ret;
60 }
61 int main()
62 {
63     int u,v,a,b,c,t1,t2,t3,jud,tmp,fu;
65     for(int i=1 ; i<n ; ++i )
66     {
69     }
70     dfs(1);dfs2(1,1);
71     while(m--)
72     {
74         t1=lca(a,b);t2=lca(a,c);t3=lca(b,c);
75         if(t1==t2)
76         {
77             fu=t3;
78             jud=lca(t3,a);
79             tmp=deep[b]+deep[c]+deep[a]-deep[t3]-deep[jud]*2;
80         }
81         if(t2==t3)
82         {
83             fu=t1;
84             jud=lca(t1,c);
85             tmp=deep[b]+deep[c]+deep[a]-deep[t1]-deep[jud]*2;
86         }
87         if(t1==t3)
88         {
89             fu=t2;
90             jud=lca(t2,b);
91             tmp=deep[b]+deep[c]+deep[a]-deep[t2]-deep[jud]*2;
92         }
93         printf("%d %d\n",fu,tmp);
94     }
95     return 0;
96 }

posted @ 2017-10-24 20:45 傅judge 阅读(...) 评论(...) 编辑 收藏