【题意】

给出一棵树和多组查询,求以每个节点为LCA的查询数有多少?

【错误点】

①读入的时候,注意它的空格是随意的呀!一开始不知道怎么弄,后来看了DISCUSS区大神的话:

询问部分输入:

scanf("%d",&m);
for(int i=0;i<m;i++){
scanf(" (%d %d)",&a,&b);
}

注意scanf(" 这里有一个空格

②多组数据啊!注意这句话:
The input file contents several data sets (at least one).

③痛心疾首!它问的是以每个节点为LCA的查询数,所以查询可以重复,同样的查询可以累计!

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int MAXN=+;
vector<int> E[MAXN];
int query[MAXN][MAXN];
int cnt[MAXN];
int notrt[MAXN],rt;
int ance[MAXN];
int vis[MAXN];
int m;//结点的总个数 int find(int u)
{
int r=u;
while (ance[r]!=r) r=ance[r];
int ans=r;
r=u;
while (ance[r]!=r)
{
int tmp=ance[r];
ance[r]=ans;
r=tmp;
}
return ans;
} void tarjan(int u)
{
vis[u]=;
for (int v=;v<=m;v++)
{
if (!query[u][v]) continue;
if (vis[v])
//被访问过有两种情况:一是祖先靠左的子树中的后代;或者是自己的祖先。两者的LCA均为v所在并查集的根
{
int LCA=find(v);
cnt[LCA]+=query[u][v];
//注意,允许同样的查询出现多次,并且同样的查询可以累计!
}
} ance[u]=u;
for (int i=;i<E[u].size();i++)
{
int v=E[u][i];
tarjan(v);
ance[v]=u;
}
} void init()
{
memset(query,,sizeof(query));
memset(cnt,,sizeof(cnt));
memset(notrt,,sizeof(notrt));
memset(vis,,sizeof(vis));
for (int i=;i<=m;i++) E[i].clear();
for (int i=;i<m;i++)
{
int p,T;
scanf("%d:(%d)",&p,&T);
for (int i=;i<T;i++)
{
int son;
scanf(" %d",&son);
E[p].push_back(son);
notrt[son]++;
}
} int n;
scanf("%d",&n);
for (int i=; i<n; i++)
{
int a,b;
scanf(" (%d %d)",&a,&b);
//注意这里(前面要有一个空格!否则无法过!
query[a][b]++;
query[b][a]++;
} for (int i=;i<=m;i++) if (!notrt[i])
{
rt=i;
break;
}
} void print()
{
for (int i=;i<=m;i++)
if (cnt[i]>)
cout<<i<<':'<<cnt[i]<<endl;
} int main()
{
while (~scanf("%d",&m))
//注意是多组数据
{
init();
tarjan(rt);
print();
}
return ;
}

【LCA/tarjan】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 Tarjan】

    题目链接: http://poj.org/problem?id=1470 题意: 给定若干有向边,构成有根数,给定若干查询,求每个查询的结点的LCA出现次数. 分析: 还是很裸的tarjan的LCA. ...

  4. POJ1470 Closest Common Ancestors

    LCA问题,用了离线的tarjan算法.输入输出参考了博客http://www.cnblogs.com/rainydays/archive/2011/06/20/2085503.htmltarjan算 ...

  5. POJ 1470 Closest Common Ancestors 【LCA】

    任意门:http://poj.org/problem?id=1470 Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000 ...

  6. POJ 1470 Closest Common Ancestors (LCA,离线Tarjan算法)

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 13372   Accept ...

  7. POJ 1470 Closest Common Ancestors(最近公共祖先 LCA)

    POJ 1470 Closest Common Ancestors(最近公共祖先 LCA) Description Write a program that takes as input a root ...

  8. POJ 1470 Closest Common Ancestors (LCA, dfs+ST在线算法)

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 13370   Accept ...

  9. poj----(1470)Closest Common Ancestors(LCA)

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 15446   Accept ...

随机推荐

  1. vim 颜色主题设置

    先看看vim编辑器提供的色彩配置方案: 首先进入vim的color目录(/usr/share/vim/vim74/colors,不同的系统目录不同,建议在-/建立.vim目录,然后在些目录里建立对应的 ...

  2. git中如何查看一个文件的修改(更新)历史

    有些时候有些文件或文件夹被移除了, 或者更换了路径或被改名了, 想跟踪一下这个文件被修改(更新)的历史, 可以用如下命令: git log -p matser -- filename 格式是: git ...

  3. Median of Two Sorted Arrays——算法课上经典的二分和分治算法

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  4. Reflow(回流)和Repaint(重绘) (转)

    原文地址:http://blog.csdn.net/qq_18826911/article/details/68924255 首先我们要明白的是,页面的显示过程分为以下几个阶段: 1.生成DOM树(包 ...

  5. 摄像机distortion vector、project matrix、camera matrix

    关于标定后图像如何校正:http://wiki.ros.org/image_pipeline/CameraInfo ros distortion vector 参数顺序:http://docs.ros ...

  6. Elasticsearch分片&副本分配

    集群索引中可能由多个分片构成,并且每个分片可以拥有多个副本,将一个单独的索引分为多个分片,可以处理不能在单一服务器上运行的 大型索引. 由于每个分片有多个副本,通过副本分配到多个服务器,可以提高查询的 ...

  7. Jmeter----请求的reponse结果中的某个参数作为JDBC Request的查询条件

    一.前言 数据库连接成功,若不会的查看:https://www.cnblogs.com/syw20170419/p/9832402.html 二.需求 将登录账号12608523691,接口的repo ...

  8. Python中列表的各种方法

    列表是Python中一种常用的存储信息的方式,所以要熟练掌握列表的各种方法: 首先我们定义一个列表(name),然后练习里面的各种方法: >>> name = ["Sora ...

  9. karma+seajs

    下面的介绍以karma能正常运行为前提,看karma系列文章:http://www.cnblogs.com/laixiangran/tag/Karma/ 目录结构 步骤 安装 npm install ...

  10. Java实现web在线预览office文档与pdf文档实例

    https://yq.aliyun.com/ziliao/1768?spm=5176.8246799.blogcont.24.1PxYoX 摘要: 本文讲的是Java实现web在线预览office文档 ...