Closest Common Ancestors


Time Limit: 10 Seconds      Memory Limit: 32768 KB

Write a program that takes as input a rooted tree and a list of pairs of vertices. For each pair (u,v) the program determines the closest common ancestor of u and v in the tree. The closest common ancestor of two nodes u and v is the node w that is an ancestor of both u and v and has the greatest depth in the tree. A node can be its own ancestor (for example in Figure 1 the ancestors of node 2 are 2 and 5)

The data set starts with the tree description, in the form:

nr_of_vertices
vertex:(nr_of_successors) successor1 successor2 ... successorn
......

where vertices are represented as integers from 1 to n. The tree description is followed by a list of pairs of vertices, in the form:

nr_of_pairs
(u v) (x y) ...

The input contents several data sets (at least one).

Note that white-spaces (tabs, spaces and line breaks) can be used freely in the input.

For each common ancestor the program prints the ancestor and the number of pair for which it is an ancestor. The results are printed on the standard output on separate lines, in to the ascending order of the vertices, in the format: ancestor:times

For example, for the following tree:

the program input and output is:

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)

Output

2:1
5:5

题意

给出一颗树, n 次查询最近公共祖先,输出所有查询所涉及到顶点的次数,未涉及则不输出。

思路

LCA模板,在输入查询的时候,用scanf(" (%d,%d)",&x,&y);输入,注意"("左边有一个空格

不知道为什么在POJ过不了,又是TLE又是MLE又是RE的,UVA,ZOJ,CSU都能过

代码

#include <iostream>
#include <vector>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define ll long long
#define ull unsigned long long
#define ms(a,b) memset(a,b,sizeof(a))
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int maxn=1e6+10;
const int mod=1e9+7;
const int maxm=3e3+10;
const int maxq=1e6+10;
using namespace std;
struct Edge
{
int to,Next;
}edge[maxm<<1];
int head1[maxm];
int tot1;
int ans[maxm];
void add_edge(int u,int v)
{
edge[tot1].to=v;
edge[tot1].Next=head1[u];
head1[u]=tot1++;
}
struct Query
{
int to,Next;
int index;
}query[maxq];
int head2[maxm];
int tot2;
void add_query(int u,int v,int index)
{
query[tot2].to=v;
query[tot2].Next=head2[u];
query[tot2].index=index;
head2[u]=tot2++;
}
int f[maxm];
int find(int x)
{
if(f[x]!=x)
f[x]=find(f[x]);
return f[x];
}
void join(int x,int y)
{
int dx=f[x],dy=f[y];
if(dx!=dy)
f[dy]=dx;
}
bool vis[maxm];
int fa[maxm];
int num[maxm];
void LCA(int u)
{
fa[u]=u;
vis[u]=1;
for(register int i=head1[u];~i;i=edge[i].Next)
{
int v=edge[i].to;
if(vis[v])
continue;
LCA(v);
join(u,v);
fa[find(u)]=u;
}
for(register int i=head2[u];~i;i=query[i].Next)
{
int v=query[i].to;
if(vis[v])
ans[query[i].index]=fa[find(v)];
}
}
bool isroot[maxm];
inline void init(int n)
{
tot1=0;tot2=0;
ms(head1,-1);ms(head2,-1);
ms(vis,0);ms(isroot,true);
ms(num,0);
for(register int i=1;i<=n;i++)
f[i]=i;
}
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("/home/wzy/in.txt", "r", stdin);
freopen("/home/wzy/out.txt", "w", stdout);
srand((unsigned int)time(NULL));
#endif
int n;
int cnt,h,p;
while(scanf("%d",&n)==1)
{
init(n);
for(register int i=1;i<=n;i++)
{
scanf("%d:(%d)",&h,&cnt);
for(int j=0;j<cnt;j++)
scanf("%d",&p),add_edge(p,h),add_edge(h,p),isroot[p]=false;
}
int root;
for(register int i=1;i<=n;i++)
if(isroot[i])
root=i;
int q;
scanf("%d",&q);
int x,y;
for(register int i=0;i<q;i++)
{
scanf(" (%d,%d)",&x,&y);
add_query(x,y,i);add_query(y,x,i);
}
LCA(root);
for(register int i=0;i<q;i++)
num[ans[i]]++;
for(register int i=1;i<=n;i++)
if(num[i])
printf("%d:%d\n",i,num[i]);
}
#ifndef ONLINE_JUDGE
cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s."<<endl;
#endif
return 0;
}

