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 ...
随机推荐
- Codeforces Round #394 (Div. 2) E. Dasha and Puzzle
E. Dasha and Puzzle time limit per test:2 seconds memory limit per test:256 megabytes input:standard ...
- SpringCloud教程 | 第十一篇: docker部署spring cloud项目
版权声明:本文为博主原创文章,欢迎转载,转载请注明作者.原文超链接 ,博主地址:http://blog.csdn.net/forezp. http://blog.csdn.net/forezp/art ...
- 【网络】<网络是怎样连接的>笔记
[一] 浏览器 http://user:pwd@hosturl:port/dir/of/file 基本思路: 1.1 生成http请求信息 包含“对什么”“进行怎样的操作”两个方法.一般常用操作是GE ...
- Windows GVLK密钥对照表(KMS激活专用
以下key来源于微软官网:https://technet.microsoft.com/en-us/library/jj612867.aspx Windows Server 2016 操作系统 KMS激 ...
- 前端之jQuery02
文档操作 重点:创建标签,jQuery里面没有这个方法 内部(子标签) 添加到指定元素内部后面 $(A).append(B): // B作为A的最后一个儿子元素:(把B追加到A) $(A).appen ...
- hashlib摘要算法模块,logging日志,configparser配置文件模块
一.hashlib模块(摘要算法模块) 1.算法介绍 Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 什么是摘要算法呢? 摘要算法又称哈希算法.散列算法.它通过一个函数,把 ...
- input光标位置不居中问题
文本输入框默认在谷歌,火狐浏览器中,光标是居中显示的.但在IE7中一开始会在顶部闪烁(输入文字后光标居中),加上行高就可以,值为文本框的高度. 注意要加*号,否则在谷歌浏览其中光标会在顶部闪烁. *l ...
- 1132. Cut Integer (20)
Cutting an integer means to cut a K digits long integer Z into two integers of (K/2) digits long int ...
- 【模板】NOIP模板汇总
图论 数据结构 数学 其他: 洛谷模板:a,b两个字符串,求b串在a串中出现的位置 #include<iostream> #include<cstdio> #include&l ...
- vba打开excel文件遍历sheet的名字和指定单元格的值
今天项目上有个应用,获取指定Excel文件下的所有sheet的名称以及当前sheet中指定单元格的值,并把他们写到固定的sheet中去,看了下,文件比较多,而且每个文件sheet的个数比较多,也不一样 ...