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 ...
随机推荐
- Windows phone 8 学习笔记
Windows phone 8 学习笔记(1) 触控输入 http://www.apkbus.com/android-138547-1-1.html Windows phone 8 学习笔记(2) ...
- Swift - 跑酷游戏开发(SpriteKit游戏开发)
一,下面演示了如何开发一个跑酷游戏,实现的功能如下: 1,平台工厂会不断地生成平台,并且向左移动.当平台移出游戏场景时就可将其移除. 2,生成的平台宽度随机,高度随机.同时短平台踩踏的时候会下落. 3 ...
- 一个关于native sql的程序
*&---------------------------------------------------------------------* *& Report ZHR_BPM11 ...
- [概念] js的函数节流和throttle和debounce详解
js的函数节流和throttle和debounce详解:同样是实现了一个功能,可能有的效率高,有的效率低,这种现象在高耗能的执行过程中区分就比较明显.本章节一个比较常用的提高性能的方式,通常叫做&qu ...
- Flash Builder 条件编译的实现
最近项目需要开发多个版本, 而Flash又没有像C++ 那样的 #ifdef, 来让一套代码支持多个版本的编译发布; 经过研究, 终于知道Flash Builder如何支持条件编译: 1. 在项目 ...
- HDU 4876 ZCC loves cards _(:зゝ∠)_ 随机输出保平安
GG,,,g艹 #include <cstdio> #include <iostream> #include <algorithm> #include <st ...
- 运行yum报错Error: Cannot retrieve metalink for reposit
http://www.netpc.com.cn/593.html 运行yum报错Error: Cannot retrieve metalink for reposit 今天给Centos通过rpm - ...
- iOS_时间相关
计算两个时间之差,并以倒计时的方式显示: 比如:剩余XX天XX小时XX分XX秒 效果图例如以下: NSDate的分类: 使用场景: watermark/2/text/aHR0cDovL2Jsb2cuY ...
- Apache commons-net用法的一个示例
jar: commons-io-2.4.jarcommons-net-3.3.jar Code: import java.io.File; import java.io.FileInputStream ...
- Zeroonepack coming~^.^
今天抓的四道DP做完了==三道是用背包做的,突然想起来背包知识点总结还没做~反正时间还早..把01背包和完全背包小结了吧~~福利来啦~~噶呜~ 01背包: 基本思路: 01背包问题是最广为人知的动态规 ...