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 ...
随机推荐
- 日志分析命令awk基础用法
awk awk是一个很好用的文本处理工具,相对于sed常用用作一整行的处理,awk则比较擅长将一行分成数个字段来处理.而在我们性能测试中,可以awk可以帮助我们造数,也可以帮助我们分析日志. 简单来说 ...
- LeetCode OJ:Rotate Image(旋转图片)
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- react: redux-devTools
import {composeWithDeTools} from 'redux-devtools-extension'; const bindMiddleware = middleware => ...
- Week03-Java学习笔记第三次作业
Week03-面向对象入门 1.本周学习总结 初学面向对象,会学习到很多碎片化的概念与知识.尝试学会使用思维导图将这些碎片化的概念.知识点组织起来.请使用工具画出本周学习到的知识点及知识点之间的联系. ...
- Leetcode 867. Transpose Matrix
class Solution: def transpose(self, A: List[List[int]]) -> List[List[int]]: return [list(i) for i ...
- 【MFC】对话框自带滚动条的使用
对话框自带滚动条的使用 摘自 http://wenku.baidu.com/link?url=aZe1zgBSBsf9xCYNpcz2fNGljmKxg372OVIGeJ7p6iRCWbbsertS7 ...
- 分类(类别/Category)与 类扩展(Extension)
一.分类(类别/Category) 1.适用范围 当你已经封装好了一个类(也可能是系统类.第三方库),不想在改动这个类了,可是随着程序功能的增加需要在类中增加一个方法,这时我们不必修改主类, ...
- CF311B Cats Transport
题意 Zxr960115 is owner of a large farm. He feeds m cute cats and employs p feeders. There's a straigh ...
- UVA11168 Airport
题意 PDF 分析 首先发现距离最短的直线肯定在凸包上面. 然后考虑直线一般方程\(Ax+By+C=0\),点\((x_0,y_0)\)到该直线的距离为 \[ \frac{|Ax_0+By_0+C|} ...
- datetimefield和datefield的区别
django创建关于时间的model时,有三个可选,datetimefield.datefield和timefield,这三个分别对应datetime.date.time对象,这三个对象都有共同的属性 ...