【Tarjan,LCA】【3-21个人赛】【problemD】
Problem D
Time Limit : 6000/3000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 249 Accepted Submission(s) : 32
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
Input
对于每一组测试数据:
第一行是一个整数n,q(3<=n,q<=100000),n表示灯泡总数,q表示查询个数。
接下来的n-1行,每行2个整数x,y(1<=x,y<=n),表示灯泡x和灯泡y的阳极相连。(数据保证合法,是一棵树)
接下来的q行,每行3个整数,a,b,c(1<=a,b,c<=n,数据保证合法,灯泡a和灯泡b之间有边且a不等于b)表示灯泡a和灯泡b之间阳极连线断开的话,在c的阳极加一个电压。
Output
Sample Input
1
3 1
1 2
2 3
1 2 3
Sample Output
2
算出 a到c距离,b到c距离 ,算距离用LCA
LCA可以考虑离线的Tarjan算法
Tarjan算法介绍参考这个
http://www.cnblogs.com/ylfdrib/archive/2010/11/03/1867901.html
1.注意反向边可以通过 s^1 得到
2.少用#define maxn 1000+5 具体看置顶
代码如下“:
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#define oo 0x13131313
#define maxn 100010
using namespace std;
int n,q;
struct Edge{int to,ans,next;
void GET(int a,int b,int c){to=a,ans=b,next=c;}
};
struct Node{int first,deep;};
Edge E[maxn*6],EE[maxn*6];
Node Tree[maxn],Q[maxn];
int _E=0,_EE=0;
int a[maxn],b[maxn],c[maxn],visit[maxn],father[maxn];
void init()
{
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
}
void CLEAR()
{
for(int i=1;i<=n;i++)
{
Tree[i].first=-1;
Q[i].first=-1;
}
memset(visit,0,sizeof(visit));
_E=0;_EE=0;
for(int i=1;i<=100001;i++)
father[i]=i;
}
void Link(int u,int v)
{
E[_E].GET(v,0,Tree[u].first);Tree[u].first=_E++;
E[_E].GET(u,0,Tree[v].first);Tree[v].first=_E++;
}
void Link2(int u,int v)
{
EE[_EE].GET(v,0,Q[u].first);Q[u].first=_EE++;
EE[_EE].GET(u,0,Q[v].first);Q[v].first=_EE++;
}
int find(int x)
{
int r=x;
while(father[r]!=r)
{
r=father[r];
}
int i=x;
while(i!=r)
{
int j=father[i];
father[i]=r;
i=j;
}
return father[x];
}
void input()
{
int x,y;
cin>>n>>q;
CLEAR();
for(int i=1;i<=n-1;i++)
{
scanf("%d%d",&x,&y);
Link(x,y);
}
for(int i=1;i<=q;i++)
{
scanf("%d%d%d",&a[i],&b[i],&c[i]);
Link2(a[i],c[i]);
Link2(b[i],c[i]);
}
}
void Tarjan(int u,int deep)
{
visit[u]=1;
Tree[u].deep=deep;
for(int p=Q[u].first;p!=-1;p=EE[p].next)
if(visit[EE[p].to]==1)
EE[p^1].ans=EE[p].ans=find(EE[p].to); int t=0;
for(int p=Tree[u].first;p!=-1;p=E[p].next)
{
if(visit[E[p].to]==0)
{
Tarjan(E[p].to,deep+1);
father[E[p].to]=u;
}
}
}
void solve()
{
Tarjan(1,1);
int AA=0,BB=0,A=0,B=0;;
for(int i=1;i<=q;i++)
{
for(int p=Q[c[i]].first;p!=-1;p=EE[p].next)
{
if(EE[p].to==a[i]) A=EE[p].ans;
if(EE[p].to==b[i]) B=EE[p].ans;
}
AA=Tree[a[i]].deep+Tree[c[i]].deep-2*Tree[A].deep;
BB=Tree[b[i]].deep+Tree[c[i]].deep-2*Tree[B].deep;
if(AA<BB) printf("%d\n",a[i]);
else printf("%d\n",b[i]);
}
}
int main()
{
// init();
int T;
cin>>T;
while(T--)
{
input();
solve();
}
}
【Tarjan,LCA】【3-21个人赛】【problemD】的更多相关文章
- Tarjan+LCA【洛谷P2783】 有机化学之神偶尔会做作弊
[洛谷P2783] 有机化学之神偶尔会做作弊 题目背景 XS中学化学竞赛组教练是一个酷爱炉石的人. 有一天他一边搓炉石一边监考,而你作为一个信息竞赛的大神也来凑热闹. 然而你的化竞基友却向你求助了. ...
- poj 3417 Network(tarjan lca)
poj 3417 Network(tarjan lca) 先给出一棵无根树,然后下面再给出m条边,把这m条边连上,然后每次你能毁掉两条边,规定一条是树边,一条是新边,问有多少种方案能使树断裂. 我们设 ...
- 洛谷 P2783 有机化学之神偶尔会做作弊(Tarjan,LCA)
题目背景 LS中学化学竞赛组教练是一个酷爱炉石的人. 有一天他一边搓炉石一边监考,而你作为一个信息竞赛的大神也来凑热闹. 然而你的化竞基友却向你求助了. “第1354题怎么做”<--手语 他问道 ...
- hdu 5286 How far away ? tarjan/lca
How far away ? Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...
- POJ 3694 Network (tarjan + LCA)
题目链接:http://poj.org/problem?id=3694 题意是给你一个无向图n个点,m条边,将m条边连接起来之后形成一个图,有Q个询问,问将u和v连接起来后图中还有多少个桥. 首先用t ...
- poj3694Network(tarjan+lca)
http://poj.org/problem?id=3694 用tarjan算出桥,用lca算出公共祖先 把路上的边更新掉 原来的桥变为不是桥 看一解题报告感觉有一部分是不用加的 不知道是不是数据水 ...
- Tarjan LCA
强连通 迷宫城堡 Proving Equivalences Equivalent Sets Summer Holiday Intelligence System The King's Problem ...
- 【洛谷2416】泡芙(Tarjan+LCA)
题目描述 火星猫经过一番努力终于到达了冥王星.他发现冥王星有 \(N\) 座城市,\(M\) 条无向边.火星猫准备出发去找冥王兔,他听说有若干泡芙掉落在一些边上,他准备采集一些去送给冥王兔.但是火星猫 ...
- Tarjan & LCA 套题题目题解
刷题之前来几套LCA的末班 对于题目 HDU 2586 How far away 2份在线模板第一份倍增,倍增还是比较好理解的 #include <map> #include <se ...
随机推荐
- #include<math.h>
1.sin(a)类:a是弧度值: 2.abs(b):结果是b的绝对值: 3.exp(c):exp()用来计算以e为底的x次方值,即ex值,然后将结果返回.返回值: 返回e的x次方计算结果. 4.log ...
- Oracle数据库使用存储过程实现分页
注:本示例来源于韩顺平[10天玩转oracle数据库]视频教程 1.创建包同时创建游标 create or replace package pagingPackage is type paging_c ...
- windows下搭建及配置mantis缺陷管理工具
在windows XP 操作系统下,如何更快.更容易地搭建及配置mantis缺陷管理工具呢?以下是我实践的具体步骤: 一.安装mantis的前提环境,需要先安装Apache HTTP Server2. ...
- mysql5.7 二进制包安装
1. 下载包 wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.12-linux-glibc2.5-x86_64.tar.gz 2. 解 ...
- Foundation--NSDictionary+NSMutableDictionary
键值对 key(一般为字符串对象)---vaule(必须是对象) Person *p1 =[[Person alloc ]init]; Person *p2 =[[Person alloc ]init ...
- GridView事件DataBinding,DataBound,RowCreated,RowDataBound区别及执行顺序分析
严格的说,DataBinding,DataBound并不是GridView特有的事件,其他的控件诸如ListBox等也有DataBinding,DataBound事件. DataBinding事件MS ...
- 1021 Fibonacci Again (hdoj)
Problem Description There are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n) = F(n-1) ...
- 多个项目使用NET Core
.NET Core系列 :3 .使用多个项目 通过前面的两篇文章,我们已经知道如何创建新的项目,如何生成并运行我们的应用程序,也知道(大致) project.json 文件中的内容是什么意思.但大 ...
- SQL Server 查看当前活动的锁
第一步: 要查看活动中的锁,如果日前根本就没有活动中的锁怎么办,还好我会自己做一把. begin tran select * from dbo.Nums with(ta ...
- fragment低版本
http://bbs.csdn.net/topics/390271980 Fragment框架开发东西确实很方便,但是恼人的是从4.0才开始支持.以前的版本必须用兼容模式开发,本人在网上找了大量资料, ...