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 ...
随机推荐
- 基于visual Studio2013解决面试题之0304镜像二叉树
题目
- Passenger/Nginx/Debian快速部署Rails
安装所需的linux包 sudo apt-get install build-essential bison openssl libreadline6 libreadline6-dev curl gi ...
- MVC3和MVC4中CRUD操作
MVC3中EF实现的CRUD操作 public class HomeController : Controller { // // GET: /Home/ CarModelContainer db = ...
- Lucene.Net 2.3.1开发介绍 —— 四、搜索(一)
原文:Lucene.Net 2.3.1开发介绍 -- 四.搜索(一) 既然是内容筛选,或者说是搜索引擎,有索引,必然要有搜索.搜索虽然与索引有关,那也只是与索引后的文件有关,和索引的程序是无关的,因此 ...
- Lucene.Net 2.3.1开发介绍 —— 简介
原文:Lucene.Net 2.3.1开发介绍 -- 简介 Lucene.Net是Lucene在dot net平台上的移植版本.它的功能与Lucene一样,都是用来提供一组API,让我们能快速开发自己 ...
- java面向对象下:Java数据库编程
19.Java数据库编程: JDBC概述: JDBC(Java Database Connection)是java中提供的一套数据库编程API,它定义了一套用来访问数据库的标准Java类 ...
- HealthKit教程 Swift版:锻炼信息
原文:HealthKit Tutorial with Swift: Workouts 作者:Ernesto García 译者:Mr_cyz ) 欢迎回到我们的HealthKit系列教程! 在我们系列 ...
- HDU4911-Inversion(树状数组)
Inversion Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Tota ...
- 三次握手wireshark抓包分析,成功握手和失败握手
启动 点击start出现下面的对话框 wireshark是捕获机器上的 某一块网卡的网络包,当机器上有多块网卡的时候,需要选择一个网卡进行捕获操作. 选择网卡 >主页面上,直接点击选中后star ...
- rsync使用指南
考虑到服务器数据的安全,我考虑增加一台备份服务器,通过数据同步,达到较好的冗余. linux下有非常好的一个命令rsync可以实现差异备份,下面就说说它的用法:ubuntu缺省安装的安装中,rsync ...