bryce1010模板

http://poj.org/problem?id=1470

/*伪代码
Tarjan(u)//marge和find为并查集合并函数和查找函数
{
for each(u,v) //访问所有u子节点v
{
Tarjan(v); //继续往下遍历
marge(u,v); //合并v到u上
标记v被访问过;
}
for each(u,e) //访问所有和u有询问关系的e
{
如果e被访问过;
u,e的最近公共祖先为find(e);
}
} */
//思想
/*
1.任选一个点为根节点,从根节点开始。
2.遍历该点u所有子节点v,并标记这些子节点v已被访问过。
3.若是v还有子节点,返回2,否则下一步。
4.合并v到u上。
5.寻找与当前点u有询问关系的点v。
6.若是v已经被访问过了,则可以确认u和v的最近公共祖先为v被合并到的父亲节点a。
*/ #include<iostream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
#include<math.h>
using namespace std;
#define ll long long const int MAXN=1010;
const int MAXQ=500010; //并查集部分
int F[MAXN];//初始化为-1
int find(int x)
{
if(F[x]==-1)return x;
return F[x]=find(F[x]);
}
//merge
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<<2];
int head[MAXN],tot;
void add_edge(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<<1]; 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++;
} //LCA部分
void init()
{
tot=0;
memset(head,-1,sizeof(head));
tt=0;
memset(h,-1,sizeof(h));
memset(vis,false,sizeof(vis));
memset(F,-1,sizeof(F));
memset(ancestor,0,sizeof(ancestor));
} void LCA(int u)
{
ancestor[u]=u;
vis[u]=true;
for(int i=head[u];i!=-1;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!=-1;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)==1)
{
init();
memset(flag,false,sizeof(flag));
for(int i=1;i<=n;i++)
{
scanf("%d:(%d)",&u,&k);
while(k--)
{
scanf("%d",&v);
flag[v]=true;
add_edge(u,v);
add_edge(v,u);
}
}
scanf("%d",&Q);
for(int i=0;i<Q;i++)
{
char ch;
cin>>ch;
scanf("%d %d)",&u,&v);
//cin>>ch;
add_query(u,v,i);
}
int root;
for(int i=1;i<=n;i++)//找到没有入度的点作为root
{
if(!flag[i])
{
root=i;
break;
}
}
LCA(root);
memset(Count_num,0,sizeof(Count_num));
for(int i=0;i<Q;i++)
{
Count_num[answer[i]]++;
}
for(int i=1;i<=n;i++)
{
if(Count_num[i]>0)
{
printf("%d:%d\n",i,Count_num[i]);
}
}
}
// getchar();getchar();
return 0;
} /*
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)
*/

POJ1470 LCA (Targan离线)的更多相关文章

  1. poj 1986 Distance Queries(LCA:倍增/离线)

    计算树上的路径长度.input要去查poj 1984. 任意建一棵树,利用树形结构,将问题转化为u,v,lca(u,v)三个点到根的距离.输出d[u]+d[v]-2*d[lca(u,v)]. 倍增求解 ...

  2. bzoj 3626 [LNOI2014]LCA(离线处理+树链剖分,线段树)

    3626: [LNOI2014]LCA Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1272  Solved: 451[Submit][Status ...

  3. poj 1330 LCA (倍增+离线Tarjan)

    /* 先来个倍增 */ #include<iostream> #include<cstring> #include<cstdio> #define maxn 100 ...

  4. hdu2586How far away ?(LCA LCATarjan离线)

    题目链接:acm.hdu.edu.cn/showproblem.php?pid=2586 题目大意:有n个点,同n-1条带有权值的双向边相连,有m个询问,每个询问包含两个数x,y,求x与y的最短距离. ...

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

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

  6. 最近公共祖先LCA Tarjan 离线算法

    [简介] 解决LCA问题的Tarjan算法利用并查集在一次DFS(深度优先遍历)中完成所有询问.换句话说,要所有询问都读入后才开始计算,所以是一种离线的算法. [原理] 先来看这样一个性质:当两个节点 ...

  7. LCA的离线快速求法

    最常见的LCA(树上公共祖先)都是在线算法,往往带了一个log.有一种办法是转化为"+-1最值问题"得到O(n)+O(1)的复杂度,但是原理复杂,常数大.今天介绍一种允许离线时接近 ...

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

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

  9. poj1470 LCA倍增法

    倍增法模板题 #include<iostream> #include<cstring> #include<cstdio> #include<queue> ...

随机推荐

  1. [NOIP2011提高组day2]-3-观光公交

    3.观光公交 (bus.cpp/c/pas) [问题描述] 风景迷人的小城 Y 市,拥有 n 个美丽的景点.由于慕名而来的游客越来越多,Y 市特意安排了一辆观光公交车,为游客提供更便捷的交通服务.观光 ...

  2. angularjs ng-repeat倒叙

    <div ng-app="myApp" ng-controller="customersCtrl"> <table> <tr ng ...

  3. requirejs测试

    参考资料:http://www.ruanyifeng.com/blog/2012/11/require_js.html 一.文件目录 二.html <!DOCTYPE html> < ...

  4. Codeforces Round #394 (Div. 2) C. Dasha and Password —— 枚举

    题目链接:http://codeforces.com/problemset/problem/761/C C. Dasha and Password time limit per test 2 seco ...

  5. Appium——Error while obtaining UI hierarchy XML file:com.android.ddmlib.SyncException:

    使用uiautomatorviewer查看页面元素时报这个错误,解决办法 cmd: adb root ok  解决

  6. java编程思想-基础

    interface: 方法默认为public:成员变量默认 static and final 对象数组的定义:理解? 多接口继承:可以多个接口,但只有一个具体类,具体类在前面 自:多接口继承时,来自不 ...

  7. python 特殊方法之new

    object.__new__(cls[, ...]) Called to create a new instance of class cls. __new__() is a static metho ...

  8. Android Developers - Training

    Recently I've been contemplating to create a new App with the true "Android Design",new An ...

  9. 更换ubuntu apt-get源

    原文地址:http://www.cnblogs.com/zhangpengshou/p/3591387.html 为了优化ubuntu软件安装/更新速度,我测试了国内几家apt源的速度,发现北京交大的 ...

  10. Mybatis一二级缓存的理解

        频繁的数据库操作是非常耗费性能的(主要是因为对于DB而言,数据是持久化在磁盘中的,因此查询操作需要通过IO,IO操作速度相比内存操作速度慢了好几个量级),尤其是对于一些相同的查询语句,完全可以 ...