POJ1470 Closest Common Ancestors 【Tarjan的LCA】
非常裸的模版题,只是Tarjan要好好多拿出来玩味几次
非常有点巧妙呢,tarjan,大概就是当前结点和它儿子结点的羁绊
WA了俩小时,,,原因是,这个题是多数据的(还没告诉你T,用scanf!=EOF来控制结束),更重要的是和这个和Codeforces不一样,Codeforces的多组数据好像会又一次開始程序似的,不用在程序里面写清零,但这个题是多数据用EOF来控制输入的,多数据在一个文件中都一次输进去了,所以要memset
btw,加上一点memset代码,多了700B代码。。。
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std;
const int MAXN=1111;
int n;
int in[MAXN];
vector<int> G[MAXN];
int ques[MAXN][MAXN];
bool vis[MAXN];
int fa[MAXN];
int countn[MAXN];
int father(int x)
{
if(x==fa[x])
return x;
return x=father(fa[x]);
}
void dfs(int x)
{
fa[x]=x;
for(int i=0;i<G[x].size();i++)
{
dfs(G[x][i]);
fa[G[x][i]]=x;
}
vis[x]=true;
for(int i=1;i<=n;i++)
{
if(ques[x][i]&&vis[i])
{
countn[father(i)]+=ques[x][i];
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("G:/1.txt","r",stdin);
freopen("G:/2.txt","w",stdout);
#endif
while(scanf("%d",&n)==1)
{
memset(ques,0,sizeof(ques));
memset(vis,0,sizeof(vis));
memset(fa,0,sizeof(fa));
memset(countn,0,sizeof(countn));
memset(in,0,sizeof(in));
for(int i=0;i<MAXN;i++)
{
G[i].clear();
}
for(int i=1;i<=n;i++)
{
int x,ynum;
scanf("%d:(%d)",&x,&ynum);
for(int j=1;j<=ynum;j++)
{
int y;
scanf(" %d",&y);
G[x].push_back(y);
in[y]++;
//G[y].push_back(x);
}
}
int quesnum;
scanf(" %d",&quesnum);
for(int i=1;i<=quesnum;i++)
{
int xx,yy;
scanf(" (%d %d)",&xx,&yy);
ques[xx][yy]++;
ques[yy][xx]++;
}
for(int i=1;i<=n;i++)
{
if(!in[i])
{
dfs(i);
break;
}
}
for(int i=1;i<=n;i++)
{
if(countn[i])
printf("%d:%d\n",i,countn[i]);
}
}
return 0;
}
POJ1470 Closest Common Ancestors 【Tarjan的LCA】的更多相关文章
- poj1470 Closest Common Ancestors [ 离线LCA tarjan ]
传送门 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 14915 Ac ...
- POJ 1330 Nearest Common Ancestors(Tarjan离线LCA)
Description A rooted tree is a well-known data structure in computer science and engineering. An exa ...
- POJ1470 Closest Common Ancestors
LCA问题,用了离线的tarjan算法.输入输出参考了博客http://www.cnblogs.com/rainydays/archive/2011/06/20/2085503.htmltarjan算 ...
- 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)
POJ 1470 Closest Common Ancestors(最近公共祖先 LCA) Description Write a program that takes as input a root ...
- 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 【LCA】
任意门:http://poj.org/problem?id=1470 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000 ...
- poj----(1470)Closest Common Ancestors(LCA)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 15446 Accept ...
- ZOJ 1141:Closest Common Ancestors(LCA)
Closest Common Ancestors Time Limit: 10 Seconds Memory Limit: 32768 KB Write a program that tak ...
随机推荐
- 【实战】静默安装-oracle 11.2.0.3 on centos 5.10
发现网上静默安装的文章非常多,乱七八糟,五花八门!来个扫盲的! centos 5.10 下安装oracle 11g_r2 ************************************* ...
- reduce个数究竟和哪些因素有关
reduce的数目究竟和哪些因素有关 1.我们知道map的数量和文件数.文件大小.块大小.以及split大小有关,而reduce的数量跟哪些因素有关呢? 设置mapred.tasktracker.r ...
- Delphi中使用GDI+进行绘图(1)
Delphi的VCL类库中,默认使用的是GDI绘图接口,该接口封装了Win32 GDI接口,能够满足基本的绘图功能,但如果要实现更高级的绘图功能,往往比较困难,GDI+是微软在GDI之后的一个图形接口 ...
- 怎么用CIFilter给图片加上各种各样的滤镜_1
近期在做对图像处理的app,而要对图像进行处理,CoreImage是离不开的框架. 上网Google了一下,大部分的博客都是仅仅讲了怎么给图像加上怀旧(CISepiaTone).模糊(CIBloom) ...
- ThinkPhp学习01
原文:ThinkPhp学习01 一.ThinkPHP的介绍 MVC M - Model 模型 工作:负责数据的操作 V - View 视图(模板 ...
- HNCU1100:彩票
http://hncu.acmclub.com/index.php?app=problem_title&id=111&problem_id=1100 题目描述 有一种彩票的玩法是从1~ ...
- ssh-copy-id -i ~/.ssh/id_rsa.pub admin@172.17.42.66
ssh-copy-id -i ~/.ssh/id_rsa.pub admin@172.17.42.66
- hdu Crazy Circuits
Crazy Circuits 题目: 给出一个电路板,从+极出发到负极. 如今给你电路板上的最小电流限制,要你在电流平衡的时候求得从正极出发的最小电流. 算法: 非常裸的有源汇最小流.安有源汇最大流做 ...
- OCP读书笔记(14) - 管理数据库性能
搜集统计信息 1. dbms_stats gather_schema_stats 1)option:有四个选项: a.gather empty:只分析目前还没有搜集过统计信息的表 SQL> co ...
- Cocos2d-x 3.1.1 Lua实例-AccelerometerTest(重力加速计)
Cocos2d-x 3.1.1 Lua实例-AccelerometerTest(重力加速计) 本篇博客介绍Cocos2d-x的第一个实例--重力加速计測试.效果图(注:这里无法模拟重力感应): --[ ...