题目链接:

http://poj.org/problem?id=1470

题意:

给定若干有向边,构成有根数,给定若干查询,求每个查询的结点的LCA出现次数。

分析:

还是很裸的tarjan的LCA。

这题我真的要吐槽!!调试了好久啊!!不是MLE就是RE。。。。

  1. 查询数量没给,所以要用矩阵来存,这样还可以避免重复。
  2. 给边的时候不知道会不会有重复的点,所以讲道理应该用vector保存。
  3. getchar。。。我不知道为什么会RE。。。
  4. 其实ance数组可以不用的,只要每次处理子树的时候pa[v] = u即可。

代码:

#include<cstdio>
#include<cstring>
#include<vector>
#include<iostream>
using namespace std;
#define mem(a, b) memset(a, b, sizeof(a))
#define fuck cout<<"fuck"<<endl;
const int maxn = 1100;
vector<int>G[maxn];
int pa[maxn];
bool vis[maxn];
bool in[maxn];
int ance[maxn];
int cnt[maxn];
int root, n, tot;
int query[maxn][maxn];
int _find(int x)
{
if(pa[x] != x) return pa[x] = _find(pa[x]);
return x;
}
void unite(int x, int y)
{
int rx = _find(x), ry = _find(y);
if(rx == ry) return;
pa[rx] = ry;
}
void init()
{
for(int i = 1; i <= n; i++){
G[i].clear();
pa[i] = i;
}
mem(ance, 0);
mem(vis, false);
mem(query, 0);mem(cnt, 0);
mem(in, false);
}
void LCA(int u)
{
ance[u] = u;
for(int i = 0; i < G[u].size(); i++){
int v = G[u][i];
LCA(v);
unite(u, v);
ance[_find(u)] = u;
}
vis[u] = true;
for(int i = 1; i <= n; i++){
if(vis[i] && query[u][i]) cnt[ance[_find(i)]] += query[u][i];
}
}
int main(void)
{
int u, v, k;
int Q;
while(~scanf("%d",&n)){
init();
for(int i = 0; i < n; i++){
scanf("%d:(%d)",&u,&k);
while(k--){
scanf("%d",&v);
in[v] = true;
G[u].push_back(v);
}
}
scanf("%d",&Q);
for(int i = 0; i < Q; i++){
scanf(" (%d %d)",&u,&v);
query[u][v]++;
query[v][u]++;
}
for(root = 1; root <= n; root++){
if(!in[root]) break;
}
LCA(root);
for(int i = 1; i <= n ; i++)
if(cnt[i]) printf("%d:%d\n",i, cnt[i]);
}
return 0;
}

POJ 1470 Closest Common Ancestors【LCA Tarjan】的更多相关文章

  1. POJ 1470 Closest Common Ancestors 【LCA】

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

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

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

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

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

  4. POJ - 1470 Closest Common Ancestors(离线Tarjan算法)

    1.输出测试用例中是最近公共祖先的节点,以及这个节点作为最近公共祖先的次数. 2.最近公共祖先,离线Tarjan算法 3. /* POJ 1470 给出一颗有向树,Q个查询 输出查询结果中每个点出现次 ...

  5. POJ 1470 Closest Common Ancestors (模板题)(Tarjan离线)【LCA】

    <题目链接> 题目大意:给你一棵树,然后进行q次询问,然后要你统计这q次询问中指定的两个节点最近公共祖先出现的次数. 解题分析:LCA模板题,下面用的是离线Tarjan来解决.并且为了代码 ...

  6. POJ 1470 Closest Common Ancestors【近期公共祖先LCA】

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u013912596/article/details/35311489 题目链接:http://poj ...

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

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

  8. poj1470 Closest Common Ancestors [ 离线LCA tarjan ]

    传送门 Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 14915   Ac ...

  9. POJ 1470 Closest Common Ancestors

    传送门 Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 17306   Ac ...

随机推荐

  1. 找不到draw9patch.bat?已经不用找了

    Google 已经因为 draw9patch 热门的原因,把它集成在 Android Studio 里面了, 你现在可以直接在 Android Studio 里直接打开编辑了.

  2. JD IPO address by liuqiangdong

    Ladies and gentlemen, Good evening.I'd rather use english, not mandarin.Because during the road show ...

  3. dede网站目录权限设置

    如果你的网站数据十分重要(那种两天就能弄好的垃圾站就算了),建议按本文所说的安全步骤进行严格的设置.1.目录权限 我们不建议用户把栏目目录设置在根目录, 原因是这样进行安全设置会十分的麻烦, 在默认的 ...

  4. liunx+mysql数据库管理

    源码安装 查询是否安装:  rpm -aq |grep mysql 1.下载yum 源            wget 'https://dev.mysql.com/get/mysql57-commu ...

  5. ubuntu命令行转换图片像素大小

    convert -resize 512x256 00433.png 00001.png 1.512和256之间是x(就是字母那个x),用' * '反而会报错 2.这个命令会按照原图的比例进行转换 3. ...

  6. faster rcnn的改进方向

    http://blog.csdn.net/linolzhang/article/details/74159463 http://blog.csdn.net/linolzhang/article/det ...

  7. python爬虫---从零开始(一)初识爬虫

    我们开始来谈谈python的爬虫. 1,什么是爬虫: 网络爬虫是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本.另外一些不常使用的名字还有蚂蚁.自动索引.模拟程序或者蠕虫.互联网犹如一个大蜘蛛 ...

  8. node程序的部署神器pm2的基本使用

    pm2是从nodejs衍生出来的服务器进程管理工具,可以做到开机就启动nodejs.当然了,也可以用nohup来做这件事情的. 前言 众所周知,Node.js运行在Chrome的JavaScript运 ...

  9. 条款3:尽可能使用const(use const whenever possible)

    1.只要这(某值保持不变)是事实,就应该确实说出来,这样可以获得编译器的协助,确保这条约束不被违反. 2.keyword const 有很多种用法,但都简单易用. 2.1classes 外部修饰glo ...

  10. pytest以类形式的测试用例

    from __future__ import print_function#pytest以类形式的测试用例class TestClass: @classmethod def setup_class(c ...