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 ...
随机推荐
- js不执行的问题
项目中有两个页面,调用的一个js引用都正确,一个js能用,一个没反应,瞅了半天 没看出什么名堂.最后发现一个页面只有一个 <script type="text/javascript&q ...
- 2017.11.16 STM8L052 温度控制器
1 J-link和ST-link的兼容性 STM8只能用ST-link.J-link兼容所有的(大部分而已)的ARM内核IC mark: http://bbs.eeworld.com.cn/thre ...
- mac上安装mongoDb以及简单使用
年初粗略学习了下node,这好几个月没玩,突然发现已经忘完了,还是简单记录下基本知识,方便再次使用时资料查找. 一.mongoDb安装 在mac上安装了brew的情况下,可以直接执行命令 brew i ...
- 机器学习算法实现解析——libFM之libFM的训练过程概述
本节主要介绍的是libFM源码分析的第四部分--libFM的训练. FM模型的训练是FM模型的核心的部分. 4.1.libFM中训练过程的实现 在FM模型的训练过程中,libFM源码中共提供了四种训练 ...
- OSPF 配置
封装在IP层:协议号 89 hello时间是dead时间的1/4 224.0.0.5 .在点到点网络, 比如T1线路,是连接单独的一对路由器的网络, 点到点网络上的有效邻居总是可以形成邻接关系的,在这 ...
- (效果二)js实现两个变量值的交换
ES5: var a = 12,b=13,c; c = a; a = b; b = c; console.log(a,b);//13,12 通过设置第三方变量交换赋值来实现 ES6 var a = ...
- (十一)java循环结构
while(循环的条件) {循环的语句} int a = 1; while(a < 5) { System.out.println(a);//1,2,3,4 a++; } System.out. ...
- notebook查找文件
- 感觉有变良好的第一次电面——yahoo北京测试实习生
一个月之前投的岗位.都已经忘了.昨天突然接到电话说今天下午3点电面. 立马又开始忐忑起来,整理了下项目啊,推荐系统相关知识啥的,跑到欧巴桑寝室去电面电面. 3点很准时的电话来了,是个女面试官. 一上来 ...
- margin和padding理解
W3C组织建议把所有网页上的对像都放 在一个盒(box)中,设计师可以通过创建定义来控制这个盒的属性,这些对像包括段落.列表.标题.图片以及层. 盒模型主要定义四个区域:内容 (content).边框 ...