专业跟队形

唯一一个有$\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】的更多相关文章

  1. 洛谷 SP14932 LCA - Lowest Common Ancestor

    洛谷 SP14932 LCA - Lowest Common Ancestor 洛谷评测传送门 题目描述 A tree is an undirected graph in which any two ...

  2. 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 ...

  3. SP14932 LCA - Lowest Common Ancestor

    Description: 一棵树是一个简单无向图,图中任意两个节点仅被一条边连接,所有连通无环无向图都是一棵树.\(-Wikipedia\) 最近公共祖先(\(LCA\))是--(此处省去对\(LCA ...

  4. 寻找二叉树中的最低公共祖先结点----LCA(Lowest Common Ancestor )问题(递归)

    转自 剑指Offer之 - 树中两个结点的最低公共祖先 题目: 求树中两个节点的最低公共祖先. 思路一: ——如果是二叉树,而且是二叉搜索树,那么是可以找到公共节点的. 二叉搜索树都是排序过的,位于左 ...

  5. 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 ...

  6. 【LeetCode】236. Lowest Common Ancestor of a Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. 【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 ...

  8. 【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 ...

  9. 【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 ...

随机推荐

  1. 原理分析dubbo分布式应用中使用zipkin做链路追踪

    zipkin是什么 Zipkin是一款开源的分布式实时数据追踪系统(Distributed Tracking System),基于 Google Dapper的论文设计而来,由 Twitter 公司开 ...

  2. Delphi Try Except 实例

    //判断单位转换率 try um_rate := vp_mstr.F('vp_um_rate_d').AsFloat / vp_mstr.F('vp_um_rate_m').AsFloat; then ...

  3. ssh中文乱码解决

    在终端执行命令:export LC_ALL=zh_CN.GB2312;export LANG=zh_CN.GB2312是最有效的.=======================1.不管用那种ssh客户 ...

  4. Pycharm+Python+PyQt5使用

    https://www.cnblogs.com/dalanjing/p/6978373.html

  5. cxf 动态调用.

    import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache ...

  6. Steady Cow Assignment POJ - 3189 (最大流+匹配)

    Farmer John's N (1 <= N <= 1000) cows each reside in one of B (1 <= B <= 20) barns which ...

  7. Antenna Placement POJ - 3020 (最小边集覆盖)

    Antenna Placement Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10699   Accepted: 526 ...

  8. 使用alien命令让deb包和rpm包互相转换

    OS version: CentOS7 / Debian9 发现alien这个命令时很惊喜,之前在debian上安装etcd找不到安装包感觉很不科学,有了alien命令事情一下就变简单了. 这里以et ...

  9. MyBatis 源码分析——配置信息

    MyBatis框架的启动前期需要加载相关的XML配置信息.从官网上我们可以了解到他具有十几个节点.其中笔者认为比较重要的节点是settings节点.properties节点.environments节 ...

  10. 收藏:IPicture总结

    1.IPicture接口对象的创建方法1:直接通过文件创建LPCSTR szFileUrl; IPicture *pIPicture; OleLoadPicturePath(CComBSTR(szFi ...