POJ 1470 Closest Common Ancestors【LCA Tarjan】
题目链接:
http://poj.org/problem?id=1470
题意:
给定若干有向边,构成有根数,给定若干查询,求每个查询的结点的LCA出现次数。
分析:
还是很裸的tarjan的LCA。
这题我真的要吐槽!!调试了好久啊!!不是MLE就是RE。。。。
- 查询数量没给,所以要用矩阵来存,这样还可以避免重复。
- 给边的时候不知道会不会有重复的点,所以讲道理应该用vector保存。
- getchar。。。我不知道为什么会RE。。。
- 其实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】的更多相关文章
- POJ 1470 Closest Common Ancestors 【LCA】
任意门:http://poj.org/problem?id=1470 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000 ...
- POJ 1470 Closest Common Ancestors (LCA,离线Tarjan算法)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 13372 Accept ...
- POJ 1470 Closest Common Ancestors (LCA, dfs+ST在线算法)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 13370 Accept ...
- POJ - 1470 Closest Common Ancestors(离线Tarjan算法)
1.输出测试用例中是最近公共祖先的节点,以及这个节点作为最近公共祖先的次数. 2.最近公共祖先,离线Tarjan算法 3. /* POJ 1470 给出一颗有向树,Q个查询 输出查询结果中每个点出现次 ...
- POJ 1470 Closest Common Ancestors (模板题)(Tarjan离线)【LCA】
<题目链接> 题目大意:给你一棵树,然后进行q次询问,然后要你统计这q次询问中指定的两个节点最近公共祖先出现的次数. 解题分析:LCA模板题,下面用的是离线Tarjan来解决.并且为了代码 ...
- POJ 1470 Closest Common Ancestors【近期公共祖先LCA】
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u013912596/article/details/35311489 题目链接:http://poj ...
- POJ 1470 Closest Common Ancestors(最近公共祖先 LCA)
POJ 1470 Closest Common Ancestors(最近公共祖先 LCA) Description Write a program that takes as input a root ...
- poj1470 Closest Common Ancestors [ 离线LCA tarjan ]
传送门 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 14915 Ac ...
- POJ 1470 Closest Common Ancestors
传送门 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 17306 Ac ...
随机推荐
- JAVA解析XML的几种方法
DOM DOM Document Object Model 文档对象模型.在应用程序中,基于DOM的解析器将一个XML文档转换成一个对象模型的集合(DOM树),应用程序正是通过对这个对象模型的操作,来 ...
- PMP项目管理学习笔记(3)——过程框架
所有项目,不论大小,都可以分解为过程组: 如果项目规模很大,可以分阶段管理,每个阶段都要经过上面的五个过程组,从启动到收尾. 阶段的类型: 顺序阶段: 一个阶段完成后进入下个阶段. 重叠阶段: 有时需 ...
- codevs 1219 骑士游历 1997年
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 设有一个n*m的棋盘(2≤n≤50,2≤m≤50),如下图,在棋盘上有一个中国象 ...
- uva12433 Rent a Car
init 一开始搞成2*n+2了...囧 所以初始化很重要! 然后提交的时候忘了删调试的数据了..囧 技巧:设立虚拟节点 建图比较麻烦(非常). 要考虑到保养完了的车可以免费再用 设立S,T ,1 ...
- 深度学总结:skip-gram pytorch实现
文章目录 skip-gram pytorch 朴素实现网络结构训练过程:使用nn.NLLLoss()batch的准备,为unsupervised,准备数据获取(center,contex)的pair: ...
- VS2015提示:未安装Style的Visual Studio语言支持,代码编辑Intellisense将不可用。服务器控件的标记Intellisense可能不起作用
一.问题 最近在VS2015打开文件,提示未安装Style的Visual Studio语言支持,代码编辑Intellisense将不可用.服务器控件的标记Intellisense可能不起作用. Int ...
- java session cookie的使用
Cookie; Session; URL重写; cookie在J2EE项目中的使用,Java中把Cookie封装成了java.servlet.http.Cookie类.每个Cookie都是该Cooki ...
- react-native 在新版Xcode(10+)中运行出现的问题: node_modules/react-native/third-party/glog-0.3.4 , C compiler cannot create executables
报错发生在 react-native : 0.55.4 (或存在于更低的版本) 报错: ----/node_modules/react-native/third-party/glog-0.3.4': ...
- 模拟--P1328 生活大爆炸版石头剪刀布 题解
P1328 生活大爆炸版石头剪刀布 这也是打表么?? #include <iostream> using namespace std; static const auto y = []() ...
- API对接中经常会出现的签名获取,这只是某一种,仅供给有需要的人参考
要求: 1.对所有传入参数(含系统参数和接口参数)按照字段名的 ASCII 码从小到大排序(字典序)后,使用 URL 键值对的格式.(即 key1=value1&key2=value2…)拼接 ...