Problem B: Countdown
Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88443#problem/B

Description

Ann Sister owns a genealogical database service, which maintains family tree history for her clients. When clients login to the system, they are presented with a variety of services: searching, printing, querying, etc. One recent question that came up which the system was not quite prepared for was the following: “Which member of my family had the most grandchildren?” The client who posed this question eventually had to answer it by manually searching the family tree database herself. Ann decided to have software written in case this question (or ones similar to it asking for great-grandchildren, or great-great-grandchildren, etc.) is asked in the future.

Input

Input will consist of multiple test cases. The first line of the input will contain a single integer indicating the number of test cases. Each test case starts with a single line containing two positive integers n and d, where n indicates the number of lines to follow containing information about the family tree, and d indicates the specific question being asked about the tree: if d = 1, then we are interested in persons with the most children (1 generation away); if d = 2, then we are interested in persons with the most grandchildren (2 generations away), and so on. The next n lines are of the form name m dname1 dname2 ... dnamem where name is one of the family members’ names, m is the number of his/her children, and dname1 through dnamem are the names of the children. These lines will be given in no particular order. You may assume that all n lines describe one single, connected tree. There will be no more than 1000 people in any one tree, and all names will be at most 10 characters long.

Output

For each test case, output the three names with the largest number of specified descendants in order of number of descendants. If there are ties, output the names within the tie in alphabetical order. Print fewer than three names if there are fewer than three people who match the problem criteria (you should not print anyone’s name who has 0 of the specified descendants), and print more than three if there is a tie near the bottom of the list. Print each name one per line, followed by a single space and then the number of specified descendants. The output for each test case should start with the line Tree i: where i is the test case number (starting at 1). Separate the output for each problem with a blank line.

Sample Input

3 8 2 Barney 2 Fred Ginger Ingrid 1 Nolan Cindy 1 Hal Jeff 2 Oliva Peter Don 2 Ingrid Jeff Fred 1 Kathy Andrea 4 Barney Cindy Don Eloise Hal 2 Lionel Mary 6 1 Phillip 5 Jim Phil Jane Joe Paul Jim 1 Jimmy Phil 1 Philly Jane 1 Janey Joe 1 Joey Paul 1 Pauly 6 2 Phillip 5 Jim Phil Jane Joe Paul Jim 1 Jimmy Phil 1 Philly Jane 1 Janey Joe 1 Joey Paul 1 Pauly

Sample Output

Tree 1: Andrea 5 Don 3 Cindy 2 Tree 2: Phillip 5 Jane 1 Jim 1 Joe 1 Paul 1 Phil 1 Tree 3: Phillip 5

HINT

题意

给你n个父子或者母子关系,然后让你求出前三多的第n代儿子的人是谁

题解

就是建一棵树,然后数据范围很小,想怎么弄就怎么弄

dfs搞一搞就好了

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200051
#define mod 10007
#define eps 1e-9
int Num;
//const int inf=0x7fffffff; //нчоч╢С
const int inf=0x3f3f3f3f;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************** map<string,int> H;
int n,d;
string s1,s2;
int tot=;
vector<int> E[];
struct node
{
string s;
int num;
int dnum;
};
bool cmp(node a,node b)
{
if(a.num==b.num)
return a.s<b.s;
return a.num>b.num;
}
node fam[];
int vis[];
int get_id(string s)
{
if(H[s]==)
{
H[s]=tot;
fam[tot].s=s;
fam[tot].num=;
tot++;
}
return H[s];
}
int solve(int x,int d)
{
if(d==)
return ;
int ans=;
for(int i=;i<E[x].size();i++)
ans+=solve(E[x][i],d-);
return ans;
}
int main()
{
//freopen("in.txt","r",stdin);
int t=read();
for(int cas=;cas<=t;cas++)
{
int n=read(),d=read();
tot=;
memset(vis,,sizeof(vis));
H.clear();
for(int i=;i<;i++)
E[i].clear();
for(int i=;i<n;i++)
{
cin>>s1;
int k=read();
for(int j=;j<k;j++)
{
cin>>s2;
E[get_id(s1)].push_back(get_id(s2));
}
}
for(int i=;i<tot;i++)
{
fam[i].num=solve(i,d);
}
sort(fam+,fam+tot,cmp);
printf("Tree %d:\n",cas);
int cc=inf;
for(int i=;i<=min(,tot-);i++)
{
if(fam[i].num==)
break;
vis[i]=;
cc=fam[i].num;
cout<<fam[i].s<<" ";
printf("%d\n",fam[i].num);
}
for(int i=;i<tot;i++)
{
if(vis[i]==)
continue;
if(fam[i].num<cc)
break;
if(fam[i].num==cc)
{
cout<<fam[i].s<<" ";
printf("%d\n",fam[i].num);
}
}
printf("\n");
}
}

