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 [ 离线LCA tarjan ]

    传送门 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, 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(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. System.Data.OracleClient 需要 Oracle 客户端软件 version 8.1.7 或更高版本

    说明: 执行当前 Web 请求期间,出现未经处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息. 异常详细信息: System.ServiceModel.FaultEx ...

  2. UCML平台中 如何设置列表单元格中的链接失效

    解决方案: 找到“a.datagrid-cell-bclink”,麻烦的是这个标记是由js动态加载的,需要等待这个加载完成:等加载完成后,删除a标记“$(“a.datagrid-cell-bclink ...

  3. mongodb学习之路1

    第一节 MongoDB介绍及下载与安装 引言 MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的.他支持的数据结构非常松散,是类似 json的b ...

  4. arcobject 相关

    要素添加: http://resources.esri.com/help/9.3/arcgisengine/arcobjects/esriGeoDatabase/IFeatureClass.Inser ...

  5. R简易入门(二)

    本文内容来源:https://www.dataquest.io/mission/128/working-with-data-frames 本文摘要:简单介绍一下用R处理数据   原始数据展示(这是一份 ...

  6. mac os使用homebrew来管理后台服务

    在linux下我们经常通过 service 或者 /etc/init.d/来管理我们的后台服务软件,并使用包管理器安装这些软件. 在mac下有homebrew这个好用的工具来安装软件,但是一直没有找到 ...

  7. selenium+python 浏览器标签页跳转 switch_to_window

    浏览器页面跳转方法记录: from selenium import webdriver import time browser = webdriver.Chrome() first_url='http ...

  8. 《.NET简单企业应用》技术路线

    前言 工作三年了,一直从事基于.NET体系的企业应用开发,心得和经验也攒了点:担心时间长了给忘了,所以得给写下来,以便以后回味回味:更重要的是能让知识系统化和体系化. 本系列以一个简单的企业应用系统为 ...

  9. 从零单排学JavaWeb

    之前是一个asp爱好者,感觉前途渺茫,特此转向Powerful的Java阵型,寻求心灵上的慰藉. 把自己遇到的问题记录下来,同时也分享给大家.  环境-下载 1 JDK http://dlsw.bai ...

  10. Java HTML页面抓取实例

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import ...