专业跟队形

唯一一个有$\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. Caffe使用: Ubuntu 14.04(x64) 从cuda 7.0 升级到 cuda8.0

    由于之前已经在Ubuntu 14.04 x64上面安装cuda7.0+caffe, 并且已经配置好,caffe也已经跑通. 但是最近需要使用Torch,而Torch对cuda的要求是8.0,因此决定对 ...

  2. NodeList & null

    NodeList & null querySelectorAll & querySelector let arr = document.querySelectorAll(`[data- ...

  3. terminal record & gif

    terminal record & gif ascii nema https://asciinema.org/ https://asciinema.org/docs/how-it-works ...

  4. CF 1070J Streets and Avenues in Berhattan

    DP的数组f其实开得不够大,应该开200000,但是它在cf上就是过了... 题意是把一堆字母分别分配到行和列. 分析一下,答案实际上只和n行中和m列中每种字母分配的个数有关.而且答案只和" ...

  5. MT【98】三元对称不等式

    评:这是一道浙江省省赛题,这里利用对称性,设$x\le y\le z$从而解决了问题.值得注意的是此处三元轮换对称正好也是完全对称,但如果变成一般的$n\ge4$元对称问题时,就不能设大小关系.事实上 ...

  6. alpine编译安装tengine,并使用supervisor启动

    Alpine是一个小型的linux系统,官方docker镜像只有不到5MB,非常适合作为容器镜像. Alpine Linux is a security-oriented, lightweight L ...

  7. 界面编程之QT窗口系统20180726

    /*******************************************************************************************/ 一.坐标系统 ...

  8. oracle执行update语句时卡住问题分析及解决办法

    转载:http://www.jb51.net/article/125754.htm 这篇文章主要介绍了oracle执行update语句时卡住问题分析及解决办法,涉及记录锁等相关知识,具有一定参考价值, ...

  9. k8s pod的4种网络模式最佳实战(externalIPs )

    [k8s]k8s pod的4种网络模式最佳实战(externalIPs )       hostPort相当于docker run -p 8081:8080,不用创建svc,因此端口只在容器运行的vm ...

  10. arcgis计算邻接矩阵

    求邻接矩阵 教程链接   http://m.blog.csdn.net/wan_yanyan528/article/details/49175673 (1) 将目标shp文件导出一份副本备用(以省级为 ...