POJ1470 Closest Common Ancestors
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的更多相关文章
- poj1470 Closest Common Ancestors [ 离线LCA tarjan ]
传送门 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 14915 Ac ...
- POJ1470 Closest Common Ancestors 【Tarjan的LCA】
非常裸的模版题,只是Tarjan要好好多拿出来玩味几次 非常有点巧妙呢,tarjan,大概就是当前结点和它儿子结点的羁绊 WA了俩小时,,,原因是,这个题是多数据的(还没告诉你T,用scanf!=EO ...
- POJ 1470 Closest Common Ancestors(最近公共祖先 LCA)
POJ 1470 Closest Common Ancestors(最近公共祖先 LCA) Description Write a program that takes as input a root ...
- POJ 1470 Closest Common Ancestors (LCA, dfs+ST在线算法)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 13370 Accept ...
- POJ 1470 Closest Common Ancestors
传送门 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 17306 Ac ...
- poj----(1470)Closest Common Ancestors(LCA)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 15446 Accept ...
- POJ 1470 Closest Common Ancestors (LCA,离线Tarjan算法)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 13372 Accept ...
- POJ 1470 Closest Common Ancestors 【LCA】
任意门:http://poj.org/problem?id=1470 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000 ...
- BNUOJ 1589 Closest Common Ancestors
Closest Common Ancestors Time Limit: 2000ms Memory Limit: 10000KB This problem will be judged on PKU ...
随机推荐
- LINQ技巧:如何通过多次调用GroupBy实现分组嵌套
问题如上,解决如下,目标在最下面:结果: using System; using System.Linq; using System.Collections.Generic; namespace Co ...
- AJAX局部更新演出排期
<script language="javascript" type="text/javascript"> function createXMLHt ...
- WPF:简洁为美
(1)3行代码实现水印TextBox(Watermark TextBox) 效果图: 源代码: <Grid> <Grid.Resources> <BooleanToVi ...
- Hadoop管理员的十个最佳实践(转)
前言 接触Hadoop有两年的时间了,期间遇到很多的问题,既有经典的NameNode和JobTracker内存溢出故障,也有HDFS存储小文件问题,既有任务调度问题,也有MapReduce性能问题.遇 ...
- 用Python作GIS之四:Tkinter基本界面的搭建
Python下的主窗口可以定义如下:def start(self): #self.project = Project("temp") #self.pro ...
- C++实现数字媒体三维图像变换
C++实现数字媒体三维图像变换 必备环境 glut.h 头文件 glut32.lib 对象文件库 glut32.dll 动态连接库 程序说明 C++实现了用glut画物体对象的功能.并附带放大缩小,旋 ...
- iOS常见问题(1)
一.storyboard连线问题 产生原因:将与storyboard关联的属性删除了,但是storyboard中还保持之前所关联的属性. 解决: 1.点击view controller 2.点击这排最 ...
- Express/Koa/Hapi
Express/Koa/Hapi 本文翻译自: https://www.airpair.com/node.js/posts/nodejs-framework-comparison-express-ko ...
- Awesome (and Free) Data Science Books[转]
Post Date: September 3, 2014By: Stephanie Miller Marty Rose, Data Scientist in the Acxiom Product an ...
- PythonChallenge 1:恺撒密码的解码
题目: 解题思路:根据图中的K→M,O→Q,E→G,我们可以发现K,O,E这三个字母都向后移动了2位.据说恺撒率先使用了这一加密方法,因此将其命名为恺撒密码.它的基本思想是:通过把字母移动一定的位数来 ...