裸的LCA,读入小坑。Tarjan算法大坑,一开始不知道哪儿错了,后来才发现,是vis数组忘了清零了(⊙﹏⊙)b

傻傻的用了邻接矩阵。。。很慢啊,1100多ms。

Closest Common Ancestors

Description

Write a program that takes as input a rooted tree and a list of pairs of vertices. For each pair (u,v) the program determines the closest common ancestor of u and v in the tree. The closest common ancestor of two nodes u and v is the node w that is an ancestor of both u and v and has the greatest depth in the tree. A node can be its own ancestor (for example in Figure 1 the ancestors of node 2 are 2 and 5)

Input

The data set, which is read from a the std input, starts with the tree description, in the form:

nr_of_vertices

vertex:(nr_of_successors) successor1 successor2 … successorn



where vertices are represented as integers from 1 to n ( n <= 900 ). The tree description is followed by a list of pairs of vertices, in the form:

nr_of_pairs

(u v) (x y) …

The input file contents several data sets (at least one).

Note that white-spaces (tabs, spaces and line breaks) can be used freely in the input.

Output

For each common ancestor the program prints the ancestor and the number of pair for which it is an ancestor. The results are printed on the standard output on separate lines, in to the ascending order of the vertices, in the format: ancestor:times

For example, for the following tree:

Sample Input

5

5:(3) 1 4 2

1:(0)

4:(0)

2:(1) 3

3:(0)

6

(1 5) (1 4) (4 2)

(2 3)

(1 3) (4 3)

Sample Output

2:1

5:5

Hint

Huge input, scanf is recommended.

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char jy;
bool vis[999];
int q,m,jyy,first[999],ans[999],next[999999],tot,qu[999][999],v[999999],n,k,xx,yy,root,f[999];
int find(int x)
{
return x==f[x]?x:f[x]=find(f[x]);
}
void Tarjan(int x)
{
f[x]=x;
for(int i=first[x];~i;i=next[i])
{
Tarjan(v[i]);
f[find(v[i])]=x;
}
vis[x]=1;
for(int i=1;i<=n;i++)
{
if(vis[i]&&qu[x][i])
{
ans[find(i)]+=qu[x][i];
qu[x][i]=qu[i][x]=0;
}
}
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
memset(qu,0,sizeof(qu));
memset(ans,0,sizeof(ans));
memset(vis,0,sizeof(vis));
memset(first,-1,sizeof(first));
tot=1;
for(int i=1;i<=n;i++)
{
scanf("%d",&q);
while(scanf("%c",&jy)&&jy!='(');
scanf("%d",&m);
while(scanf("%c",&jy)&&jy!=')');
for(int i=1;i<=m;i++)
{
scanf("%d",&v[tot]);
vis[v[tot]]=1;
next[tot]=first[q];
first[q]=tot++;
}
}
for(int i=1;i<=n;i++)
if(!vis[i])
{
root=i;
break;
}
memset(vis,0,sizeof(vis));
scanf("%d",&k);
for(int i=1;i<=k;i++)
{
while(scanf("%c",&jy)&&jy!='(');
scanf("%d%d",&xx,&yy);
qu[xx][yy]++;
qu[yy][xx]=qu[xx][yy];
while(scanf("%c",&jy)&&jy!=')');
}
Tarjan(root);
for(int i=1;i<=n;i++)
if(ans[i])
printf("%d:%d\n",i,ans[i]);
}
}

最新文章

  1. springmvc处理上传图片代码(校验图片尺寸、图片大小)
  2. [iOS][issis] requestLocationWithReGeocode AMapLocationErrorDomain Code=5 &quot;取消&quot;
  3. windows查看端口命令
  4. DOM扩展之Selectors API
  5. Datatables带参重绘
  6. 删除用不到的linux内核
  7. VS2015 Cordova Ionic移动开发(四)
  8. OpenCV——写手势识别碰到的各种错误
  9. 4455[Zjoi2016]小星星 容斥+dp
  10. RESTful 最佳实战
  11. iOS下FMDB的多线程操作(二)
  12. 【爆料】-《阿伯泰大学毕业证书》Abertay一模一样原件
  13. Redis哨兵机制
  14. gitbook 入门教程之主题插件
  15. maven 中的pom中的 dependencyManagement 和 dependencies
  16. L3-1 二叉搜索树的结构 (30 分)
  17. Java实现排行榜基于Redis
  18. badboy录制网站出现css样式混乱,网页的图标点击没反应
  19. django之中间件、缓存、信号、admin内置后台
  20. flask_restful(转载)

热门文章

  1. ionic下拉多项选择
  2. vue02 过滤器、计算和侦听属性、vue对象的生命周期、阻止事件冒泡和刷新页面
  3. 零基础转行Linux云计算运维工程师获得20万年薪的超级学习技巧
  4. IE浏览器new Date()带参返回NaN解决方法
  5. react 子组件给父组件传值
  6. kernel-常见参数或宏
  7. [SQL Service] 时间处理:获取今天的00:00:00/获取今天的23:59:59
  8. 使用PHP操作MongoDB数据库
  9. TestNG的安装和使用
  10. [BZOJ 4999]This Problem Is Too Simple!