POJ 1470 Closest Common Ancestors (模板题)(Tarjan离线)【LCA】
<题目链接>
题目大意:
给你一棵树,然后进行q次询问,然后要你统计这q次询问中指定的两个节点最近公共祖先出现的次数。
解题分析:
LCA模板题,下面用的是离线Tarjan来解决。并且为了代码的简洁,本代码用的是vector存图。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
;
vector<int>edge[N];
int query[N][N],father[N],count[N],indeg[N];
bool vis[N];
int n,m;
void init(){
;i<=n;i++)edge[i].clear();
memset(query,,sizeof(query));
memset(vis,false,sizeof(vis));
memset(count,,sizeof(count));
memset(indeg,,sizeof(indeg));
}
int find(int x){ //找到根节点
if(x!=father[x])
father[x]=find(father[x]);
return father[x];
}
void Tarjan(int u){
father[u]=u;
;i<edge[u].size();i++){ //得到该树上所有节点的父子关系
int v=edge[u][i];
Tarjan(v);
father[v]=u;
}
vis[u]=true;
;i<=n;i++)
if(vis[i] && query[u][i])
count[find(i)]+=query[u][i]; //最近公共祖先出现次数+1
}
int main(){
while(~scanf("%d",&n)){
init();
int u,v;
;i<n;i++){
scanf("%d:(%d)",&u,&m);
while(m--){
scanf(" %d",&v);
edge[u].push_back(v); //建立有向边
indeg[v]++; //统计入度,用于寻找根节点
}
}
scanf(" %d",&m);
;i<m;i++){
scanf(" (%d %d)",&u,&v);
query[u][v]++; //将代查询的节点也全部记录下来
query[v][u]++;
}
;i<=n;i++)
){
Tarjan(i);
break;
}
;i<=n;i++)
if(count[i])
printf("%d:%d\n",i,count[i]);
}
;
}
2018-10-21
POJ 1470 Closest Common Ancestors (模板题)(Tarjan离线)【LCA】的更多相关文章
- POJ 1470 Closest Common Ancestors(最近公共祖先 LCA)
POJ 1470 Closest Common Ancestors(最近公共祖先 LCA) Description Write a program that takes as input a root ...
- POJ 1470 Closest Common Ancestors (最近公共祖先LCA 的离线算法Tarjan)
Tarjan算法的详细介绍,请戳: http://www.cnblogs.com/chenxiwenruo/p/3529533.html #include <iostream> #incl ...
- POJ 1470 Closest Common Ancestors【近期公共祖先LCA】
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u013912596/article/details/35311489 题目链接:http://poj ...
- POJ 1470 Closest Common Ancestors 【LCA】
任意门:http://poj.org/problem?id=1470 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000 ...
- POJ 1470 Closest Common Ancestors (LCA,离线Tarjan算法)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 13372 Accept ...
- POJ 1470 Closest Common Ancestors (LCA, dfs+ST在线算法)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 13370 Accept ...
- POJ 1470 Closest Common Ancestors
传送门 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 17306 Ac ...
- poj——1470 Closest Common Ancestors
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 20804 Accept ...
- POJ 1470 Closest Common Ancestors【LCA Tarjan】
题目链接: http://poj.org/problem?id=1470 题意: 给定若干有向边,构成有根数,给定若干查询,求每个查询的结点的LCA出现次数. 分析: 还是很裸的tarjan的LCA. ...
随机推荐
- Confluence 6 选择一个默认的语言
管理员可以设置应用到你 Confluence 站点所有空间的默认语言.请注意,一个独立的用户可以在他们自己的属性中选择他们独立的语言属性. 设定默认的语言 在 Confluence 站点中修改默认的语 ...
- nginx之请求限制与连接限制
模块limit_conn_module与limit_req_module limit_conn_module语法 limit_req_module语法 附: http长连接与短连接
- linux 源码安装PHP
解压: 解压完: configure: configure成功: make: make完成: 安装完成!!! 测试: 需要./bin/php来运行php 想要任何目录输入PHP就能使用php 方法一: ...
- PyCharm+SVN
首先电脑安装svn,并且确svn/bin下面有svn.exe文件 没有bin/svn.exe解决方法: 重新打开TortoiseSVN安装文件-Modify-Next后在command line cl ...
- python 全栈开发,Day71(模型层-单表操作)
昨日内容回顾 1. {% include '' %} 2. extend base.html: <html> ..... ..... ..... {% block content%} {% ...
- 异常:Keyword not supported: 'data source'的解决办法
将连接字符串中的"换为“'”,一个单引号即可. 详细解释:https://blogs.msdn.microsoft.com/rickandy/2008/12/09/explicit-c ...
- linux基础练习题(2)
Linux命令作业(关卡二) 练习题1 理解操作系统的作用,以及各种操作系统的不同 要求: 为什么要有OS?没有OS能行吗?原因是什么? Linux内核指的是什么? Linux主要应用在哪些地方? 使 ...
- PV-UV-QPS
QPS:每秒查询率(Query Per Second) ,每秒的响应请求数,也即是最大吞吐能力.QPS = req/sec = 请求数/秒QPS统计方式 [一般使用 http_load 进行统计]QP ...
- pandas之系列操作(一)
1.读Excel: # coding=utf-8 import pandas as pd import pymysql sql_select =" xxxxx " con = py ...
- 用C语言实现窗口抖动
#include "stdafx.h" #include <stdio.h> #include<Windows.h> int main() { ; //休眠 ...