BNUOJ 1589 Closest Common Ancestors
Closest Common Ancestors
This problem will be judged on PKU. Original ID: 1470
64-bit integer IO format: %lld Java class name: Main
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
Source
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <climits>
#include <algorithm>
#include <cmath>
#define LL long long
#define INF 0x3f3f3f
using namespace std;
const int maxn = ;
vector<int>g[maxn];
vector<int>q[maxn];
int n,m,cnt[maxn],uf[maxn];
bool vis[maxn],indeg[maxn];
int Find(int x) {
if(x != uf[x])
uf[x] = Find(uf[x]);
return uf[x];
}
void tarjan(int u) {
int i;
uf[u] = u;
for(i = ; i < g[u].size(); i++) {
if(!vis[g[u][i]] && g[u][i] != u) {
tarjan(g[u][i]);
uf[g[u][i]] = u;
}
}
vis[u] = true;
for(i = ; i < q[u].size(); i++) {
if(vis[q[u][i]]) cnt[Find(q[u][i])]++;
}
}
int main() {
int i,j,u,v,k;
while(~scanf("%d",&n)) {
for(i = ; i <= n; i++) {
g[i].clear();
q[i].clear();
cnt[i] = ;
indeg[i] = false;
}
for(i = ; i < n; i++) {
scanf("%d:(%d)",&u,&k);
for(j = ; j < k; j++) {
scanf("%d",&v);
g[u].push_back(v);
indeg[v] = true;
}
}
scanf("%d",&m);
while(m--) {
scanf(" (%d %d)",&u,&v);
q[u].push_back(v);
q[v].push_back(u);
}
memset(vis,false,sizeof(vis));
memset(cnt,,sizeof(cnt));
for(i = ; i <= n; i++)
if(!indeg[i]) {
tarjan(i);
break;
}
for(i = ; i <= n; i++)
if(cnt[i]) printf("%d:%d\n",i,cnt[i]);
}
return ;
}
BNUOJ 1589 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 ...
- 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 ...
随机推荐
- 题解报告:hdu 1398 Square Coins(母函数或dp)
Problem Description People in Silverland use square coins. Not only they have square shapes but also ...
- QQ文件没有读取权限,60017导致QQ无法登陆的终极解决办法
每隔一段时间,我的QQ就无法登陆,提示:QQ文件没有读取权限,60017导致QQ无法登陆的终极解决办法 点击了解详情发现里面的解决办法根本不起作用,网上 说的各种解决办法都不起作用,解决办法如下 1. ...
- Hackonacci Matrix Rotations 观察题 ,更新了我的模板
https://www.hackerrank.com/contests/w27/challenges/hackonacci-matrix-rotations 一开始是没想到观察题的.只想到直接矩阵快速 ...
- Java核心技术梳理-异常处理
一.引言 异常总是不可避免的,就算我们自身的代码足够优秀,但却不能保证用户都按照我们想法进行输入,就算用户按照我们的想法进行输入,我们也不能保证操作系统稳定,另外还有网络环境等,不可控因素太多,异常也 ...
- hihocoder offer收割编程练习赛11 C 岛屿3
思路: 并查集的应用. 实现: #include <iostream> #include <cstdio> using namespace std; ][]; int n, x ...
- 掌握Spark机器学习库-08.2-朴素贝叶斯算法
数据集 iris.data 数据集概览 代码 import org.apache.spark.SparkConf import org.apache.spark.ml.classification.{ ...
- 未找到框架“.NETFramework,Version=v4.5”的引用程序集
问题描述 一般是在编译的时候会出现这样子的问题, 问题原因: C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETF ...
- Struts1 MVC框架的工作原理
MVC英文及Model-View-Controller,分别是模型(Model),视图(View)和控制(Controller).MVC模式的目的是实现web系统的职能分工. View:即用户交互界面 ...
- PHP面向对象考察点
面向对象三大特性 封装 封装性就是把对象的属性和服务结合成一个独立的相同单位,并尽可能隐蔽对象的内部细节,包含两个含义: 把对象的全部属性和全部服务结合在一起,形成一个不可分割的独立单位(即对象). ...
- 对称加密DES加密
DES加密: des是对称加密,加密和解密需要相同的秘钥,它的密码最长56位,必须是8的倍数,秘钥越长,越安全. package com.trm.util.encrypt; import java.s ...