POJ1470 Closest Common Ancestors

时间:2022-03-14 14:45:14

LCA问题,用了离线的tarjan算法。输入输出参考了博客http://www.cnblogs.com/rainydays/archive/2011/06/20/2085503.html
tarjan算法是用了dfs+并查集的方式做的。这里输入输出有个不错的地方,就是用scanf("%[^0-9]", st);跳过非数字。
里面用数组g来表示多维的树,还用并查集的id数组的-1来表示是否访问。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std; #define MAXN 909 /*
g for the edges
hasroot for whether the node is under root, it helps to identify the root
id for disjoint-set
lca for LCA of two nodes
sum for the count for ancestors in result
*/
int n, root;
bool g[MAXN][MAXN], hasroot[MAXN];
int id[MAXN], lca[MAXN][MAXN];
int sum[MAXN]; void input()
{
int a, b, m;
char str[100];
memset(g, 0, sizeof(g));
memset(hasroot, 0, sizeof(hasroot));
for (int i = 0; i < n; i++)
{
scanf("%d", &a);
a--;
scanf("%[^0-9]", str);
scanf("%d", &m);
scanf("%[^0-9]", str);
for (int i = 0; i < m; i++)
{
scanf("%d", &b);
b--;
hasroot[b] =true;
g[a][b] = g[b][a] =true;
}
}
for (int i = 0; i < n; i++)
if (!hasroot[i])
{
root = i;
break;
}
} // for disjoint-set
int find(int i)
{
if (id[i] == i)
return i;
return id[i] = find(id[i]);;
} void merge(int i, int j)
{
id[find(i)] = find(j);
} // do the tarjan algo and update lca table
void tarjan(int rt)
{
id[rt] = rt;
// id[k] != -1 means visited
for (int i = 0; i < n; i++)
if (g[rt][i] && id[i] == -1)
{
tarjan(i);
merge(i, rt); // the order matters, because of the implementaion of merge
}
for (int i = 0; i < n; i++)
if (id[i] != -1)
lca[rt][i] = lca[i][rt] = find(i);
} void solve()
{
int m;
char str[100];
scanf("%d", &m);
for (int i =0; i < m; i++)
{
int a, b;
scanf("%[^0-9]", str);
scanf("%d", &a);
scanf("%[^0-9]", str);
scanf("%d", &b);
a--;
b--;
sum[lca[a][b]]++;
}
for (int i =0; i < n; i++)
if (sum[i])
printf("%d:%d\n", i + 1, sum[i]);
} int main()
{
//freopen("d:\\\\t.txt", "r", stdin);
while (scanf("%d", &n) != EOF)
{
char str[100];
input();
memset(id, -1, sizeof(id));
memset(sum, 0, sizeof(sum));
tarjan(root);
solve();
scanf("%[^0-9]", str);
}
return 0;
}

  

POJ1470 Closest Common Ancestors的更多相关文章

  1. poj1470 Closest Common Ancestors &lbrack; 离线LCA tarjan &rsqb;

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

  2. POJ1470 Closest Common Ancestors 【Tarjan的LCA】

    非常裸的模版题,只是Tarjan要好好多拿出来玩味几次 非常有点巧妙呢,tarjan,大概就是当前结点和它儿子结点的羁绊 WA了俩小时,,,原因是,这个题是多数据的(还没告诉你T,用scanf!=EO ...

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

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

  4. POJ 1470 Closest Common Ancestors (LCA&comma; dfs&plus;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(LCA)

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

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

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

  8. POJ 1470 Closest Common Ancestors 【LCA】

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

  9. BNUOJ 1589 Closest Common Ancestors

    Closest Common Ancestors Time Limit: 2000ms Memory Limit: 10000KB This problem will be judged on PKU ...

随机推荐

  1. Android View的加载过程

    大家都知道Android中加载view是从Activity的onCreate方法调用setContentView开始的,那么View的具体加载过程又是怎么的呢?这一节我们做一下分析. 首先追踪一下代码 ...

  2. JSON详解 &period;net

    之前json掌握的不好,浪费了好多时间在查找一些json有关的转换问题,我所知道的方法只有把json序列化和反序列化一下,但是太麻烦了我觉得,所以就在找一些更简单又方便使用的方法.也许这个会有用吧,所 ...

  3. CSS实现三角形方法二--border&plus;content

    方法说明: 1.将一个div块的内容设置为空(content=" "), 2.设置它的边框(上下左右)颜色为透明(transparent), 3.设置它的左侧边框颜色为pink. ...

  4. &lbrack;Abp 源码分析&rsqb;九、事件总线

    0.简介 事件总线就是订阅/发布模式的一种实现,本质上事件总线的存在是为了降低耦合而存在的. 从上图可以看到事件由发布者发布到事件总线处理器当中,然后经由事件总线处理器调用订阅者的处理方法,而发布者和 ...

  5. lua相关的小知识

    lua的特性 1. 轻量级:一标准的C语言编写原发开放,编译后仅仅100K,占用内存小: 2. 扩展性:Lua提供了非常已于使用的扩展口和机制: 3. 支持面向过程编程和函数式编程 lua的数据类型 ...

  6. MPU6050带字符驱动的i2c从设备驱动1

    开干: 1.闲言碎语 这个驱动,越写觉的越简单,入门难,入门之后感觉还好.Linux开发还是比较友好的. 2.编写MPU6050带字符驱动的i2c从设备驱动 要实现的功能就是,将MPU6050作为字符 ...

  7. 利用CSS3实现透明边框和多重边框

    使用background-clip属性实现透明边框 .bordertest { border: 30px solid hsla(0,0%,90%,.5); background: #bbb; back ...

  8. 【Java入门提高篇】Day34 Java容器类详解(十五)WeakHashMap详解

    源码详解系列均基于JDK8进行解析 说明 在Java容器详解系列文章的最后,介绍一个相对特殊的成员:WeakHashMap,从名字可以看出它是一个 Map.它的使用上跟HashMap并没有什么区别,所 ...

  9. 添加RPMfusion仓库

    先添加epel Fedora的意识形态很是严谨,它不会自带任何非*组件.官方仓库不会提供一些包含有非*组件的基本软件,比如像多媒体编码.因此,安装一些第三方仓库很有必要,这些仓库会为我们提供一些基 ...

  10. JS实现数组去重(重复的元素只保留一个)

    1.遍历数组法 它是最简单的数组去重方法(indexOf方法) 实现思路:新建一个数组,遍历去要重的数组,当值不在新数组的时候(indexOf为-1)就加入该新数组中: ,,,,,,,,]; func ...