标签:des style blog color java os io strong
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1111 Accepted Submission(s): 297
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <map> 5 #include <vector> 6 #include <queue> 7 #include <iostream> 8 using namespace std; 9 #define N 100005 10 #define M 20 11 12 int dep[N]; 13 int f[N][M]; 14 int visited[N]; 15 map<string,int>ma; 16 int n, m; 17 vector<int>ve[N]; 18 19 20 void init() 21 { 22 memset(dep,0,sizeof(dep)); 23 memset(f,0,sizeof(f)); 24 memset(visited,0,sizeof(visited)); 25 ma.clear(); 26 for(int i=0;i<=n;i++) ve[i].clear(); 27 } 28 29 void bfs(int root){ 30 queue<int>Q; 31 int i, u; 32 Q.push(root); 33 while(!Q.empty()){ 34 u=Q.front(); 35 Q.pop(); 36 if(visited[u]) continue; 37 visited[u]=1; 38 dep[u]=dep[f[u][0]]+1; 39 for(i=1;i<M;i++) f[u][i]=f[f[u][i-1]][i-1]; 40 for(i=0;i<ve[u].size();i++) Q.push(ve[u][i]); 41 } 42 } 43 44 int lca(int x,int y){ 45 if(dep[x]<dep[y]) swap(x,y); 46 int i, k=dep[x]-dep[y]; 47 for(i=0;i<M;i++){ 48 if(1<<i&k){ 49 x=f[x][i]; 50 } 51 } 52 if(x==y) return x; 53 for(i=M-1;i>=0;i--){ 54 if(f[x][i]!=f[y][i]){ 55 x=f[x][i]; 56 y=f[y][i]; 57 } 58 } 59 return f[x][0]; 60 } 61 62 main() 63 { 64 int i, j; 65 int k; 66 string s1, s2; 67 int t; 68 cin>>t; 69 while(t--){ 70 init(); 71 scanf("%d %d",&n,&m); 72 k=1; 73 for(i=1;i<n;i++){ 74 cin>>s1>>s2; 75 if(ma.find(s1)==ma.end()){ 76 ma[s1]=k++; 77 } 78 if(ma.find(s2)==ma.end()){ 79 ma[s2]=k++; 80 } 81 f[ma[s1]][0]=ma[s2]; 82 ve[ma[s2]].push_back(ma[s1]); 83 } 84 int root; 85 for(i=1;i<=n;i++){ 86 if(!f[i][0]){ 87 root=i;break; 88 } 89 } 90 bfs(root); 91 while(m--){ 92 cin>>s1>>s2; 93 int node=lca(ma[s1],ma[s2]); 94 if(ma[s1]==ma[s2]){ 95 printf("0\n"); 96 } 97 else if(node!=ma[s1]&&node!=ma[s2]) printf("%d\n",dep[ma[s1]]-dep[node]+1); 98 else if(node==ma[s1]) printf("1\n"); 99 else if(node==ma[s2]) printf("%d\n",dep[ma[s1]]-dep[ma[s2]]); 100 } 101 102 } 103 }
HDU 4547 LCA倍增算法,布布扣,bubuko.com
标签:des style blog color java os io strong
原文地址:http://www.cnblogs.com/qq1012662902/p/3907527.html