Codeforces Gym 100650B Countdown DFS的更多相关文章

  1. Codeforces Gym 100650B Countdown (离线)

    题目链接:http://codeforces.com/gym/100650 根据给出的树和d,求出一些结点,这些结点形成子树的第d层结点数应该尽量多,具体要求可以参考题目. dfs一个结点前保存询问深 ...

  2. Codeforces Gym 100463D Evil DFS

    Evil Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Descr ...

  3. Codeforces Gym 101252D&&floyd判圈算法学习笔记

    一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...

  4. Codeforces Gym 101190M Mole Tunnels - 费用流

    题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...

  5. Codeforces Gym 101623A - 动态规划

    题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...

  6. 【Codeforces Gym 100725K】Key Insertion

    Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ...

  7. Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】

     2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...

  8. codeforces gym 100553I

    codeforces gym 100553I solution 令a[i]表示位置i的船的编号 研究可以发现,应是从中间开始,往两边跳.... 于是就是一个点往两边的最长下降子序列之和减一 魔改树状数 ...

  9. [Codeforces 1214D]Treasure Island(dfs)

    [Codeforces 1214D]Treasure Island(dfs) 题面 给出一个n*m的字符矩阵,'.'表示能通过,'#'表示不能通过.每步可以往下或往右走.问至少把多少个'.'变成'#' ...

随机推荐

  1. Oracle权限一览表

    权限 所能实现的操作 分析 ANALYZE ANY  分析数据库中的任何表.簇或索引 审计 AUDIT ANY  审计数据库中的任何模式对象 AUDIT SYSTEM  启用与停用语句和特权的审计选项 ...

  2. C# chart控件绘制曲线

    在.NET中以前经常用GDI去绘制,虽然效果也不错,自从.NET 4.0开始,专门为绘制图表而生的Chart控件出现了,有了它,就可以轻松的绘制你所需要的曲线图.柱状图什么的了. using Syst ...

  3. C# chart,有关如何在鼠标移动到Series上时显示节点及数据 (有待继续更新)

    一.效果与思路 效果: 解决方案1 用chart的mousemove时间,实时跟踪鼠标最近的X轴的位置,然后把cursorX设置到那个位置上,让用户知道我是选的那一个X的值,同时用tooltip显示该 ...

  4. Dapper.net 在Parameterized时对于String的扩展(转)

    虽然Dapper通过提供的DbString本身支持对于String的指定Parameterized,但这方法明显不够,当Insert时,我们更希望是把一个Poco直接传递过去,而不是来new一个匿名函 ...

  5. Filezilla中文字符文件看不到或显示乱码的解决办法

    Filezilla确实是跨平台的好软件,可之前我就在ubuntu下郁闷为什么看坛子FTP里竟然是空的.最近换MAC版的FZ结果还是这样就奇怪了. 后来想Filezilla应该是支持字符集转换的,所以在 ...

  6. 《Python核心编程》 第六章 序列 - 课后习题

    课后习题 6–1.字符串.string 模块中是否有一种字符串方法或者函数可以帮我鉴定一下一个字符串是否是另一个大字符串的一部分? 答:成员关系操作符(in.not in) import string ...

  7. qt集成到vs2010

    因为要正式开始学习图形处理相关的知识了,所以要搭建开发平台.我所要用到的平台主要是vs和qt.虽然现在已经出了vs2013了,并且外观看起来特别漂亮,但是我所要用到的qt4版本中,没有对于的qtAdd ...

  8. Windows安装pomelo过程

    安装总要出点状况的.操作系统是win7 64bit. 为了保证顺利,打开的是VS2012命令行提示.运行 npm install -g pomelo 经过一系列输出,最后安装提示完成了.但是输入 po ...

  9. sgu 194 Reactor Cooling(有容量上下界的无源无汇可行流)

    [题目链接] http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20757 [题意] 求有容量上下界的无源无汇可行流. [思路] ...

  10. CORBA

    公共对象请求代理体系结构(Common Object Request Broker Architecture)