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. Bootstrap模态框

    backdrop选项,当设置成false的时候, 背景不会出现半透明的遮盖层,当用户点击模态框外部时不会关闭模态框: 设置成true的时候会出现遮盖层,当用户点击模态框外部时则会关闭模态框. 那如果又 ...

  2. Response.Redirect和Server.Transfer

    今天又比较闲,逛了逛园子,看看asp.net的内容,看到一篇关于这两个的比较: http://www.cnblogs.com/yunfeng8967/archive/2008/03/06/109323 ...

  3. 22 高级SQL特性

    1.约束 为正确地进行关系数据库设计,需要一种方法来保证只在表中插入合法的数据.例如,如果Orders表存储订单信息,OrderItems表存储订单详细内容,应该保证Orderitems中引用的任何订 ...

  4. 1097. Deduplication on a Linked List (25)

    Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated ...

  5. Java从入门到精通——调错篇之ORACLE 打开PLSQL时提示ora-01033

    客户Oracle服务器进入PL/SQL Developer时报ora-01033:oracle initializationg or shutdown in progress 错误提示,应用系统无法连 ...

  6. MySQL 多实例删库脚本

    DB版本:5.5.14 OS:CentOS 6.3 在测试环境中,在一台服务器上创建多个实例,在每个实例中一个一个删库比较麻烦,因此用下面脚本,可以直接删除所有库,除了系统库以外: #!/bin/ba ...

  7. OJ网站程序员必备

    一. Online Judge简介: Online Judge系统(简称OJ)是一个在线的判题系统.用户可以在线提交程序多种程序(如C.C++.Pascal)源代码,系统对源代码进行编译和执行,并通过 ...

  8. posix thread 浅谈

    用Posix thread进行多线程设计,就不怕跨平台了,因为很多OS都兼容Posix thread,如Linux/Windows等,甚至嵌入式系统上(如rt-thread)都支持posix thre ...

  9. [css]兼容性

    div +input 输入框 , 在微信中  有问题 块级元素 行内元素

  10. [转]宏的高级使用--##,__VA_ARGS__, __FILE__, __FUNCTION__等

    [转]宏的高级使用--##,__VA_ARGS__, __FILE__, __FUNCTION__等 http://blog.csdn.net/yiya1989/article/details/784 ...