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 ...
随机推荐
- LeetCode OJ:Spiral Matrix(螺旋矩阵)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- LeetCode OJ:Valid Palindrome(验证回文)
Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...
- New Concept English three (37)
28 words/minute 44 typing errors We have learnt to expect that trains will be punctual. After years ...
- gethostbyname()函数
gethostbyname()函数说明——用域名或主机名获取IP地址 包含头文件 #include <netdb.h> #include <sys/socket.h> ...
- SPS和PPS有哪些重要的参数?
SPS: Level_idc: Bit_depth_luma_minus8: Bit_depth_chroma_minus8: Pic_order_cnt_type: Num_ref_frames: ...
- java String 转 Long 两种方法区别
Long.ValueOf("String")返回Long包装类型 包装类型: Byte,Integer,Short,Long,Boolean,Character,Float,Dou ...
- Leetcode 1013. Partition Array Into Three Parts With Equal Sum
简单题,暴力找出来就行. class Solution: def canThreePartsEqualSum(self, A: List[int]) -> bool: s = sum(A) if ...
- (一)Nginx正向代理与反向代理
引言:身为前端开发人员来说对于Nginx的作用或许很少听到,这个东西是后端使用的,Nginx对前端而言意味着什么,有什么用呢?大白会整理出几篇文章给大家细细道来. 1.正向代理的概念 正向代理,也就是 ...
- 【MFC】picture控件 两种有细微差别的动态加载图片方法
摘自:http://www.jizhuomi.com/software/193.html VS2010/MFC编程入门之二十七(常用控件:图片控件Picture Control) 分类标签: 编程入门 ...
- c++使用http协议上传文件到七牛云服务器
使用c++ http协议上传文件到七牛服务器时,比较搞的一点就是header的设置: "Content-Type:multipart/form-data;boundary=xxx" ...