非常裸的模版题,只是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】的更多相关文章

  1. poj1470 Closest Common Ancestors [ 离线LCA tarjan ]

    传送门 Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 14915   Ac ...

  2. POJ 1330 Nearest Common Ancestors(Tarjan离线LCA)

    Description A rooted tree is a well-known data structure in computer science and engineering. An exa ...

  3. POJ1470 Closest Common Ancestors

    LCA问题,用了离线的tarjan算法.输入输出参考了博客http://www.cnblogs.com/rainydays/archive/2011/06/20/2085503.htmltarjan算 ...

  4. POJ 1470 Closest Common Ancestors (LCA,离线Tarjan算法)

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 13372   Accept ...

  5. POJ 1470 Closest Common Ancestors(最近公共祖先 LCA)

    POJ 1470 Closest Common Ancestors(最近公共祖先 LCA) Description Write a program that takes as input a root ...

  6. POJ 1470 Closest Common Ancestors (LCA, dfs+ST在线算法)

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 13370   Accept ...

  7. POJ 1470 Closest Common Ancestors 【LCA】

    任意门:http://poj.org/problem?id=1470 Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000 ...

  8. poj----(1470)Closest Common Ancestors(LCA)

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 15446   Accept ...

  9. ZOJ 1141:Closest Common Ancestors(LCA)

    Closest Common Ancestors Time Limit: 10 Seconds      Memory Limit: 32768 KB Write a program that tak ...

随机推荐

  1. Oracle Patch Bundle Update

    一.相关知识介绍 以前只知道有CPU(Critical Patch Update)和PSU(Patch Set Update),不知道还有个Bundle Patch,由于出现了TNS-12531的BU ...

  2. <转载>如何解决子级用float浮动父级div高度不能自适应的问题

    转载:http://www.kwstu.com/ArticleView/divcss_2013101582430202 解决子级对象使用css float浮动 而父级div不能自适应高度,不能被父级内 ...

  3. Selenium WebDriver java 简单实例

    开发环境 JDK 下载地址: http://www.oracle.com/technetwork/java/javase/downloads/index.html Eclipse: 下载地址:http ...

  4. HDOJ 4883 TIANKENG’s restaurant

    称号: TIANKENG's restaurant Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Ja ...

  5. String,StringBuffer与StringBuilder差异??

    String 字符串常量StringBuffer 字符串变量(线程安全)StringBuilder 字符串变量(非线程安全) 简要地, String 类型和 StringBuffer 类型的主要性能差 ...

  6. 与众不同 windows phone (14) - Media(媒体)之音频播放器, 视频播放器, 与 Windows Phone 的音乐和视频中心集成

    原文:与众不同 windows phone (14) - Media(媒体)之音频播放器, 视频播放器, 与 Windows Phone 的音乐和视频中心集成 [索引页][源码下载] 与众不同 win ...

  7. A Very Easy Triangle Counting Game

    题意:在圆上取n个点,相邻两个点之间连线,(注意,n和1相邻),然后所有点对(i ,i+2)相连,问能形成的不同的三角形有多少个? 思路:找规律 n=3,cnt=1; n=4,cnt=8; n=5 c ...

  8. Android代码混淆和项目宣布步骤记录器

    原本放在一起Android项目与发布的文件相混淆.我突然想到,为什么不写博客,分享.有这篇文章的情况下,. Android代码混淆及项目公布步骤记录 一.清理代码中的调试信息,如Log.System. ...

  9. 忽然想到:QProcess的Read功能太强,如果有什么搞不定的,可以调用外部程序 good

    这样就可以用其它语言来无限扩展它的功能了,比如golang,比如Delphi

  10. openCV之头文件分析

    我们利用openCV开源库进行项目开发时,往往要牵涉到头文件的添加问题,而openCV中头文件众多,该如何选择呢?下面对openCV2.4.10的头文件进行一个简单的梳理,以便能够快速的添加对应的头文 ...