非常裸的模版题,只是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. 使用gradle打包jar包

    近期用android studio来做android开发的IDE,它是使用gradle来构建的,于是開始学习gradle. 如今有一个项目,里面有一个android-library的模块.我想在做re ...

  2. Swift - 在界面上生成81个随机红,灰色圆点(SpriteKit游戏开发)

    下面是生成一个“围住神经猫”游戏的初始场景: 1,界面下方会生成9*9共81个圆点,同时圆点内部添加文本标签显示索引 2,默认圆点为灰色,每行随机取两个点变为红色 3,奇数行和偶数行有一定的错位,错位 ...

  3. 关于在打包Jar文件时遇到的资源路径问题(二)

    在关于<关于在打包Jar文件时遇到的资源路径问题(一)>中,以及描述了当资源与可执行JAr分离时的资源路径代码的编写问题,后来想了想,为什么将<Java核心技术卷一>中的程序1 ...

  4. C#用正则表达式去掉Html中的script脚本和html标签

    原文 C#用正则表达式去掉Html中的script脚本和html标签 /// <summary>         /// 用正则表达式去掉Html中的script脚本和html标签     ...

  5. Html网页表格结构化标记的应用

    在讲网页表格的结构化标记之前,还是先看几幅图片. Html表格的结构化 所谓的结构化,正如上述第一副图所看到的,就是把我们的表格划分为三种:表头.表体.表尾.从而当我们在改动表体部分的时候,不会影响到 ...

  6. ZeroMQ:云计算时代最好的通讯库

    还在学socket编程吗?还在研究为什么epoll比select更好吗? 噢,不必了! 在复杂的云计算环境中,我们面临的难题远比这个复杂得多. 庞大的服务器集群作为计算云,对来来看或许只是一个简单的搜 ...

  7. c#(winform,webform通用)利用npoi将xls文件复制为xlsx文件(excel的修改,保存,包括excel2003-office2007+的处理)

    1.程序界面 每次需要处理excel文件的时候,都是去百度找方案,真是气一头火,今天好好总结一下,下次就不用度娘了. 我是用winform来试验的,因为winform比较方便测试,实际上只要是在.ne ...

  8. 【Demo 0008】标签控制器

    本章学习要点:       1.  了解标签控制器基础知识;       2.  掌握标签控制器层次结构;       3.  掌握标签控制器基本用法;       4.  掌握自定义标签控制器:   ...

  9. Test oracle db iops

    Today, i need to test one database's iops and do something for oracle db's io test. How to test the ...

  10. 第四题(迅雷笔试题):编写一个程序,开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC….依次递推。

    #include <iostream> #include <stdlib.h> #include <pthread.h> using namespace std; ...