Closest Common Ancestors
Input
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 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
#include <iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#include<map>
#include<algorithm>
typedef long long ll;
using namespace std;
const int MAXN=1e3+;
int m,n;
int visit[MAXN];
int is_root[MAXN];
int str[MAXN];
int head[MAXN];//以第i条边为起点的最后输入的那个编号
int ans[MAXN];
int mp[MAXN][MAXN];
int cnt,root,x,y,cx,cy;
struct node
{
int to;//边的终点
int next;//与第i条边同起点的一条边的存储位置
int vi;//权值
}edge[MAXN];
void add_edge(int x,int y)
{
edge[cnt].to=y;
edge[cnt].next=head[x];
head[x]=cnt++;
}
void init()
{
cnt=;
memset(visit,,sizeof(visit));
memset(mp,,sizeof(mp));
memset(ans,,sizeof(ans));
memset(is_root,true,sizeof(is_root));
memset(head,-,sizeof(head));
for(int i=;i<=m;i++)
{
str[i]=i;
}
int p,k;
for(int i=;i<=m;i++)
{
scanf("%d:(%d)",&p,&k);
for(int j=;j<=k;j++)
{
scanf("%d",&x);
add_edge(p,x);
is_root[x]=false;
} }
for(int i=;i<=m;i++)//找根节点(入度为0的点)
{
if(is_root[i])
{
root=i;
break;
}
}
}
int Find(int x)
{
int temp=x;
while(temp!=str[temp])
{
temp=str[temp];
}
return temp;
}
void Unit(int x,int y)
{
int root1=Find(x);
int root2=Find(y);
if(root1!=root2)
{
str[y]=root1;
}
}
void LCA(int u)
{
for(int i=head[u];i!=-;i=edge[i].next)
{
int v=edge[i].to;
LCA(v);
Unit(u,v);
visit[v]=true;
}
for(int i=;i<=m;i++)//遍历图中所有点,找出与当前顶点u有关系的点,若该点i已访问,则找到v,i的lca;
{
if(visit[i]&&mp[u][i])
{
int k=Find(i);
ans[k]+=mp[u][i];
}
}
}
void solve()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf(" (%d%d)",&cx,&cy);
mp[cx][cy]++;
mp[cy][cx]++;
}
LCA(root);
}
void output()
{
for(int i=;i<=m;i++)
{
if(ans[i])
{
printf("%d:%d\n",i,ans[i]);
}
}
}
int main()
{
while(scanf("%d",&m)!=-)
{
init();
solve();
output();
}
return ;
}
Closest Common Ancestors的更多相关文章
- POJ 1470 Closest Common Ancestors
传送门 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 17306 Ac ...
- poj----(1470)Closest Common Ancestors(LCA)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 15446 Accept ...
- POJ 1470 Closest Common Ancestors(最近公共祖先 LCA)
POJ 1470 Closest Common Ancestors(最近公共祖先 LCA) Description Write a program that takes as input a root ...
- POJ 1470 Closest Common Ancestors (LCA,离线Tarjan算法)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 13372 Accept ...
- POJ 1470 Closest Common Ancestors (LCA, dfs+ST在线算法)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 13370 Accept ...
- POJ 1470 Closest Common Ancestors 【LCA】
任意门:http://poj.org/problem?id=1470 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000 ...
- poj1470 Closest Common Ancestors [ 离线LCA tarjan ]
传送门 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 14915 Ac ...
- BNUOJ 1589 Closest Common Ancestors
Closest Common Ancestors Time Limit: 2000ms Memory Limit: 10000KB This problem will be judged on PKU ...
- poj——1470 Closest Common Ancestors
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 20804 Accept ...
- Closest Common Ancestors POJ 1470
Language: Default Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissio ...
随机推荐
- 日常生活小技巧 -- 惠普 Windows10 进入安全模式
今天手贱,是真的很贱.将用户模式从管理员组改为标准用户 方法是:WIN+R 打开 control userpasswords2 然后出现了用户账户控制,你要允许此应用对你的设备进行更改吗?最关键的是没 ...
- Arcgis For Android之离线地图实现的几种方式
为什么要用,我想离线地图的好处是不言而喻的,所以很多人做系统的时候都会考虑用离线地图.在此,我给大家介绍几种Arcgis For Android下加载离线地图的方式. 在Arcgis For Andr ...
- 学习nodejs部分基础内容入门小结
Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境. Node.js 使用了一个事件驱动.非阻塞式 I/O 的模型,使其轻量又高效. Node.js 的包管理器 n ...
- ng 自定义过滤器的创建和使用
过滤器的本质就是一个方法,参数就是输入的值以及给过滤器指定的参数,返回值就是处理后要显示的值. ①过滤器创建var app = angular.module();app.filter(‘名称’,fun ...
- linux进程通信全面解析
进程IPC 的 7种方式 linux下 进程通讯IPC的方式主要有以下7种: 1.文件 2.共享内存 3.信号 4.管道 5.套接字 6.消息列队 7.信号量 以下正文 中 一一 分析下: 1 ...
- HashMap(HashSet)的实现
0. HashMap(TreeMAP).HashSet.HashTable 的关系 HashMap 的底层则维护着 Node<K, V>[] table; 一个一维数组用于快速访问(只在初 ...
- HihoCoder1325 : 平衡树·Treap(附STL版本)
平衡树·Treap 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho:小Hi,我发现我们以前讲过的两个数据结构特别相似. 小Hi:你说的是哪两个啊? 小Ho:就是二 ...
- HihoCoder 1053 : 居民迁移 二分+贪心+双指针(好题)
居民迁移 时间限制:3000ms 单点时限:1000ms 内存限制:256MB 描述 公元2411年,人类开始在地球以外的行星建立居住点.在第1326号殖民星上,N个居住点分布在一条直线上.为了方便描 ...
- Python Requests快速入门
迫不及待了吗?本页内容为如何入门Requests提供了很好的指引.其假设你已经安装了Requests.如果还没有, 去 安装 一节看看吧. 首先,确认一下: Requests 已安装 Requests ...
- LeetCode Optimal Division
原题链接在这里:https://leetcode.com/problems/optimal-division/description/ 题目: Given a list of positive int ...