ZOJ 1141:Closest Common Ancestors(LCA)的更多相关文章

  1. poj----(1470)Closest Common Ancestors(LCA)

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 15446   Accept ...

  2. ZOJ 1141 Closest Common Ancestors(LCA)

    注意:poj上的数据与zoj不同,第二处输入没有逗号 ' , ' 题意:输出测试用例中是最近公共祖先的节点,以及这个节点作为最近公共祖先的次数. 思路:直接求,两个节点一直往上爬,知道爬到同一个节点, ...

  3. POJ 1470 Closest Common Ancestors (LCA,离线Tarjan算法)

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 13372   Accept ...

  4. POJ 1470 Closest Common Ancestors (LCA, dfs+ST在线算法)

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 13370   Accept ...

  5. POJ:1330-Nearest Common Ancestors(LCA在线、离线、优化算法)

    传送门:http://poj.org/problem?id=1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K ...

  6. POJ 1330 Nearest Common Ancestors(lca)

    POJ 1330 Nearest Common Ancestors A rooted tree is a well-known data structure in computer science a ...

  7. 最近公共祖先 Least Common Ancestors(LCA)算法 --- 与RMQ问题的转换

    [简介] LCA(T,u,v):在有根树T中,询问一个距离根最远的结点x,使得x同时为结点u.v的祖先. RMQ(A,i,j):对于线性序列A中,询问区间[i,j]上的最值.见我的博客---RMQ - ...

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

    LCA思想:http://www.cnblogs.com/hujunzheng/p/3945885.html 在求解最近公共祖先为问题上,用到的是Tarjan的思想,从根结点开始形成一棵深搜树,非常好 ...

  9. POJ 1470 Closest Common Ancestors(最近公共祖先 LCA)

    POJ 1470 Closest Common Ancestors(最近公共祖先 LCA) Description Write a program that takes as input a root ...

随机推荐

  1. 自动化测试系列(三)|UI测试

    UI 测试是一种测试类型,也称为用户界面测试,通过该测试,我们检查应用程序的界面是否工作正常或是否存在任何妨碍用户行为且不符合书面规格的 BUG.了解用户将如何在用户和网站之间进行交互以执行 UI 测 ...

  2. 学习java的第十一天

    一.今日收获 1.学习java完全学习手册2.9.3循环结构的内容并验证例题 2.观看哔哩哔哩上的教学视频 二.今日问题 1.基本没有 三.明日目标 1.继续完成2.9.3循环结构的例题 2.哔哩哔哩 ...

  3. day08 索引的创建与慢查询优化

    day08 索引的创建与慢查询优化 昨日内容回顾 视图 视图:将SQL语句查询结果实体化保存起来,方便下次查询使用. 视图里面的数据来源于原表,视图只有表结构 # 创建视图 create view 视 ...

  4. Flume(四)【配置文件总结】

    目录 一.Agent 二.Source taildir arvo netstat exec spooldir 三.Sink hdfs kafka(待续) hbase(待续) arvo logger 本 ...

  5. Oracle—数据库名、数据库实例名、数据库域名、数据库服务名的区别

    Oracle-数据库名.数据库实例名.数据库域名.数据库服务名的区别 一.数据库名 1.什么是数据库名       数据库名就是一个数据库的标识,就像人的身份证号一样.他用参数DB_NAME表示,如果 ...

  6. 转 Android Studio中Junit调试

    转:https://blog.csdn.net/xanthus_li/article/details/54314189 在程序开发完成后,需要交给专业的调试人员进行相关的专业调试(白盒测试,黑盒测试, ...

  7. adb命令对app进行测试

    1.何为adb adb android  debug  bridge ,sdk包中的工具,将Platform-tooks 和tools  两个路径配置到环境变量中 2.SDK下载链接:http://t ...

  8. Linux基础命令---get获取ftp文件

    get 使用lftp登录ftp服务器之后,可以使用get指令从服务器获取文件.   1.语法       get [-E]  [-a]  [-c] [-O base]  rfile  [-o lfil ...

  9. Oracle 学习PL/SQL

    先上一张实用的图:用于转义字符的. SQL> select chr(42) ||'is what?' from dual; CHR(42)||---------*is what? 想转义哪个就转 ...

  10. Dubbo管控平台

    2019年初,官方发布了Dubbo管理控制台0.1版本.结构上采取了前后端分离的方式,前端使用Vue和Vuetify分别作为Javascript框架和UI框架,后端采用Spring Boot框架 一. ...