【LCA】 POJ 1470 Closest Common Ancestors
来源:互联网 发布:matte painting软件 编辑:程序博客网 时间:2024/05/20 07:34
给出一棵树 再给出m个询问 求m个询问后
最近每个点成为公共祖先的次数 (输出大于0次的)
/** LCA离线算法,Tarjan* 复杂度O(n+Q);*/#include <cstdio>#include <cstring>#include <cstdlib>#include <string>#include <iostream>#include <algorithm>#include <sstream>#include <cmath>using namespace std;#include <queue>#include <stack>#include <vector>#include <deque>#define cler(arr, val) memset(arr, val, sizeof(arr))#define FOR(i,a,b) for(int i=a;i<=b;i++)#define IN freopen ("in.txt" , "r" , stdin);#define OUT freopen ("out.txt" , "w" , stdout);typedef long long LL;const int MAXN = 1000+5;const int MAXM = 550000;const int INF = 0x3f3f3f3f;const int mod = 1000000007;int F[MAXN];//需要初始化为-1int find(int x){ if(F[x] == -1)return x; return F[x] = find(F[x]);}void bing(int u,int v){ int t1 = find(u); int t2 = find(v); if(t1 != t2) F[t1] = t2;}//************************bool vis[MAXN];//访问标记int ancestor[MAXN];//祖先struct Edge{ int to,next;} edge[MAXN*2];int head[MAXN],tot;void addedge (int u,int v){ edge[tot].to = v; edge[tot].next = head[u]; head[u] = tot++;}struct Query{ int q,next; int index;//查询编号} query[MAXM*2];int answer[MAXM];//存储最后的查询结果,下标0~Q-1int h[MAXM];int tt;int Q;void add_query(int u,int v,int index){ query[tt].q = v; query[tt].next = h[u]; query[tt].index = index; h[u] = tt++; query[tt].q = u; query[tt].next = h[v]; query[tt].index = index; h[v] = tt++;}void init(){ tot = 0; memset(head,-1,sizeof (head)); tt = 0; memset(h,-1,sizeof(h)); memset(vis,false,sizeof(vis)); memset(F,-1,sizeof(F)); memset(ancestor,0,sizeof(ancestor));}void LCA(int u){ ancestor[u] = u; vis[u] = true; for(int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].to; if(vis[v])continue; LCA(v); bing(u,v); ancestor[find(u)] = u; } for(int i = h[u]; i != -1; i = query[i].next) { int v = query[i]. q; if(vis[v]) { answer[query[i].index] = ancestor[find(v)]; } }}bool flag[MAXN];int Count_num[MAXN];int main(){#ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout);#endif int k,u,v; int n,m; while(scanf("%d",&n)!=EOF) { init(); cler(flag,false); for(int i=0; i<n; i++) { scanf("%d:(%d)",&u,&k); for(int i=0; i<k; i++) { scanf("%d",&v); flag[v]=true; addedge(u,v); addedge(v,u); } } scanf("%d",&m); for(int i=0; i<m; i++) { char s; cin>>s; scanf("%d %d)",&u,&v); add_query(u,v,i); } int root; for(int i=1; i<=n; i++) { if(!flag[i]) { root=i; break; } } LCA(root); cler(Count_num,0); for(int i=0; i<m; i++) Count_num[answer[i]]++; for(int i=1; i<=n; i++) if(Count_num[i]>0) printf("%d:%d\n",i,Count_num[i]); } return 0;}
0 0
- POJ-1470 Closest Common Ancestors【LCA】
- POJ-1470 Closest Common Ancestors【LCA】
- POJ 1470 Closest Common Ancestors [LCA+RMQ]
- poj 1470 Closest Common Ancestors(LCA)
- poj--1470--Closest Common Ancestors--LCA
- POJ 1470 Closest Common Ancestors LCA题解
- Closest Common Ancestors - POJ 1470 LCA
- poj 1470 Closest Common Ancestors(LCA)
- 【LCA】 POJ 1470 Closest Common Ancestors
- poj 1470 Closest Common Ancestors(LCA)
- POJ - 1470 Closest Common Ancestors(LCA离线)
- POJ 1470Closest Common Ancestors(LCA)
- POJ 1470 Closest Common Ancestors【LCA Tarjan】
- [POJ 1470]Closest Common Ancestors[离线LCA]
- poj 1470 Closest Common Ancestors LCA离线
- POJ 1470 Closest Common Ancestors (LCA)
- 【LCA基础】poj 1470 Closest Common Ancestors
- POJ 1470 Closest Common Ancestors 离线LCA
- asp.net中Page.ClientScript.RegisterStartupScript用法
- android res文件夹说明
- 程序猿崛起
- windows驱动程序开发之环境搭建1 --windows符号文件
- Android笔记进程优先级
- 【LCA】 POJ 1470 Closest Common Ancestors
- 安卓中从一个Activity启动另一个Activity
- iPhone&iPad DFU及恢复模式刷机、降级教程
- Android4.4——service之bindService
- Android之Fragment学习心得
- 使用rman创建及更新一个复制库
- JSON-RPC for JAVA
- 深度揭秘:微软内部爱恨纠葛
- Cocos2d-x 3.x 事件分发机制