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 ...
随机推荐
- sql server 中更改默认实例
因为安装了多个版本的sql server,会造成同时存在多个实例的情况. 1.关闭旧版本的sql server实例,并设置为手动启动 2.按下图步骤操作,把tcp端口设置为1433 3.重启sql s ...
- windows bat脚本实现ftp自动下载 删除
现在有一个需求就是把远程某个文件下面的图片,下载到本地,并且删除下载成功的的文件,而且远程目录下的那个图片会随时增加.假设一下如果所有的脚本都写好了,那么就需要调用windows上的计划任务定时执行脚 ...
- java 语法糖
package syntax.autoCase; import java.util.Arrays; import java.util.List; public class autoCase { pub ...
- 第22章 项目3:万能的XML
Mix-in:混入类,是一种Python程序设计中的技术,作用是在运行期间动态改变类的基类或类的方法,从而使得类的表现可以发生变化.可以用在一个通用类接口中. 在实践一个解析XML文件的实践中,体会动 ...
- vim使用手册
1. 关于Vim 1.1 Vim的几种模式 2. 启动Vim 3. 文档操作 4. 光标的移动 4.1 基本移动 4.2 翻屏 4.3 标记 5. 插入文本 5.1 基本插入 5.2 改写插入 6. ...
- 【iis错误码】IIS 服务 这些年遇到的错误码
II 发生错误,客户端似乎有问题.例如,客户端请求不存在的页面,客户端未提供有效的身份验证信息. 400 - 错误的请求. 401 - 访问被拒绝. -- 暴力添加everyone用户, i ...
- String对象中常用的方法
String对象中常用的方法 1.charCodeAt方法返回一个整数,代表指定位置字符的Unicode编码.strObj.charCodeAt(index)说明:index将被处理字符的从零开始 ...
- Export Farm Solution wsp Files SharePoint 2007 and 2010
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")$farm = [Microsof ...
- flash builder Error #2032
flash builder编译时flash player报错:"Error #2032". 解决办法:在菜单栏的 项目-->属性-->构建路径,不要勾选“在调试时使用本 ...
- Cookie 获取
二级域名可以获取一级域名的Cookie值 二级域名下删除顶级域名下的Cookie,需要添加顶级域名的Cookie作用域 /// <summary> /// 根据cookie名称删除 /// ...