传送门

Time Limit: 2000MS   Memory Limit: 10000K
Total Submissions: 14915   Accepted: 4745

Description

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)

Input

The data set, which is read from a the std input, 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 ( n <= 900 ). The tree description is followed by a list of pairs of vertices, in the form: 
nr_of_pairs 
(u v) (x y) ...

The input file contents several data sets (at least one). 
Note that white-spaces (tabs, spaces and line breaks) can be used freely in the input.

Output

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: 

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

Hint

Huge input, scanf is recommended.

Source

 
 
万分感谢万能的kss,几句话就教会了窝,以下题意及题解均转自kss博客:
http://blog.csdn.net/u011645923/article/details/35780547
 
题意:给定一个n个结点的有向树,m次询问(u,v)的 lca,输出询问中作为lca的点以及作为lca的次数。

思路:离线lca ,利用tarjan算法。

tarjan算法的步骤是(当dfs到节点u时):
1. 在并查集中建立仅有u的集合,设置该集合的祖先为u
2. 对u的每个孩子v:
   2.1 tarjan之
   2.2 合并v到父节点u的集合,确保集合的祖先是u
3. 设置u为已遍历
4. 处理关于u的查询,若查询(u,v)中的v已遍历过,则LCA(u,v)=v所在的集合的祖先

14006525 njczy2010 1470 Accepted 2992K 516MS G++ 1979B 2015-03-25 19:29:20
 #include <cstdio>
#include <cstring>
#include <stack>
#include <vector>
#include <algorithm> #define ll long long
int const N = ;
int const M = ;
int const inf = ;
ll const mod = ; using namespace std; int n,m;
vector<int> bian[N];
vector<int> query[N];
int cnt[N];
int fa[N];
int vis[N];
int degree[N]; int findfa(int x)
{
return fa[x] == x ? fa[x] : fa[x] = findfa(fa[x]);
} void ini()
{
int i,j;
int k,u,v;
memset(cnt,,sizeof(cnt));
memset(vis,,sizeof(vis));
memset(degree,,sizeof(degree));
for(i=;i<=n;i++){
bian[i].clear();
query[i].clear();
fa[i]=i;
}
for(i=;i<=n;i++){
scanf("%d:(%d)",&u,&k);
for(j=;j<k;j++){
scanf("%d",&v);
bian[u].push_back(v);
degree[v]++;
}
}
scanf("%d",&m);
for(i=;i<=m;i++){
while(getchar()!='(') ;
scanf("%d%d",&u,&v);
while(getchar()!=')') ;
query[u].push_back(v);
query[v].push_back(u);
}
} void tarjan(int u,int f)
{
vector<int>::iterator it;
int v;
for(it=bian[u].begin();it!=bian[u].end();it++){
v=*it;
tarjan(v,u);
} for(it=query[u].begin();it!=query[u].end();it++){
v=*it;
if(vis[v]==) continue;
cnt[ findfa(v) ]++;
}
vis[u]=;
fa[u]=f;
} void solve()
{
int i;
for(i=;i<=n;i++){
if(degree[i]==){
tarjan(i,-);
}
}
} void out()
{
int i;
for(i=;i<=n;i++){
// printf("%d:%d\n",i,cnt[i]);
if(cnt[i]!=){
printf("%d:%d\n",i,cnt[i]);
}
}
} int main()
{
//freopen("data.in","r",stdin);
//scanf("%d",&T);
// for(cnt=1;cnt<=T;cnt++)
//while(T--)
while(scanf("%d",&n)!=EOF)
{
ini();
solve();
out();
}
}

poj1470 Closest Common Ancestors [ 离线LCA tarjan ]的更多相关文章

  1. POJ 1470 Closest Common Ancestors【LCA Tarjan】

    题目链接: http://poj.org/problem?id=1470 题意: 给定若干有向边,构成有根数,给定若干查询,求每个查询的结点的LCA出现次数. 分析: 还是很裸的tarjan的LCA. ...

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

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

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

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

  4. POJ 1470 Closest Common Ancestors 【LCA】

    任意门:http://poj.org/problem?id=1470 Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000 ...

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

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

  6. ZOJ 1141:Closest Common Ancestors(LCA)

    Closest Common Ancestors Time Limit: 10 Seconds      Memory Limit: 32768 KB Write a program that tak ...

  7. POJ1470 Closest Common Ancestors

    LCA问题,用了离线的tarjan算法.输入输出参考了博客http://www.cnblogs.com/rainydays/archive/2011/06/20/2085503.htmltarjan算 ...

  8. POJ 1470 Closest Common Ancestors (模板题)(Tarjan离线)【LCA】

    <题目链接> 题目大意:给你一棵树,然后进行q次询问,然后要你统计这q次询问中指定的两个节点最近公共祖先出现的次数. 解题分析:LCA模板题,下面用的是离线Tarjan来解决.并且为了代码 ...

  9. POJ1470 Closest Common Ancestors 【Tarjan的LCA】

    非常裸的模版题,只是Tarjan要好好多拿出来玩味几次 非常有点巧妙呢,tarjan,大概就是当前结点和它儿子结点的羁绊 WA了俩小时,,,原因是,这个题是多数据的(还没告诉你T,用scanf!=EO ...

随机推荐

  1. cordova安装方法

    安装cordova之前需要先安装node.js和npm 然后在命令行运行:npm install -g cordova即可安装到最新版本的cordova 如果要安装指定版本,命令为:npm insta ...

  2. iOS 动画(基于Lottie封装)

    一般app中都会带有动画,而如果是一些复杂的动画,不但实现成本比较高,而且实现效果可能还不能达到UI想要的效果,于是我们可以借助lottie来完成我们想要的动画.   lottie动画1.gif   ...

  3. 位bit,字节byte,K,M,G(转)

      字节是由8个位所组成,可代表一个字符(A~Z).数字(0~9).或符号(,.?!%&+-*/),是内存储存数据的基本单位.1 byte = 8 bit 1 KB = 1024 bytes1 ...

  4. gulp自动化构建工具使用

    gulpfile.js: var gulp = require("gulp"); var imagemin = require("gulp-imagemin") ...

  5. Git 版本控制系统的基本使用、常用操作

    以Ubuntu16.04操作系统为例(其他系统类似),主要记录常用的.基本操作: 0. 安装Git 分散型版本控制系统(CVS): sudo apt-get install git 1. 初始化本地配 ...

  6. Python3简明教程(九)—— 文件处理

    文件是保存在计算机存储设备上的一些信息或数据.你已经知道了一些不同的文件类型,比如你的音乐文件,视频文件,文本文件.Linux 有一个思想是“一切皆文件”,这在实验最后的 lscpu 的实现中得到了体 ...

  7. 在CNN网络中roi从原图映射到feature map中的计算方法

    在使用fast rcnn以及faster rcnn做检测任务的时候,涉及到从图像的roi区域到feature map中roi的映射,然后再进行roi_pooling之类的操作.比如图像的大小是(600 ...

  8. c++ vector容器遍历方式

    #include <vector> #include <iostream> class Test { public: int a; int b; int c; Test() { ...

  9. 03pandas

    一.pandas简述 1)pandas是一个开源的,BSD许可的库,为Python编程语言提供高性能,易于使用的数据结构和数据分析工具. 2)numpy能够帮助我们处理数值,但是pandas除了处理数 ...

  10. (转)浅谈trie树

    浅谈Trie树(字典树)         Trie树(字典树) 一.引入 字典是干啥的?查找字的. 字典树自然也是起查找作用的.查找的是啥?单词. 看以下几个题: 1.给出n个单词和m个询问,每次询问 ...