SP14932 【LCA - Lowest Common Ancestor】
专业跟队形
唯一一个有$\LaTeX$的
裸的$LCA$,我用的是$Tarjan~LCA$,注意两点相同特判
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=;
struct edge{
int next,to;
}e[maxn],q[maxn<<];
int n,m,s,head[maxn],cnt,ans[maxn],heads[maxn],f[maxn],cntc;
bool vis[maxn];
int find(int x)
{
return x==f[x]?x:f[x]=find(f[x]);
}
void add(int x,int y)
{
e[++cnt].next=head[x];
e[cnt].to=y;
head[x]=cnt;
}
void adds(int x,int y)
{
q[++cnt].next=heads[x];
q[cnt].to=y;
heads[x]=cnt;
}
void tarjan(int x,int pre)
{
for(int v,i=head[x];i;i=e[i].next)
{
if((v=e[i].to)==pre)
continue;
tarjan(v=e[i].to,x);
int f1=find(x),f2=find(v);
if(f1!=f2)
f[f2]=f1;
}
for(int i=heads[x];i;i=q[i].next)
if(vis[q[i].to])
ans[(i+)>>]=find(q[i].to);
vis[x]=;
}
int main()
{
scanf("%d",&s);
while(s--)
{
scanf("%d",&n);
cnt=;
memset(head,,sizeof(head));
memset(heads,,sizeof(heads));
memset(vis,,sizeof(vis));
for(int x,num,i=;i<=n;i++)
{
scanf("%d",&num);
for(int j=;j<=num;j++)
{
scanf("%d",&x);
add(i,x);
}
}
scanf("%d",&m);
cnt=;
for(int x,y,i=;i<=m;i++)
{
scanf("%d%d",&x,&y);
if(x==y)
ans[i]=x;
adds(x,y),adds(y,x);
}
for(int i=;i<=n;i++)
f[i]=i;
tarjan(,);
printf("Case %d:\n",++cntc);
for(int i=;i<=m;i++)
printf("%d\n",ans[i]);
}
return ;
}
SP14932 【LCA - Lowest Common Ancestor】的更多相关文章
- 洛谷 SP14932 LCA - Lowest Common Ancestor
洛谷 SP14932 LCA - Lowest Common Ancestor 洛谷评测传送门 题目描述 A tree is an undirected graph in which any two ...
- LeetCode 【235. Lowest Common Ancestor of a Binary Search Tree】
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- SP14932 LCA - Lowest Common Ancestor
Description: 一棵树是一个简单无向图,图中任意两个节点仅被一条边连接,所有连通无环无向图都是一棵树.\(-Wikipedia\) 最近公共祖先(\(LCA\))是--(此处省去对\(LCA ...
- 寻找二叉树中的最低公共祖先结点----LCA(Lowest Common Ancestor )问题(递归)
转自 剑指Offer之 - 树中两个结点的最低公共祖先 题目: 求树中两个节点的最低公共祖先. 思路一: ——如果是二叉树,而且是二叉搜索树,那么是可以找到公共节点的. 二叉搜索树都是排序过的,位于左 ...
- LeetCode 235. Lowest Common Ancestor of a Binary Search Tree (二叉搜索树最近的共同祖先)
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- 【LeetCode】236. Lowest Common Ancestor of a Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode 236】Lowest Common Ancestor of a Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- 【LeetCode 235】Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- 【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree
题目: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in th ...
随机推荐
- C# 妈妈再打我一下生成器
设计背景 网上很火的一个"妈妈再打我一下"的漫画图片,给了网友无限的想象发挥空间,此小程序可以给图片添加配文的形式,快速生成图片 设计思路 GDI+ 绘图技术,在图片基础上添加文字 ...
- SP1043 GSS1
题目链接 简单说就是带修的查询区间最大子段和,用线段树维护即可 对于每个区间,我们肯定要记录它的最大子段和\(v\),但是怎么维护呢? 我们可以记录下从区间左端点开始的最大子段和\(v1\),从右端点 ...
- linux下MySQL使用方法
一.登录MySQL 登录MySQL的命令是mysql, mysql 的使用语法如下: mysql [-u username] [-h host] [-p[password]] [dbname] us ...
- 【刷题】BZOJ 2151 种树
Description A城市有一个巨大的圆形广场,为了绿化环境和净化空气,市政府决定沿圆形广场外圈种一圈树.园林部门得到指令后,初步规划出n个种树的位置,顺时针编号1到n.并且每个位置都有一个美观度 ...
- 【Revit API】创建相机视角
在Revit中有一个相机功能可以以相机视角产生一个视图.一开始我在Revit2016的API文档中找关键词Camera,但是没什么收获. 其实这个相机功能的真正核心是创建透视视图:View3D.Cre ...
- 【转】Keil ARM开发 error L6236E错误解决
顺利创建了第一个Keil工程却发现不能完成链接,出现了一个下面这样的报错: .\Objects\demo_simple.sct(7): error: L6236E: No section matche ...
- 在“安装”阶段发生异常。 System.Security.SecurityException: 未找到源,但未能
写Windows服务的时候,运行了一下,就是没反应,命令框一闪而过,查了一下异常,大致是题目的那样.原因是因为权限不足.但是在网上搜的方法都不顶用. 解决方法如下: (1)以管理员身份运行CMD: ( ...
- SQL记录-PLSQL触发器
PL/SQL触发器 触发器是存储程序,它会自动执行或发射当一些事件发生.触发器,事实上,写入响应于以下任一事件将被执行: 数据库操作(DML)语句(DELETE,INSERT,UPDATE或) 数据库 ...
- redis添加systemctl服务
1.编辑systemctl服务配置文件 vim /lib/systemd/system/redis.service 2.内容如下 [Unit]Description=RedisAfter=networ ...
- Easy2Boot-小清新教程
Author:KillerLegend Date:2014.8.14 From:http://www.cnblogs.com/killerlegend/p/3913614.html 之所以说是小清新, ...