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 ...
随机推荐
- ES之各种运算符,for、while、do while 、switch case循环
运算符优先级: 在所有的运算符中,括号的优先级最高,赋值符号的优先级最低. 小括号 > 计算运算符 > 比较运算符 > 逻辑运算符 > 赋值符号———————————————— ...
- Spark学习之Spark SQL(8)
Spark学习之Spark SQL(8) 1. Spark用来操作结构化和半结构化数据的接口--Spark SQL. 2. Spark SQL的三大功能 2.1 Spark SQL可以从各种结构化数据 ...
- IOS 中使用token机制来验证用户的安全性
登录的业务逻辑{ http:是短连接. 服务器如何判断当前用户是否登录? // 1. 如果是即时通信类:长连接. // 如何保证服务器跟客户端保持长连接状态? // ...
- 关于Farseer.net轻量级ORM开源框架 V1.0 概念版本开发的消息
V0.2版的开源距离今天(05年03月)已有近3年的时间.可以说这个版本已经有点落伍的感觉了,呵呵. V0.2版至今一直处于BUG的修复及一些细小功能的增加,所以版本号上一直没有变化. 其实在这1.2 ...
- asterisk-java ami4 一些基本功能的例子
比如:挂机,拨号,拨外线,保留通话,示闲,示忙等等.... 在api中可以获得这些方法说明 /** * 给分机挂机 * @param Extension 要挂机的分机号 * @return * ...
- 半斤八两中级破解 (四) TCP_UDP协议转向本地验证
首先要用抓包工具判断是哪种协议,根据封包助手来看,教程中给出的例子是个TCP协议的,此时要记录下包的: 源地址,源端口 目的地址,目的端口 源包大小 目的包大小 然后再重新运行抓包工具和 ...
- COGS.1200 ganggang的烦恼
背景 Zhang Gangrui 年纪大了,记性不好,保险箱的密码记不住了,他只记得密码是一个数的阶乘各个位的数相加的和,最后还有个T或F,代表这个数是否为素数,正好,你到他家去了,他请你帮他这个忙, ...
- Java常用工具类---image图片处理工具类、Json工具类
package com.jarvis.base.util; import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStre ...
- hibernate cascade属性
cascade属性是存在于set标签中,用来做级联删除和保存. 它的值有以下几种: 1)默认值是none,不做级联动作: 2)save-update:级联保存 3)delete:级联删除 4)all: ...
- 奇异值分解 SVD 的数学解释
奇异值分解(Singular Value Decomposition,SVD)是一种矩阵分解(Matrix Decomposition)的方法.除此之外,矩阵分解还有很多方法,例如特征分解(Eigen ...