POJ 1470

标准的LCA,输入感觉怪怪的=.=

自己看了下Tarjan,再参考了下别人的处理方法(感觉自己好弱。。)

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std; const int N = 1010;
const int M = 1000010; int in[N], par[N], ans[N];
bool vis[N];
int n, edge, m; struct ST
{
int top;
int next[M],key[M],head[N];
void add(int x,int y)
{
key[top] = y; //记录儿子
next[top] = head[x];
head[x] = top++;
}
void clear()
{
top = 0;
memset(head,-1,sizeof(head));
}
}son,q; int fin(int x)
{
if(x != par[x])
par[x] = fin(par[x]);
return par[x];
} void lca(int cur)
{
par[cur] = cur;
for(int i = son.head[cur];i != -1;i = son.next[i])
{
lca(son.key[i]);
par[son.key[i]] = cur;
}
vis[cur] = true;
for(int i = q.head[cur];i!= -1;i = q.next[i])
{
if(vis[q.key[i]])
ans[fin(q.key[i])]++;
}
} void ini()
{
memset(in,0,sizeof(in));
memset(vis,false,sizeof(vis));
memset(ans,0,sizeof(ans));
} int main()
{
int root, u, num, v;
while(scanf("%d", &n) != EOF)
{
ini();
son.clear(), q.clear();
for(int j = 0; j < n; ++j)
{
scanf("%d:(%d)", &u, &num);
for(int i = 0; i < num; ++i)
{
scanf("%d", &v);
son.add(u, v);
in[v]++;
}
}
for(root = 1;in[root];root++);
scanf("%d",&m);
while(m--)
{
scanf(" (%d %d)", &u, &v);
if(u == v)
q.add(v, u);
else
{
q.add(u, v);
q.add(v, u);
}
}
lca(root);
for(int i = 1;i <= n;i++)
if(ans[i])
printf("%d:%d\n", i, ans[i]);
}
return 0;
}

  

最小公共祖先 (Tarjan) POJ1470的更多相关文章

  1. 51.Lowest Common Ancestor of a Binary Tree(二叉树的最小公共祖先)

    Level:   Medium 题目描述: Given a binary tree, find the lowest common ancestor (LCA) of two given nodes ...

  2. [leetcode]236. Lowest Common Ancestor of a Binary Tree树的最小公共祖先

    如果一个节点的左右子树上分别有两个节点,那么这棵树是祖先,但是不一定是最小的,但是从下边开始判断,找到后一直返回到上边就是最小的. 如果一个节点的左右子树上只有一个子树上遍历到了节点,那么那个子树可能 ...

  3. LCA 最近公共祖先 tarjan离线 总结 结合3个例题

    在网上找了一些对tarjan算法解释较好的文章 并加入了自己的理解 LCA(Least Common Ancestor),顾名思义,是指在一棵树中,距离两个点最近的两者的公共节点.也就是说,在两个点通 ...

  4. HDU 4547 CD操作 (LCA最近公共祖先Tarjan模版)

    CD操作 倍增法  https://i.cnblogs.com/EditPosts.aspx?postid=8605845 Time Limit : 10000/5000ms (Java/Other) ...

  5. LCA最近公共祖先——Tarjan模板

    LCA(Lowest Common Ancestors),即最近公共祖先,是指在有根树中,找出某两个结点u和v最近的公共祖先. Tarjan是一种离线算法,时间复杂度O(n+Q),Q表示询问次数,其中 ...

  6. Network-POJ3694(最小公共祖先LCA+Tarjin)

    http://poj.org/problem?id=3694 这一题  为什么要找最小祖先呢 当两个节点连到一块的时候  找最小公共节点就相当于找强连通分支 再找最小公共节点的过程中直到找到  这个过 ...

  7. LCA 最近公共祖先 Tarjan(离线)算法的基本思路及其算法实现

    首先是最近公共祖先的概念(什么是最近公共祖先?): 在一棵没有环的树上,每个节点肯定有其父亲节点和祖先节点,而最近公共祖先,就是两个节点在这棵树上深度最大的公共的祖先节点. 换句话说,就是两个点在这棵 ...

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

    学习博客:  http://noalgo.info/476.html 讲的很清楚! 对于一颗树,dfs遍历时,先向下遍历,并且用并查集维护当前节点和父节点的集合.这样如果关于当前节点(A)的关联节点( ...

  9. PAT A1151 LCA in a Binary Tree (30 分)——二叉树,最小公共祖先(lca)

    The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...

随机推荐

  1. PYTHON 词云

    #!/usr/bin/env python # -*- coding:utf-8 -*- import matplotlib.pyplot as plt from wordcloud import W ...

  2. Golang学习--开篇

    最近开始接收一个新项目,是使用Golang写的,需要重新捡起Golang来,于是就有了这个系列博客. Golang的环境配置,我就不说了,让我们直接开始. Golang官网:https://golan ...

  3. OAuth2.0学习(1-6)授权方式3-密码模式(Resource Owner Password Credentials Grant)

    授权方式3-密码模式(Resource Owner Password Credentials Grant) 密码模式(Resource Owner Password Credentials Grant ...

  4. redis中的aof模式,产生的是增量数据,还是全量数据?

    先说答案:全量数据. 1.修改redis.conf,开启rdb,禁用aof 上面这个是持久化文件的路径,我们ll看下: 2.启动redis后,cli查看里面的key [root@mini1 redis ...

  5. codeforces round 425 div2

    A. Sasha and Sticks 水题,判断一下次数的奇和偶就可以的. B. Petya and Exam 赛上的时候没有写出来,orz,记录一下吧. 题意:给出一个模式串,可能会有?和*两种符 ...

  6. Hive函数:SUM,AVG,MIN,MAX

    转自:http://lxw1234.com/archives/2015/04/176.htm,Hive分析窗口函数(一) SUM,AVG,MIN,MAX 之前看到大数据田地有关于max()over(p ...

  7. React-Native(五):React Native之Text学习

    本章节主要学习Text的布局,仿照网易新网: 代码: /** * Sample React Native App * https://github.com/facebook/react-native ...

  8. POJ-1860 Currency Exchange---Bellman-Ford判断正环

    题目链接: https://vjudge.net/problem/POJ-1860 题目大意: 我们的城市有几个货币兑换点.让我们假设每一个点都只能兑换专门的两种货币.可以有几个点,专门从事相同货币兑 ...

  9. Angular筛选功能

    业务场景:依据级别(level )和主题(Subtype )向后台传参数,进行筛选向前台返回数据列表. 代码如下:其中filterChoose()用于弹出筛选下拉框,filterButton()用于选 ...

  10. 关于sql多表去重问题

    很多人新手对于数据库distinct 的用法有误解接下来我们看一段代码: 数据表:table  id name    1 a   2 b   3 c   4 c   5 b   我们使用distinc ...