POJ 1470 Closest Common Ancestors(离线tarjan-LCA)
来源:互联网 发布:知乎 可怕 编辑:程序博客网 时间:2024/05/18 10:40
Description
给出一棵节点数为n的树和q次查询,每次查询a和b的LCA,最后输出每个节点被查询的次数
Input
第一行为一整数n表示树的节点数,之后n行每行输入一个节点的邻接关系,然后是一整数q表示查询次数,最后q行每行两个整数a和b表示查询a和b的LCA
Output
如果某个节点作为查询中两个点的LCA,则输出其被查询的次数
Sample Input
5
5:(3) 1 4 2
1:(0)
4:(0)
2:(1) 3
3:(0)
6
(1 5) (1 4) (4 2)
(2 3)
(1 3) (4 3)
Sample Output
2:1
5:5
Solution
离线所有查询并求出LCA后统计次数即可
Code
#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>using namespace std;#define maxn 1111#define maxq 555555struct Edge{ int to,next;}edge[maxn*2];struct Query{ int to,next,id;}query[maxq*2];int f[maxn];bool vis[maxn];int ancestor[maxn];int head1[maxn],tot1;int head2[maxq],tot2;int lca[maxq]; void init(){ tot1=tot2=0; memset(head1,-1,sizeof(head1)); memset(head2,-1,sizeof(head2)); memset(vis,0,sizeof(vis)); memset(f,-1,sizeof(f)); memset(ancestor,0,sizeof(ancestor));}int find(int x){ if(f[x]==-1)return x; return f[x]=find(f[x]);}void unite(int x,int y){ x=find(x),y=find(y); if(x!=y)f[x]=y;}void add_edge(int u,int v){ edge[tot1].to=v; edge[tot1].next=head1[u]; head1[u]=tot1++;}void add_query(int u,int v,int id){ query[tot2].to=v; query[tot2].next=head2[u]; query[tot2].id=id; head2[u]=tot2++; query[tot2].to=u; query[tot2].next=head2[v]; query[tot2].id=id; head2[v]=tot2++;}void tarjan(int u){ ancestor[u]=u; vis[u]=1; for(int i=head1[u];~i;i=edge[i].next) { int v=edge[i].to; if(vis[v])continue; tarjan(v); unite(u,v); ancestor[find(u)]=u; } for(int i=head2[u];~i;i=query[i].next) { int v=query[i].to; if(vis[v])lca[query[i].id]=ancestor[find(v)]; }} int main(){ int n,q,root,flag[maxn],ans[maxn]; while(~scanf("%d",&n)) { init(); memset(flag,0,sizeof(flag)); memset(ans,0,sizeof(ans)); int u,v,num; for(int i=1;i<=n;i++) { scanf("%d:(%d)",&u,&num); while(num--) { scanf("%d",&v); add_edge(u,v),add_edge(v,u); flag[v]=1; } } scanf("%d",&q); for(int i=1;i<=q;i++) { char ch; cin>>ch; scanf("%d %d)",&u,&v); add_query(u,v,i); } for(int i=1;i<=n;i++) if(!flag[i]) { root=i; break; } tarjan(root); for(int i=1;i<=q;i++) ans[lca[i]]++; for(int i=1;i<=n;i++) if(ans[i])printf("%d:%d\n",i,ans[i]); } return 0;}
0 0
- poj 1470 Closest Common Ancestors(tarjan离线求lca)
- poj 1470 Closest Common Ancestors (离线LCA Tarjan)
- POJ 1470 Closest Common Ancestors(离线tarjan-LCA)
- poj 1470 Closest Common Ancestors 【Tarjan 离线 LCA】
- POJ 1470 Closest Common Ancestors【LCA Tarjan】
- (算法)Tarjan离线算法解决LCA问题 (附POJ 1470 Closest Common Ancestors 代码)
- (算法)Tarjan离线算法解决LCA问题 (附POJ 1470 Closest Common Ancestors 代码)
- POJ - 1470 Closest Common Ancestors(LCA离线)
- [POJ 1470]Closest Common Ancestors[离线LCA]
- poj 1470 Closest Common Ancestors LCA离线
- POJ 1470 Closest Common Ancestors 离线LCA
- poj 1470 Closest Common Ancestors(离线Tarjan算法)
- POJ 1470 Closest Common Ancestors (离线LCA,4级)
- POJ 1470Closest Common Ancestors tarjan算法求LCA
- POJ 1470 Closest Common Ancestors LCA Tarjan算法
- POJ 1470Closest Common Ancestors(LCA)
- POJ 1470 Closest Common Ancestors LCA离线Tarjin算法
- poj1470 Closest Common Ancestors Lca离线算法Tarjan
- c#中 foreach 用法
- java多线程的线程池
- 随便写写C++,看看Essential C++
- 操作系统三个简单的部分(Operating Systems in Three Easy Pieces)4.1
- Java并发编程:Callable、Future和FutureTask
- POJ 1470 Closest Common Ancestors(离线tarjan-LCA)
- 使用病毒库检查手机中已经安装的程序
- java ssh
- HDU 2187
- HDU 1846 (巴什博弈)
- A.c和B.c两个文件中使用了名字相同的static变量,编译的时候会不会有问题?这两个static变量存储在什么地方?
- 七层登录之机房收费系统
- iOS开发-进阶:iOS7中的多任务 - Background Fetch,Silent Remote Notifications,Background Transfer Service
- hdu5299 Circles Game