一开始看到这题真的有点懵逼...一直在想着套算法,结果题解除了sort和dfs其他什么都没用到

  显然每次到达的一定都是叶子,先从根节点dfs一遍,按深度对叶子降序排序,按这个顺序向根节点dfs,路径上没有被之前的叶子遍历过的点就是到达这个叶子时未经过的点的个数,于是我们跑出所有叶子未经过点的个数再排序一次输出就行了

#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cmath>
#include<map>
#define ll long long
using namespace std;
const int maxn=,inf=1e9;
struct tjm{int too,pre;}e[maxn];
struct poi{int w,pos;}a[maxn],b[maxn];
int n,x,tot,cnt,root,t;
int last[maxn];
bool v[maxn];
void read(int &k)
{
int f=;k=;char c=getchar();
while(c<''||c>'')c=='-'&&(f=-),c=getchar();
while(c<=''&&c>='')k=k*+c-'',c=getchar();
k*=f;
}
inline bool cmp(poi a,poi b){return a.w==b.w?a.pos<b.pos:a.w>b.w;}
inline void add(int x,int y){e[++tot].too=y;e[tot].pre=last[x];last[x]=tot;}
inline void dfs(int x,int dep,int fa)
{
bool flag=;
for(int i=last[x];i;i=e[i].pre)
if(e[i].too!=fa)dfs(e[i].too,dep+,x),flag=;
if((!flag)&&x!=root)a[++cnt].pos=x,a[cnt].w=dep;
}
inline bool dfs2(int x,int fa)
{
if(x==root||v[x])return ;
for(int i=last[x];i;i=e[i].pre)
if(e[i].too!=fa)if(dfs2(e[i].too,x))return t++,v[x]=,;
return ;
}
int main()
{
read(n);read(root);
for(int i=;i<n;i++)read(x),add(x,i),add(i,x);
dfs(root,,-);sort(a+,a++cnt,cmp);
for(int i=;i<=cnt;i++)
{
t=;b[i].pos=a[i].pos;
dfs2(a[i].pos,-);
b[i].w=t;
}
sort(b+,b++cnt,cmp);
printf("%d\n",root);for(int i=;i<=cnt;i++)printf("%d\n",b[i].pos);
return ;
}

先分析,再做题!

51nod 1273 旅行计划(思维题)的更多相关文章

  1. 51nod 1273 旅行计划——思维题

    某个国家有N个城市,编号0 至 N-1,他们之间用N - 1条道路连接,道路是双向行驶的,沿着道路你可以到达任何一个城市.你有一个旅行计划,这个计划是从编号K的城市出发,每天到达一个你没有去过的城市, ...

  2. 【51Nod】1273 旅行计划 树上贪心

    [题目]51Nod 1273 旅行计划 [题意]给定n个点的树和出发点k,要求每次选择一个目的地旅行后返回,使得路径上未访问过的点最多(相同取编号最小),旅行后路径上所有点视为访问过,求旅行方案.\( ...

  3. csps模拟83最大异或和简单的括号序列旅行计划题解

    题面:https://www.cnblogs.com/Juve/articles/11733280.html 最大异或和: 简单博弈,小Q一定不会输,如果异或和为0,则平局,因为无论小Q如何拿,小T都 ...

  4. cogs 2. 旅行计划 dijkstra+打印路径小技巧

    2. 旅行计划 ★★   输入文件:djs.in   输出文件:djs.out   简单对比时间限制:3 s   内存限制:128 MB [题目描述] 过暑假了,阿杜准备出行旅游,他已经查到了某些城市 ...

  5. COGS 2. 旅行计划

    2. 旅行计划 ★☆   输入文件:djs.in   输出文件:djs.out   简单对比时间限制:3 s   内存限制:128 MB 过暑假了,阿杜准备出行旅游,他已经查到了某些城市的两两之间的距 ...

  6. zoj 3778 Talented Chef(思维题)

    题目 题意:一个人可以在一分钟同时进行m道菜的一个步骤,共有n道菜,每道菜各有xi个步骤,求做完的最短时间. 思路:一道很水的思维题, 根本不需要去 考虑模拟过程 以及先做那道菜(比赛的时候就是这么考 ...

  7. cf A. Inna and Pink Pony(思维题)

    题目:http://codeforces.com/contest/374/problem/A 题意:求到达边界的最小步数.. 刚开始以为是 bfs,不过数据10^6太大了,肯定不是... 一个思维题, ...

  8. Crash的旅行计划

    除草了.. Crash的旅行计划 [问题描述] 过不了多久,Crash就要迎来他朝思暮想的暑假.在这个暑假里,他计划着到火星上旅游.在火星上有N个旅游景点,Crash用1至N这N个正整数对这些景点标号 ...

  9. ZOJ 3829 贪心 思维题

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 现场做这道题的时候,感觉是思维题.自己智商不够.不敢搞,想着队友智商 ...

随机推荐

  1. 「日常训练」Ice Cave(Codeforces Round 301 Div.2 C)

    题意与分析(CodeForces 540C) 这题坑惨了我....我和一道经典的bfs题混淆了,这题比那题简单. 那题大概是这样的,一个冰塔,第一次踩某块会碎,第二次踩碎的会掉落.然后求可行解. 但是 ...

  2. hdu2098分拆素数和(素数+暴力)

    分拆素数和 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  3. Selenium自动化测试第二天(下)

    如有任何学习问题,可以添加作者微信:lockingfree 目录 Selenium自动化测试基础 Selenium自动化测试第一天(上) Selenium自动化测试第一天(下) Selenium自动化 ...

  4. [wirtting] top01 independent

    Do you agree or disagree with the following statement? At universities and colleges, sports and soci ...

  5. Idea Live Templates

    常用live templates 模板 注释 : * * @param $params$ * @return $return$ * $date$ $time$ chiyuanzhen743 */ lo ...

  6. Android开发-API指南-<receiver>

    <receiver> 英文原文:http://developer.android.com/guide/topics/manifest/receiver-element.html 采集(更新 ...

  7. 最全的Markdown语法

    目录 Markdown语法 多级标题 引用与注释 插入代码 行内代码 代码段 图片 超链接 行内超链接 参数式超链接 字体 表格 分割线 多级列表 无序列表 有序列表 多选框 LaTeX公式 行内La ...

  8. [C++] Class (part 2)

    Members that are const or reference must be initialized. Similary, members that are of a class type ...

  9. Python高级编程-itertoos模块

    Python的内建模块itertools提供了非常有用的用于操作迭代对象的函数. 首先我们看看itertools模块提供的几个“无限”迭代器, import itertools naturals = ...

  10. python中spilt()函数和os.path.spilt()函数区别

    Python中有split()和os.path.split()两个函数: split():拆分字符串.通过指定分隔符对字符串进行切片,并返回分割后的字符串列表. os.path.split():将文件 ...