poj1330Nearest Common Ancestors 1470 Closest Common Ancestors(LCA算法)

LCA思想:http://www.cnblogs.com/hujunzheng/p/3945885.html

在求解最近公共祖先为问题上,用到的是Tarjan的思想,从根结点开始形成一棵深搜树,非常好的处理技巧就是在回溯到结点u的时候,u的子树已经遍历,这时候才把u结点放入合并集合中,
这样u结点和所有u的子树中的结点的最近公共祖先就是u了,u和还未遍历的所有u的兄弟结点及子树中的最近公共祖先就是u的父亲结点。以此类推。。这样我们在对树深度遍历的时候就很自然的将树中的结点分成若干的集合,两个集合中的所属不同集合的任意一对顶点的公共祖先都是相同的,也就是说这两个集合的最近公共最先只有一个。对于每个集合而言可以用并查集来优化,时间复杂度就大大降低了,为O(n + q),n为总结点数,q为询问结点对数。

 1 /*
 2     题意很明显,就是求LCA!
 3 */
 4 #include<iostream>
 5 #include<cstdio>
 6 #include<cstring>
 7 #include<algorithm>
 8 #include<vector>
 9 #define N 10005
10 using namespace std;
11 
12 int n;
13 int x, y;
14 vector<int>g[N];
15 int f[N];
16 int vis[N], cnt[N];
17 int ret;
18 
19 int getFather(int x){
20    return x==f[x] ? x : f[x]=getFather(f[x]);
21 }
22 
23 bool LCA(int u){
24     vis[u]=1;
25     f[u]=u;
26     int len=g[u].size();
27     
28     if(u==x && vis[y]){
29         ret=getFather(y);
30         return true;
31     }
32 
33     else if(u==y && vis[x]){
34         ret=getFather(x);
35         return true;
36     }
37 
38     for(int i=0; i<len; ++i){
39         int v=g[u][i];
40         if(!vis[v]){
41             if(LCA(v)) return true;
42             f[v]=u;
43         }
44     }
45     return false;
46 }
47 
48 int main(){
49     int t;
50     scanf("%d", &t);
51     while(t--){
52         scanf("%d", &n);
53         memset(cnt, 0, sizeof(cnt));
54         for(int i=1; i<n; ++i){
55             int u, v;
56             scanf("%d%d", &u, &v);
57             g[u].push_back(v);
58             ++cnt[v];
59         }
60         memset(vis, 0, sizeof(vis));
61         scanf("%d%d", &x, &y);
62         for(int i=1; i<=n; ++i)
63             if(cnt[i]==0){
64                 LCA(i);
65                 break;
66             }
67         printf("%d
", ret);
68         for(int i=1; i<=n; ++i)
69             g[i].clear();
70     }
71     return 0;
72 }
73  
View Code
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<algorithm>
 5 #include<vector>
 6 #define N 905
 7 #define M 25000
 8 using namespace std;
 9 
10 vector<int>g[N];
11 vector<int>p[M];
12 int cnt[N];
13 int f[N];
14 int vis[N];
15 int ans[N];
16 
17 int n, m;
18 
19 int getFather(int x){
20    return x==f[x] ? x : f[x]=getFather(f[x]);
21 }
22 
23 void LCA(int u){
24     f[u]=u;
25     vis[u]=1;
26     int len;
27     
28     len=p[u].size();
29     for(int i=0; i<len; ++i){
30         int v=p[u][i];
31         if(vis[v])
32               ++ans[getFather(v)];
33     }
34     
35     len=g[u].size();
36     for(int i=0; i<len; ++i){
37            int v=g[u][i];
38            if(!vis[v]){
39             LCA(v);
40             f[v]=u;   
41         }
42     }
43 }
44 
45 int main(){
46       while(scanf("%d", &n)!=EOF){
47             memset(cnt, 0, sizeof(cnt));
48           for(int i=1; i<=n; ++i){
49                   vis[i]=0;
50                   ans[i]=0;
51                   int a, d, b;
52                   char ch;
53                   scanf("%d", &a);
54                   while(scanf("%c", &ch) && ch!='(');
55                   scanf("%d", &d);
56                   while(scanf("%c", &ch) && ch!=')');
57                   while(d--){
58                    scanf("%d", &b);
59                    ++cnt[b];
60                    g[a].push_back(b);
61                 }
62           }
63           scanf("%d", &m);
64           while(m--){
65                char ch;
66              while(scanf("%c", &ch) && ch!='(');
67              int u, v;
68              scanf("%d%d", &u, &v);
69              p[u].push_back(v);
70              p[v].push_back(u);
71              while(scanf("%c", &ch) && ch!=')');
72           }
73 
74           for(int i=1; i<=n; ++i)
75              if(cnt[i]==0){
76                 LCA(i);
77                 break;
78             }
79          for(int i=1; i<=n; ++i)
80             if(ans[i]!=0)
81                 printf("%d:%d
", i, ans[i]);
82                 
83          for(int i=1; i<=n; ++i)
84             g[i].clear(), p[i].clear();
85     }
86     return 0;
87 }
View Code
原文地址:https://www.cnblogs.com/hujunzheng/p/3945593.html