HDU-4547 CD操作 LCA

  题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4547

  去年区预赛网络赛的时候写过一到LCA的题目<HDU4409>,本来想用那份代码修改下来交这道题的,不过貌似难得修改,就重新写了ST+DFS的代码当模板用。跑了2078ms,应该是map的效率低了。。。

  1 //STATUS:C++_AC_2078MS_26116KB

  2 #include <functional>

  3 #include <algorithm>

  4 #include <iostream>

  5 //#include <ext/rope>

  6 #include <fstream>

  7 #include <sstream>

  8 #include <iomanip>

  9 #include <numeric>

 10 #include <cstring>

 11 #include <cassert>

 12 #include <cstdio>

 13 #include <string>

 14 #include <vector>

 15 #include <bitset>

 16 #include <queue>

 17 #include <stack>

 18 #include <cmath>

 19 #include <ctime>

 20 #include <list>

 21 #include <set>

 22 #include <map>

 23 using namespace std;

 24 //using namespace __gnu_cxx;

 25 //define

 26 #define pii pair<int,int>

 27 #define mem(a,b) memset(a,b,sizeof(a))

 28 #define lson l,mid,rt<<1

 29 #define rson mid+1,r,rt<<1|1

 30 #define PI acos(-1.0)

 31 //typedef

 32 typedef __int64 LL;

 33 typedef unsigned __int64 ULL;

 34 //const

 35 const int N= 200010;

 36 const int INF=0x3f3f3f3f;

 37 const int MOD=100000,STA=8000010;

 38 const LL LNF=1LL<<60;

 39 const double EPS=1e-8;

 40 const double OO=1e15;

 41 const int dx[4]={-1,0,1,0};

 42 const int dy[4]={0,1,0,-1};

 43 const int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};

 44 //Daily Use ...

 45 inline int sign(double x){return (x>EPS)-(x<-EPS);}

 46 template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}

 47 template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}

 48 template<class T> inline T lcm(T a,T b,T d){return a/d*b;}

 49 template<class T> inline T Min(T a,T b){return a<b?a:b;}

 50 template<class T> inline T Max(T a,T b){return a>b?a:b;}

 51 template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}

 52 template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}

 53 template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}

 54 template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}

 55 //End

 56 

 57 struct Edge{

 58     int u,v;

 59 }e[N];

 60 int first[N],next[N],f[N][20],d[N],E[N],R[N],vis[N];

 61 int T,n,m,mt,all,dis,root;

 62 map<string,int> q;

 63 

 64 void adde(int a,int b)

 65 {

 66     e[mt].u=a;e[mt].v=b;

 67     next[mt]=first[a],first[a]=mt++;

 68 }

 69 

 70 int Minele(int i,int j)

 71 {

 72     return d[i]<d[j]?i:j;

 73 }

 74 

 75 void rmq_init(int n)

 76 {

 77     int i,j;

 78     for(i=1;i<=n;i++)f[i][0]=i;

 79     for(j=1;(1<<j)<=n;j++){

 80         for(i=1;i+(1<<j)-1<=n;i++){

 81             f[i][j]=Minele(f[i][j-1],f[i+(1<<(j-1))][j-1]);

 82         }

 83     }

 84 }

 85 

 86 int rmq(int l,int r)

 87 {

 88     int k=0;

 89     while((1<<(k+1))<=r-l+1)k++;

 90     return Minele(f[l][k],f[r-(1<<k)+1][k]);

 91 }

 92 

 93 void dfs(int u,int deep)

 94 {

 95     d[dis]=deep;

 96     E[dis]=u;

 97     R[u]=dis++;

 98     int i;

 99     for(i=first[u];i!=-1;i=next[i]){

100         dfs(e[i].v,deep+1);

101         d[dis]=deep;

102         E[dis++]=u;

103     }

104 }

105 

106 void LCA_Init()

107 {

108     dis=1;

109     dfs(root,0);

110     rmq_init(dis-1);

111 }

112 

113 int LCA(int a,int b)

114 {

115     int left=R[a],right=R[b];

116     if(left>right)swap(left,right);

117     return E[rmq(left,right)];

118 }

119 

120 int main()

121 {

122  //   freopen("in.txt","r",stdin);

123     int i,j,fa,a,b;

124     char s1[42],s2[42];

125     scanf("%d",&T);

126     while(T--)

127     {

128         scanf("%d%d",&n,&m);

129         q.clear();all=1;

130         mem(first,-1);mt=0;

131         mem(vis,0);

132         while(--n){

133             scanf("%s%s",s1,s2);

134             if(!q[s1])q[s1]=all++;

135             if(!q[s2])q[s2]=all++;

136             vis[q[s1]]=1;

137             adde(q[s2],q[s1]);

138         }

139         for(i=1;i<all;i++)if(!vis[i]){root=i;break;}

140 

141         LCA_Init();

142         while(m--){

143             scanf("%s%s",s1,s2);

144             a=q[s1];b=q[s2];

145             fa=LCA(a,b);

146             printf("%d\n",d[R[a]]-d[R[fa]]+(d[R[b]]>d[R[fa]]));

147         }

148     }

149     return 0;

150 }

 

你可能感兴趣的:(HDU)