vector模拟邻接表:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<vector> 6 #include<queue> 7 #define eps 1e-8 8 #define memset(a,v) memset(a,v,sizeof(a)) 9 using namespace std; 10 typedef long long int LL; 11 const int MAXL(1e4); 12 const int INF(0x7f7f7f7f); 13 const int mod(1e9+7); 14 int dir[4][2]= {{-1,0},{1,0},{0,1},{0,-1}}; 15 int father[MAXL+50]; 16 bool is_root[MAXL+50]; 17 bool vis[MAXL+50]; 18 vector<int>v[MAXL+50]; 19 int root; 20 int cx,cy; 21 int ans; 22 int Find(int x) 23 { 24 if(x!=father[x]) 25 father[x]=Find(father[x]); 26 return father[x]; 27 } 28 29 void Join(int x,int y) 30 { 31 int fx=Find(x),fy=Find(y); 32 if(fx!=fy) 33 father[fy]=fx; 34 } 35 36 void LCA(int u) 37 { 38 for(int i=0; i<v[u].size(); i++) 39 { 40 int child=v[u][i]; 41 if(!vis[child]) 42 { 43 LCA(child); 44 Join(u,child); 45 vis[child]=true; 46 } 47 } 48 if(u==cx&&vis[cy]==true) 49 ans=Find(cy); 50 if(u==cy&&vis[cx]==true) 51 ans=Find(cx); 52 53 } 54 55 void init() 56 { 57 memset(is_root,true); 58 memset(vis,false); 59 int n; 60 scanf("%d",&n); 61 for(int i=0; i<=n; i++) 62 v[i].clear(); 63 for(int i=1; i<=n; i++) 64 father[i]=i; 65 for(int i=1; i<n; i++) 66 { 67 int x,y; 68 scanf("%d%d",&x,&y); 69 v[x].push_back(y); 70 is_root[y]=false; 71 } 72 scanf("%d%d",&cx,&cy); 73 for(int i=1; i<=n; i++) 74 { 75 if(is_root[i]==true) 76 { 77 root=i; 78 break; 79 } 80 } 81 82 } 83 int main() 84 { 85 int T; 86 scanf("%d",&T); 87 while(T--) 88 { 89 init(); 90 LCA(root); 91 cout<<ans<<endl; 92 } 93 } 94
链式前向星写法:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<vector> 6 #include<queue> 7 #define eps 1e-8 8 #define memset(a,v) memset(a,v,sizeof(a)) 9 using namespace std; 10 typedef long long int LL; 11 const int MAXL(1e6); 12 const int INF(0x7f7f7f7f); 13 const int mod(1e9+7); 14 int dir[4][2]= {{-1,0},{1,0},{0,1},{0,-1}}; 15 struct node 16 { 17 int to; 18 int next; 19 }edge[MAXL+50]; 20 int head[MAXL+50]; 21 int father[MAXL+50]; 22 bool vis[MAXL+50]; 23 bool is_root[MAXL+50]; 24 int n; 25 int cnt; 26 int cx,cy; 27 int ans; 28 int root; 29 30 31 int Find(int x) 32 { 33 if(x!=father[x]) 34 father[x]=Find(father[x]); 35 return father[x]; 36 } 37 38 void Join(int x,int y) 39 { 40 int fx=Find(x),fy=Find(y); 41 if(fx!=fy) 42 father[fy]=fx; 43 } 44 45 void add_edge(int x,int y) 46 { 47 edge[cnt].to=y; 48 edge[cnt].next=head[x]; 49 head[x]=cnt++; 50 } 51 52 void init() 53 { 54 cnt=0; 55 memset(head,-1); 56 memset(vis,false); 57 memset(is_root,true); 58 scanf("%d",&n); 59 for(int i=0;i<=n;i++) 60 father[i]=i; 61 for(int i=1;i<n;i++) 62 { 63 int x,y; 64 scanf("%d%d",&x,&y); 65 add_edge(x,y); 66 is_root[y]=false; 67 } 68 for(int i=1;i<=n;i++) 69 if(is_root[i]==true) 70 root=i; 71 } 72 73 void LCA(int u) 74 { 75 for(int i=head[u];~i;i=edge[i].next) 76 { 77 int v=edge[i].to; 78 LCA(v); 79 Join(u,v); 80 vis[v]=true; 81 82 } 83 if(cx==u&&vis[cy]==true) 84 ans=Find(cy); 85 if(cy==u&&vis[cx]==true) 86 ans=Find(cx); 87 } 88 void solve() 89 { 90 scanf("%d%d",&cx,&cy); 91 LCA(root); 92 } 93 int main() 94 { 95 int T; 96 scanf("%d",&T); 97 while(T--) 98 { 99 init(); 100 solve(); 101 cout<<ans<<endl; 102 } 103 } 104
转自:https://blog.csdn.net/baiyi_destroyer/article/details/81363221
来源:https://www.cnblogs.com/baiyi-destroyer/p/9415278.html