可惜没如果=_= 不知那年的樱花树上，哪一瓣最美

## 1787: [Ahoi2008]Meet 紧急集合

Time Limit: 20 Sec  Memory Limit: 162 MB
Submit: 3445  Solved: 1565
[Submit][Status][Discuss]

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

5 2
2 5
4 1
6 0

## Source

 1 #include "bits/stdc++.h"
2 using namespace std;
3 typedef long long LL;
4 const int MAX=5e5+5;
5 int n,m;
7 int fa[MAX][21],deep[MAX];
8 bool vis[MAX];
10     int an=0,x=1;char c=getchar();
11     while (c<'0' || c>'9') {if (c=='-') x=-1;c=getchar();}
12     while (c>='0' && c<='9') {an=(an<<3)+(an<<1)+c-'0';c=getchar();}
13     return an;
14 }
17 }
18 void dfs(int x){
19     vis[x]=true;
20     int i,j;
21     for (i=1;i<=20;i++){
22         if (deep[x]<(1<<i)) break;
23         fa[x][i]=fa[fa[x][i-1]][i-1];
24     }
30         }
31     }
32 }
33 int lca(int x,int y){
34     int i,j;
35     if (deep[x]<deep[y]) swap(x,y);
36     int dd=deep[x]-deep[y];
37     for (i=20;i>=0;i--)
38         if (dd&(1<<i)) x=fa[x][i];
39     for (i=20;i>=0;i--)
40         if (fa[x][i]!=fa[y][i]){
41             x=fa[x][i],y=fa[y][i];
42         }
43     return x==y?x:fa[x][0];
44 }
45 inline int dist(int x,int y,int z){return deep[x]+deep[y]-2*deep[z];}
46 int main(){
47     freopen ("emer.in","r",stdin);freopen ("emer.out","w",stdout);
48     int i,j,u,v,x,y,z,xy,yz,zx,dis;
50     for (i=1;i<n;i++){
53     }
54     dfs(1);
55     for (i=1;i<=m;i++){
57         dis=0;
58         xy=lca(x,y),zx=lca(x,z),yz=lca(y,z);
59         if (xy==zx){
60             printf("%d ",yz);
61             dis=dist(y,yz,yz)+dist(z,yz,yz)+dist(yz,x,xy);
62             printf("%d\n",dis);
63         }
64         else if (xy==yz){
65             printf("%d ",zx);
66             dis=dist(zx,x,zx)+dist(z,zx,zx)+dist(zx,y,xy);
67             printf("%d\n",dis);
68         }
69         else if (zx==yz){
70             printf("%d ",xy);
71             dis=dist(xy,x,xy)+dist(y,xy,xy)+dist(xy,z,zx);
72             printf("%d\n",dis);
73         }
74     }
75     return 0;
76 }

posted on 2017-10-24 19:13  珍珠鸟  阅读(237)  评论(0编辑  收藏  举报