码迷,mamicode.com
首页 > 其他好文 > 详细

bzoj1787

时间:2015-07-18 18:21:38      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:

题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1787

刚开始好像很难的样子

画了一下,发现好像只有3种情况,且最终的P点一定是两两LCA中的一个。

裸的LCA

怪不得数据那么大

技术分享
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<utility>
#include<set>
#include<bitset>
#include<vector>
#include<functional>
#include<deque>
#include<cctype>
#include<climits>
#include<complex>
//#include<bits/stdc++.h>适用于CF,UOJ,但不适用于poj
 
using namespace std;

typedef long long LL;
typedef double DB;
typedef pair<int,int> PII;
typedef complex<DB> CP;

#define mmst(a,v) memset(a,v,sizeof(a))
#define mmcy(a,b) memcpy(a,b,sizeof(a))
#define re(i,a,b)  for(i=a;i<=b;i++)
#define red(i,a,b) for(i=a;i>=b;i--)
#define fi first
#define se second
#define m_p(a,b) make_pair(a,b)
#define SF scanf
#define PF printf
#define two(k) (1<<(k))

template<class T>inline T sqr(T x){return x*x;}
template<class T>inline void upmin(T &t,T tmp){if(t>tmp)t=tmp;}
template<class T>inline void upmax(T &t,T tmp){if(t<tmp)t=tmp;}

const DB EPS=1e-9;
inline int sgn(DB x){if(abs(x)<EPS)return 0;return(x>0)?1:-1;}
const DB Pi=acos(-1.0);

inline int gint()
  {
        int res=0;bool neg=0;char z;
        for(z=getchar();z!=EOF && z!=- && !isdigit(z);z=getchar());
        if(z==EOF)return 0;
        if(z==-){neg=1;z=getchar();}
        for(;z!=EOF && isdigit(z);res=res*10+z-0,z=getchar());
        return (neg)?-res:res; 
    }
inline LL gll()
  {
      LL res=0;bool neg=0;char z;
        for(z=getchar();z!=EOF && z!=- && !isdigit(z);z=getchar());
        if(z==EOF)return 0;
        if(z==-){neg=1;z=getchar();}
        for(;z!=EOF && isdigit(z);res=res*10+z-0,z=getchar());
        return (neg)?-res:res; 
    }

const int maxN=500000;
const int maxM=500000;

int N,M;
int now,first[maxN+100];
struct Tedge{int v,next;}edge[2*maxN+100];
int fa[maxN+100],dep[maxN+100],jump[maxN+100][40];

inline void addedge(int u,int v)
  {
      now++;
      edge[now].v=v;
      edge[now].next=first[u];
      first[u]=now;
  }

int head,tail,que[maxN+100];
inline void BFS()
  {
      int i,j;
      dep[que[head=tail=1]=1]=1;
      fa[1]=1;
      while(head<=tail)
        {
            int u=que[head++],v;
            for(i=first[u],v=edge[i].v;i!=-1;i=edge[i].next,v=edge[i].v)if(!dep[v]) dep[que[++tail]=v]=dep[u]+1,fa[v]=u;
        }
      re(j,1,tail)
        {
            int u=que[j];
            jump[u][0]=fa[u];
            re(i,1,30)jump[u][i]=jump[jump[u][i-1]][i-1];
        }
  }

inline void swim(int &x,int H){for(int i=0;H!=0;H>>=1,i++)if(H&1)x=jump[x][i];}
inline int lca(int x,int y)
  {
      if(dep[x]<dep[y]) swap(x,y);
      swim(x,dep[x]-dep[y]);
      if(x==y)return x;
      int i;red(i,30,0)if(jump[x][i]!=jump[y][i])x=jump[x][i],y=jump[y][i];
      return jump[x][0];
  }

inline int dist(int a,int b){return dep[a]+dep[b]-2*dep[lca(a,b)];}

int main()
  {
      freopen("bzoj1787.in","r",stdin);
      freopen("bzoj1787.out","w",stdout);
      int i;
      N=gint();M=gint();
      mmst(first,-1);now=-1;
      re(i,1,N-1){int x=gint(),y=gint();addedge(x,y);addedge(y,x);}
      BFS();
      while(M--)
        {
            int a=gint(),b=gint(),c=gint(),p,cost,ansp,anscost;
            ansp=lca(a,b);anscost=dist(a,ansp)+dist(b,ansp)+dist(c,ansp);
            p=lca(a,c);cost=dist(a,p)+dist(b,p)+dist(c,p);if(cost<anscost){anscost=cost;ansp=p;}
            p=lca(b,c);cost=dist(a,p)+dist(b,p)+dist(c,p);if(cost<anscost){anscost=cost;ansp=p;}
            PF("%d %d\n",ansp,anscost);
        }
      return 0;
  }
            
View Code

 

bzoj1787

标签:

原文地址:http://www.cnblogs.com/maijing/p/4657209.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!