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

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

## HINT

solution:

1 #include<iostream>
2 #include<cstring>
3 #include<cstdio>
4 #include<algorithm>
5 #define maxn 500010
6 using namespace std;
7 struct node{int to,next;}e[maxn*2];
9 int n,m;
11 {
13 }
14 void dfs1(int x)
15 {
16     deep[x]=deep[fa[x]]+1;siz[x]=1;
18     {
19         int to=e[i].to;
20         if(fa[x]!=to)
21         {
22             fa[to]=x;
23             dfs1(to);
24             siz[x]+=siz[to];
25             if(siz[son[x]]<siz[to]) son[x]=to;
26         }
27     }
28 }
29 void dfs2(int x)
30 {
31     if(x==son[fa[x]]) top[x]=top[fa[x]];
32     else top[x]=x;
34     if(fa[e[i].to]==x) dfs2(e[i].to);
35 }
36 void inti()
37 {
38     int a,b;
39     scanf("%d%d",&n,&m);
41     dfs1(1);dfs2(1);
42 }
43 int lca(int x,int y)
44 {
45     for(;top[x]!=top[y];deep[top[x]]>deep[top[y]]?x=fa[top[x]]:y=fa[top[y]]);
46     return deep[x]<deep[y]?x:y;
47 }
48 int cc(int x,int y)
49 {
50     int t=lca(x,y);
51     return deep[x]+deep[y]-2*deep[t];
52 }
53 void work()
54 {
55     int a,b,c,x,y,z,ans,sum;
56     for(int i=1;i<=m;i++)
57     {
58         scanf("%d%d%d",&x,&y,&z);
59         int p1=lca(x,y),p2=lca(x,z),p3=lca(y,z),t;
60         if(p1==p2)t=p3;
61          else if(p2==p3)t=p1;
62           else t=p2;
63        int ans;
64        ans=cc(x,t)+cc(y,t)+cc(z,t);
65        printf("%d %d\n",t,ans);
66     }
67
68 }
69 int main()
70 {
71     inti();
72     work();
73     return 0;
74 }
View Code

以上by Carrot

posted @ 2016-10-18 16:45  Native_carrot  阅读(126)  评论(0编辑  收藏  举报