当前位置: 首页 > news >正文

POJ 1470 Closest Common Ancestors

传送门

Closest Common Ancestors
Time Limit: 2000MS Memory Limit: 10000K
Total Submissions: 17306 Accepted: 5549

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

Southeastern Europe 2000

-----------------------------------------------------------------------

LCA

采用 Tarjan 离线 LCA 算法比较方便

注意读入细节

-------------------------------------------------------------------------

 

#include <cstdio>
#include <vector>
#include <cstring>
#define pb push_back

using namespace std;
const int N(905);
vector<int> q[N], g[N];
int par[N], ans[N], col[N];
int find(int u){return par[u]==u?u:find(par[u]);}
void dfs(int u, int f){
	col[u]=-1;
	for(int i=0; i<q[u].size(); i++){
		int &v=q[u][i];
		if(col[v]==-1) ans[v]++;
		else if(col[v]==1) ans[find(v)]++;
		else q[v].pb(u);
	}
	for(int i=0; i<g[u].size(); i++){
		int &v=g[u][i];
		dfs(v, u);
	}
	col[u]=1;
	par[u]=f;
}
int main(){
	//freopen("in", "r", stdin);
	int n, m, u, v;
	for(;~scanf("%d", &n);){
		for(int i=1; i<=n; i++) g[i].clear(), q[i].clear();
		memset(par, 0, sizeof(par));
		for(int i=0; i<n; i++){
			scanf("%d:(%d)", &u, &m);
			while(m--){
				scanf("%d", &v);
				par[v]=u;
				g[u].pb(v);
			}
		}
		scanf("%d", &m);
		while(m--){
			scanf(" (%d%d)", &u, &v);
			q[u].pb(v);
		}
		int rt;
		for(rt=1; par[rt]; rt=par[rt]);
		for(int i=1; i<=n; i++) par[i]=i;
		memset(ans, 0, sizeof(ans));
		memset(col, 0, sizeof(col));
		dfs(rt, rt);
		for(int i=1; i<=n; i++) if(ans[i]) printf("%d:%d\n", i, ans[i]);
	}
}

 

转载于:https://www.cnblogs.com/Patt/p/4728270.html

相关文章:

  • S3C2440-中文手册
  • URAL 1779 F - The Great Team 构造
  • 如何应用混沌进行置乱
  • Ruby源文件指引
  • poj 2828 块状链表 OR 线段树 OR 树状数组
  • Ruby用6行搞定P2P
  • Bootstrap中面板的使用
  • LCA rmq st model
  • 一个有意思的Ruby脚本
  • 如何提醒客户重载父类的指定方法?
  • 将键盘的按键转换成相应的Unicode 值
  • sqlserver 锁表语句分享
  • 产品版本改造中的项目管理
  • 一种人吃蜂蜜火上浇油
  • windows 特殊文件后缀集合
  • CAP 一致性协议及应用解析
  • es6
  • Github访问慢解决办法
  • Java IO学习笔记一
  • Java教程_软件开发基础
  • Java新版本的开发已正式进入轨道,版本号18.3
  • mysql 5.6 原生Online DDL解析
  • 创建一种深思熟虑的文化
  • 多线程 start 和 run 方法到底有什么区别?
  • 使用 Xcode 的 Target 区分开发和生产环境
  • 使用权重正则化较少模型过拟合
  • 王永庆:技术创新改变教育未来
  • 正则学习笔记
  • 湖北分布式智能数据采集方法有哪些?
  • 通过调用文摘列表API获取文摘
  • ​中南建设2022年半年报“韧”字当头,经营性现金流持续为正​
  • #{}和${}的区别?
  • (06)Hive——正则表达式
  • (1/2)敏捷实践指南 Agile Practice Guide ([美] Project Management institute 著)
  • (4)STL算法之比较
  • (42)STM32——LCD显示屏实验笔记
  • (Redis使用系列) Springboot 使用redis实现接口Api限流 十
  • (zz)子曾经曰过:先有司,赦小过,举贤才
  • (安卓)跳转应用市场APP详情页的方式
  • (附源码)ssm教师工作量核算统计系统 毕业设计 162307
  • (十六)串口UART
  • (一)VirtualBox安装增强功能
  • ./和../以及/和~之间的区别
  • .“空心村”成因分析及解决对策122344
  • .NET Core 实现 Redis 批量查询指定格式的Key
  • .NET Core 中的路径问题
  • .NET Remoting学习笔记(三)信道
  • .NET/C# 反射的的性能数据,以及高性能开发建议(反射获取 Attribute 和反射调用方法)
  • .Net程序猿乐Android发展---(10)框架布局FrameLayout
  • .NET建议使用的大小写命名原则
  • .NET框架设计—常被忽视的C#设计技巧
  • .net企业级架构实战之7——Spring.net整合Asp.net mvc
  • .NET学习全景图
  • .pub是什么文件_Rust 模块和文件 - 「译」
  • @Resource和@Autowired的区别