题目链接:

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. 跟随鼠标指针跑的div拖拽效果

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  2. 洛谷P1724 东风谷早苗

    题目描述 在幻想乡,东风谷早苗是以高达控闻名的高中生宅巫女.某一天,早苗终于入手了最新款的钢达姆模型.作为最新的钢达姆,当然有了与以往不同的功能了,那就是它能够自动行走,厉害吧(好吧,我自重).早苗的 ...

  3. asp.net mvc 5 微信接入VB版 - 获取AccessToken

    获取AccessToken是微信接入的又一个基础操作.很多微信接口需要这个2小时一刷新的AccessToken作为参数. 转载请说明作者Nukepayload2 首先根据开发文档把获取AccessTo ...

  4. Python 语言规范

    Python 语言规范 pychecker  对你的代码运行pychecker 定义: pychecker 是一个在Python 源代码中查找bug 的工具. 对于C 和C++这样的不那 么动态的( ...

  5. dnskeygen - 针对DNS安全性所生成的公共,私有和共享的密钥

    SYNOPSIS(总览) dnskeygen [- [DHR ] size ] [-F ] -[zhu ] [-a ] [-c ] [-p num ] [-s num ] -n name DESCRI ...

  6. node.js中使用Redis

    服务端: 启动Redis服务: redis-server 客户端: 1.安装Redis        npm install redis --save 2.redisTest.js文件 //引入red ...

  7. Spread / Rest 操作符

    Spread / Rest 操作符指的是 ...,具体是 Spread 还是 Rest 需要看上下文语境. 当被用于迭代器中时,它是一个 Spread 操作符:(参数为数组) function foo ...

  8. Cycloid Hydraulic Motor Use: Use Failure And Treatment

    The cycloidal hydraulic motor is a small low-speed, high-torque hydraulic motor with a shaft-distrib ...

  9. dotTrace激活服务器

    http://active.09l.me IntelliJ IDEA 7.0 或 更高DataGrip 1.0或更高ReSharper 3.1 或更高ReSharper Cpp 1.0 或更高dotT ...

  10. 编码&解码

    编码与解码首先,明确一点,计算机中存储的信息都是二进制的 编码/解码本质上是一种映射(对应关系):比如‘a’用ascii编码则是65,计算机中存储的就是00110101,但是显示的时候不能显示0011 ...