1、输出测试用例中是最近公共祖先的节点,以及这个节点作为最近公共祖先的次数。

2、最近公共祖先,离线Tarjan算法

3、

/*
POJ 1470
给出一颗有向树,Q个查询
输出查询结果中每个点出现次数
*/
/*
离线算法,LCATarjan
复杂度O(n+Q);
*/
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std; const int MAXN=;
const int MAXQ=;//查询数的最大值 //并查集部分
int F[MAXN];//需要初始化为-1
int find(int x){
if(F[x]==-)return x;
return F[x]=find(F[x]);
}
void bing(int u,int v){
int t1=find(u);
int t2=find(v);
if(t1!=t2)
F[t1]=t2;
}
//***********************
bool vis[MAXN];//访问标记
int ancestor[MAXN];//祖先
struct Edge{
int to,next;
}edge[MAXN*];
int head[MAXN],tot;
void addedge(int u,int v){
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
} struct Query{
int q,next;
int index;//查询编号
}query[MAXQ*];
int answer[MAXQ];//存储最后的查询结果,下标0 Q-1
int h[MAXQ];
int tt;
int Q; void add_query(int u,int v,int index){
query[tt].q=v;
query[tt].next=h[u];
query[tt].index=index;
h[u]=tt++;
query[tt].q=u;
query[tt].next=h[v];
query[tt].index=index;
h[v]=tt++;
} void init(){
tot=;
memset(head,-,sizeof(head));
tt=;
memset(h,-,sizeof(h));
memset(vis,false,sizeof(vis));
memset(F,-,sizeof(F));
memset(ancestor,,sizeof(ancestor));
}
void LCA(int u){
ancestor[u]=u;
vis[u]=true;
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].to;
if(vis[v])continue;
LCA(v);
bing(u,v);
ancestor[find(u)]=u;
}
for(int i=h[u];i!=-;i=query[i].next){
int v=query[i].q;
if(vis[v]){
answer[query[i].index]=ancestor[find(v)];
}
}
}
bool flag[MAXN];
int Count_num[MAXN];
int main(){
int n;
int u,v,k;
while(scanf("%d",&n)==){
init();
memset(flag,false,sizeof(flag));
for(int i=;i<=n;i++){
scanf("%d:(%d)",&u,&k);
while(k--){
scanf("%d",&v);
flag[v]=true;
addedge(u,v);
addedge(v,u);
}
}
scanf("%d",&Q);
for(int i=;i<Q;i++){
char ch;
cin>>ch;
scanf("%d %d)",&u,&v);
add_query(u,v,i);
}
int root;
for(int i=;i<=n;i++)
if(!flag[i]){
root=i;
break;
}
LCA(root);
memset(Count_num,,sizeof(Count_num));
for(int i=;i<Q;i++)
Count_num[answer[i]]++;
for(int i=;i<=n;i++)
if(Count_num[i]>)
printf("%d:%d\n",i,Count_num[i]);
}
return ;
}

POJ - 1470 Closest Common Ancestors(离线Tarjan算法)的更多相关文章

  1. POJ 1470 Closest Common Ancestors(最近公共祖先 LCA)

    POJ 1470 Closest Common Ancestors(最近公共祖先 LCA) Description Write a program that takes as input a root ...

  2. POJ 1470 Closest Common Ancestors 【LCA】

    任意门:http://poj.org/problem?id=1470 Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000 ...

  3. POJ 1470 Closest Common Ancestors (LCA,离线Tarjan算法)

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 13372   Accept ...

  4. POJ 1470 Closest Common Ancestors (LCA, dfs+ST在线算法)

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 13370   Accept ...

  5. POJ 1470 Closest Common Ancestors

    传送门 Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 17306   Ac ...

  6. poj——1470 Closest Common Ancestors

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 20804   Accept ...

  7. POJ 1470 Closest Common Ancestors(LCA&RMQ)

    题意比较费劲:输入看起来很麻烦.处理括号冒号的时候是用%1s就可以.还有就是注意它有根节点...Q次查询 在线st算法 /*************************************** ...

  8. POJ 1470 Closest Common Ancestors (最近公共祖先LCA 的离线算法Tarjan)

    Tarjan算法的详细介绍,请戳: http://www.cnblogs.com/chenxiwenruo/p/3529533.html #include <iostream> #incl ...

  9. POJ 1470 Closest Common Ancestors (模板题)(Tarjan离线)【LCA】

    <题目链接> 题目大意:给你一棵树,然后进行q次询问,然后要你统计这q次询问中指定的两个节点最近公共祖先出现的次数. 解题分析:LCA模板题,下面用的是离线Tarjan来解决.并且为了代码 ...

随机推荐

  1. php 正则匹配包含字母、数字以及下划线,且至少包含2种

    新系统注册功能需对用户名和密码做以下要求:包含字母.数字以及下划线,且至少包含2种: 在网上没有搜到符合要求的代码,于是自己对他人代码做了一点修改,经测试满足要求.代码如下: if (!preg_ma ...

  2. 表连接sql执行计划学习

    循环嵌套连接(Nested Loop Join) 合并连接(Merge Join) 哈西匹配(Hash Join) 文章:浅谈SQL Server中的三种物理连接操作 循环嵌套,如果内循环列上有索引, ...

  3. 如何在非localhost情况下访问Istio中的服务UI

    在使用Istio时经常会遇到需要用localhost访问服务UI才能看到相关的一些数据 但对于远程连接的时候使用localhost并不方便,所以需要修改一下它的部署文件,将原先的cluster IP改 ...

  4. HDU 3932 模拟退火

    HDU3932 题目大意:给定一堆点,找到一个点的位置使这个点到所有点中的最大距离最小 简单的模拟退火即可 #include <iostream> #include <cstdio& ...

  5. vue验证码点击更新

    vue验证码点击更新 不说啥,直接贴代码 html: <img class="captcha" @click="editCaptcha" :src=&qu ...

  6. iOS-bug·Failed to connect to github-production-release-asset-2e65be.s3.amazonaws.com port 443: Operation time out

    在首次运行 react naticve 项目时, 遇到了如下的问题: Failed to connect to github-production-release-asset-2e65be.s3.am ...

  7. django学习之- Models笔记

    1:创建数据库表 #单表# app01_user 生成的表明为 tb1class User(models.Model): name = models.CharField(max_length=32,d ...

  8. poj - 3686 The Windy's (KM算法)

    题意:n个订单和m个生产车间,每个订单在不同的车间生产所需要的时间不一样,并且每个订单只能在同一个车间中完成,直到这个车间完成这个订单就可以生产下一个订单.现在需要求完成n个订单的平均时间最少是多少. ...

  9. Java fail-fast 与 fail-safe 机制对比

    关于fail-fast参考这篇文章:   从 modCount 看 java集合 fail-fast 机制 一.fail-safe概述以及与fail-fast区别 首先 fail-safe 并不属于J ...

  10. Java加载配置文件类

    /** *  对应配置文件类, */ package com.up72.parkSys.ThirdParty; import java.io.IOException;import java.io.In ...