专业跟队形

唯一一个有$\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. wordApp.Documents.Open 未将对象引用实例

    wordApp.Documents.Open (.........),当我打开的是.docx,能正常打开当是.doc时,却返回空置,显示失败,未能找到文件.......,但其实文件都在 解决方案 WO ...

  2. JVM学习笔记(三):类文件结构

    代码编译的结果从本地机器码转变为字节码,是存储格式发展的一小步,却是编程语言发展的一大步. 实现语言无关性的基础是虚拟机和字节码存储格式.Java虚拟机不和包括Java在内的任何语言绑定,只与&quo ...

  3. 拿到一个崭新的Linux!

    买了服务器,这次买了半年,不能浪费了! 0.准备工作: l查看Ip:ifconfig  ip addr 不能上网? 执行以下: vi /etc/sysconfig/network-scripts/if ...

  4. Spring中ClassPathXmlApplication与FileSystemXmlApplicationContext的区别以及ClassPathXmlApplicationContext 的具体路径

    一.ClassPathXmlApplicationContext 的具体路径 String s[] = System.getProperty("java.class.path"). ...

  5. 【bzoj1089】严格n元树

    Description 如果一棵树的所有非叶节点都恰好有n个儿子,那么我们称它为严格n元树.如果该树中最底层的节点深度为d(根的深度为0),那么我们称它为一棵深度为d的严格n元树.例如,深度为2的严格 ...

  6. zabbix agent安装(三)

    转载于https://mp.weixin.qq.com/s/33ab-JLoRfMkeI4aZDciJQ 前一篇文章介绍了zabbix server安装,这篇文章主要讲解zabbix agent安装以 ...

  7. 烦人的IE7、8,半透明滤镜(filter:alpha)失效、png半透明失效的解决办法

    在项目中的问题,之前用的是用IETest测试IE7,8发现背景透明设置无效,后来找文章解决!看了一些资料,做下总结. 几种IE半透明CSS样式 IE8里可以这样写 -ms-filter:”progid ...

  8. vue中assets文件夹与static文件夹的区别

    1.如果这些产品图片文件“万年不变”,放在 /static 目录里,(不需要使用require将这些图片作为模块来引用) var products = [{ img: '/static/img/pro ...

  9. Java远程访问接口的几种方式

    一.Java访问远程url接口并获取结果 1.原生JavaAPI获取 package com.util; import java.io.DataOutputStream; import java.io ...

  10. 基于docker的spark-hadoop分布式集群之一: 环境搭建

    一.软件准备 1.基础docker镜像:ubuntu,目前最新的版本是18 2.需准备的环境软件包: (1) spark-2.3.0-bin-hadoop2.7.tgz (2) hadoop-2.7. ...