Cloesest Common Ancestors
Cloesest Common Ancestors
题目大意:给出一个n个节点的树,m组询问求两点LCA。
注释:n<=900.
想法:这题一看,我去,这不傻题吗?一看读入方式,完了,懵逼了... ...这题是考读入啊一大堆乱七八糟的东西,真正有用的只有里面的数... ...然后,我学了两个比较有用的东西。
1.scanf("%*d");其中,*表示读完就删,所以可以用来处理本题。
2.强大的nc+read(),在此不做介绍。
总之,初学LCA,这是另一道朴素的LCA,用桶记录即可。
最后,附上丑陋的代码... ...
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 1000
using namespace std;
int to[N],nxt[N],tot,fa[N],deep[N],barrel[N];
int head[N];
bool v[N];
// inline char nc()
// {
// static char buf[100000],*p1,*p2;
// return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
// }
// inline void read(int &x)
// {
// x=0;char c=nc();
// while(!isdigit(c))c=nc();
// while(isdigit(c))x=x*10+c-'0',c=nc();
// }
inline void add(int x,int y)
{
to[++tot]=y;
nxt[tot]=head[x];
head[x]=tot;
}
void dfs(int x,int pre,int step)
{
fa[x]=pre;
deep[x]=step;
for(int i=head[x];i;i=nxt[i])
{
dfs(to[i],x,step+);
}
}
inline int lca(int x,int y)
{
if(deep[x]<deep[y]) swap(x,y);
while(deep[x]>deep[y]) x=fa[x];
while(x!=y) x=fa[x],y=fa[y];
return x;
}
int main()
{
int n,m;
int a,b;
char s[];
while(~scanf("%d",&n))
{
tot=;
memset(head,,sizeof(head));
memset(v,false,sizeof(v));
memset(barrel,,sizeof(barrel)); // scanf("%d",&n);
for(int i=;i<=n;i++)
{
// read(a);
scanf("%*[^0-9]%d",&a);
// read(m);
scanf("%*[^0-9]%d",&m);
for(int i=;i<=m;i++)
{
scanf("%*[^0-9]%d",&b);
add(a,b);
v[b]=;
}
}
int root;
for(int i=;i<=n;i++)
{
if(!v[i])
{
root=i;
break;
}
}
dfs(root,root,);
scanf("%*[^0-9]%d",&m);
for(int i=;i<=m;i++)
{
scanf("%*[^0-9]%d",&a);
scanf("%*[^0-9]%d",&b);
barrel[lca(a,b)]++;
}
for(int i=;i<=n;i++)
{
if(barrel[i]!=) printf("%d:%d\n",i,barrel[i]);
}
scanf("%*[^0-9]");
}
}
小结:LCA的朴素算法还是很有用的啊... ...
错误:1.在读取链式前向星时一定要记得是i=nxt[i]而不是nxt[i]。
2.最后仍然会剩余一个括号,别忘记了它会影响下一组数据。
Cloesest Common Ancestors的更多相关文章
- POJ 1330 Nearest Common Ancestors(Targin求LCA)
传送门 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26612 Ac ...
- [最近公共祖先] POJ 1330 Nearest Common Ancestors
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 27316 Accept ...
- POJ 1330 Nearest Common Ancestors
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14698 Accept ...
- POJ1330 Nearest Common Ancestors
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24587 Acce ...
- POJ 1470 Closest Common Ancestors
传送门 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 17306 Ac ...
- poj----(1470)Closest Common Ancestors(LCA)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 15446 Accept ...
- poj 1330 Nearest Common Ancestors LCA
题目链接:http://poj.org/problem?id=1330 A rooted tree is a well-known data structure in computer science ...
- POJ 1330 Nearest Common Ancestors(Tree)
题目:Nearest Common Ancestors 根据输入建立树,然后求2个结点的最近共同祖先. 注意几点: (1)记录每个结点的父亲,比较层级时要用: (2)记录层级: (3)记录每个结点的孩 ...
- 【POJ1330】Nearest Common Ancestors(树链剖分求LCA)
Description A rooted tree is a well-known data structure in computer science and engineering. An exa ...
随机推荐
- Ext Designer生成表格
1.生成表格代码 Ext.MyPanel=Ext.extend(Ext.Panel ,{ xtype:"panel", title:"我的面板", width: ...
- PHP常见错误
1.关于单引号和双引号的区别. $sql="insert into tableName values ('".$name."','".$email." ...
- HighCharts之2D带Label的折线图
HighCharts之2D带Label的折线图 1.HighCharts之2D带Label的折线图源码 LineLabel.html: <!DOCTYPE html> <html&g ...
- Java Web项目(Extjs)报错五
1. Java Web项目(Extjs)报错五 具体报错如下: usage: java org.apache.catalina.startup.Catalina [ -config {pathname ...
- 【mongodb系统学习之三】进入mongodb shell
三. 进入mongodb shell(数据库操作界面) : 1).在mongodb的bin目录下输入./mongo,默认连接test数据库,连接成功会显示数据库版本和当前连接的数据库名,如图: 2). ...
- Java Breakpoint
1.错误描述 Java Breakpoint Unable to install breakpoint in com.you.humb.web.commom.dao.impl.ExportDaoImp ...
- [APIO2009]会议中心
[APIO2009]会议中心 题目大意: 原网址与样例戳我! 给定n个区间,询问以下问题: 1.最多能够选择多少个不相交的区间? 2.在第一问的基础上,输出字典序最小的方案. 数据范围:\(n \le ...
- NOIP2011题解
NOIP2011其实早就做完了....一直懒得写.... Day1 T1铺地毯 我什么都不想说,不会做您就没必要接着看了.... #include<iostream> using name ...
- [BZOJ1061] [Noi2008] 志愿者招募 (费用流)
Description 申奥成功后,布布经过不懈努力,终于成为奥组委下属公司人力资源部门的主管.布布刚上任就遇到了一个难 题:为即将启动的奥运新项目招募一批短期志愿者.经过估算,这个项目需要N 天才能 ...
- 厉害了!阿里安全图灵实验室在ICDAR2017 MLT竞赛刷新世界最好成绩
近日,阿里安全图灵实验室(Alibaba Turing Lab)的ATL Cangjie OCR算法在ICDAR2017的MLT(Competition on Multi-lingual scene ...