https://codility.com/programmers/challenges/fluorum2014

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1273

http://blog.csdn.net/caopengcs/article/details/36872627

http://www.quora.com/How-do-I-determine-the-order-of-visiting-all-leaves-of-a-rooted-tree-so-that-in-each-step-I-visit-a-leaf-whose-path-from-root-contains-the-most-unvisited-nodes#

思路如曹博所说,先dfs记录离根的距离,来的方向的前驱。然后按距离排序,之后按此排序求有意义的增加距离。

直观感受是,如果两个叶子在一个分叉上,显然深的节点更先被访问,如果不在一个分叉上,那么先算深的也没什么损失。最后,按照这个权值再对叶子排序一次,就是所要的结果。

#include <iostream>
using namespace std;
void dfs(vector<vector<int>> &tree, vector<int> &parent, vector<int> &depth, vector<bool> &visited, int root, int dep) {
visited[root] = true;
depth[root] = dep;
for (int i = 0; i < tree[root].size(); i++) {
if (visited[tree[root][i]])
continue;
parent[tree[root][i]] = root;
dfs(tree, parent, depth, visited, tree[root][i], dep + 1);
}
} vector<int> solution(int K, vector<int> &T) {
int n = T.size();
vector<vector<int>> tree(n);
for (int i = 0; i < n; i++) {
if (T[i] != i) {
tree[i].push_back(T[i]);
tree[T[i]].push_back(i);
}
}
vector<int> parent(n);
vector<int> depth(n);
vector<bool> visited(n);
dfs(tree, parent, depth, visited, K, 0);
vector<vector<int>> cnt(n);
for (int i = 0; i < n; i++) {
cnt[depth[i]].push_back(i);
}
vector<int> ordered;
for (int i = n - 1; i >= 0; i--) {
for (int j = 0; j < cnt[i].size(); j++) {
ordered.push_back(cnt[i][j]);
}
}
vector<int> res;
vector<int> length(n);
visited.clear();
visited.resize(n);
visited[K] = true;
for (int i = 0; i < ordered.size(); i++) {
int len = 0;
int x = ordered[i];
while (!visited[x]) {
visited[x] = true;
x = parent[x];
len++;
}
length[ordered[i]] = len;
//cout << "length[" << ordered[i] << "]:" << len << endl;
} cnt.clear();
cnt.resize(n);
for (int i = 0; i < length.size(); i++) {
cnt[length[i]].push_back(i);
}
res.push_back(K);
for (int i = cnt.size() - 1; i > 0; i--) {
for (int j = 0; j < cnt[i].size(); j++) {
res.push_back(cnt[i][j]);
}
}
return res;
}

  

*[codility]Country network的更多相关文章

  1. 【Android】4.2 资源限定符和可视化选项

    分类:C#.Android.VS2015:创建日期:2016-02-06 在设计界面中,所有资源都可以被限定为使用哪个国家或地区的语言.例如,将字符串资源限定为默认使用中文等. 将字符串资源限定为默认 ...

  2. Bubble Cup X - Finals [Online Mirror] B. Neural Network country 矩阵快速幂加速转移

    B. Neural Network country time limit per test 2 seconds memory limit per test 256 megabytes Due to t ...

  3. zjuoj 3604 Tunnel Network

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3604 Tunnel Network Time Limit: 2 Secon ...

  4. 神经网络第三部分:网络Neural Networks, Part 3: The Network

    NEURAL NETWORKS, PART 3: THE NETWORK We have learned about individual neurons in the previous sectio ...

  5. NEURAL NETWORKS, PART 3: THE NETWORK

    NEURAL NETWORKS, PART 3: THE NETWORK We have learned about individual neurons in the previous sectio ...

  6. 递归神经网络(Recursive Neural Network, RNN)

    信息往往还存在着诸如树结构.图结构等更复杂的结构.这就需要用到递归神经网络 (Recursive Neural Network, RNN),巧合的是递归神经网络的缩写和循环神经网络一样,也是RNN,递 ...

  7. 转:Exploiting Windows 10 in a Local Network with WPAD/PAC and JScript

    转:https://googleprojectzero.blogspot.com/2017/12/apacolypse-now-exploiting-windows-10-in_18.html aPA ...

  8. 「Baltic2015」Network

    题目描述 原文 The government of Byteland has decided that it is time to connect their little country to th ...

  9. Recurrent Neural Network(2):BPTT and Long-term Dependencies

    在RNN(1)中,我们将带有Reccurent Connection的node依照时间维度展开成了如下的形式: 在每个时刻t=0,1,2,3,...,神经网络的输出都会产生error:E0,E1,E2 ...

随机推荐

  1. linux下别名alias的设置

    我有一个常用目录/volumes/mac/www’,每次都要输入这么长的路径,麻烦,所以有了以下配置 1.vi ~/.bash_profile 2.按住shift + i进入编辑状态 3.插入 ali ...

  2. NetworkInfo 手机中的网络类型

    04-27 21:56:54.442: E/NetworkInfo(26457): NetworkInfo: type: mobile[EDGE], state: DISCONNECTED/IDLE, ...

  3. Spark菜鸟学习营Day2 分布式系统需求分析

    Spark菜鸟学习营Day2 分布式系统需求分析 本分析主要针对从原有代码向Spark的迁移.要注意的是Spark和传统开发有着截然不同的思考思路,所以我们需要首先对原有代码进行需求分析,形成改造思路 ...

  4. Selenium-RC Python 2.7 环境配置

    1.下载并安装Python http://www.python.org/getit/,我使用的是2.7.3的python版本 2.下载并安装setuptools[这个工具是python的基础包工具] ...

  5. python 字典内置方法get应用

    python字典内置方法get应用,如果我们需要获取字典值的话,我们有两种方法,一个是通过dict['key'],另外一个就是dict.get()方法. 今天给大家分享的就是字典的get()方法. 这 ...

  6. Codeforces 559A 第六周 O题

    Description Gerald got a very curious hexagon for his birthday. The boy found out that all the angle ...

  7. Redis集群明细文档

    Redis目前版本是没有提供集群功能的,如果要实现多台Redis同时提供服务只能通过客户端自身去实现(Memchached也是客户端实现分布式).目前根据文档已经看到Redis正在开发集群功能,其中一 ...

  8. 设计模式之单例模式(Singleton Pattern)

    单例模式 单例模式(Singleton Pattern)在java中算是最常用的设计模式之一,主要用于控制控制类实例的数量,防止外部实例化或者修改.单例模式在某些场景下可以提高系统运行效率.实现中的主 ...

  9. android控件之EditText

    EditText继承关系:View-->TextView-->EditTextEditText的属性很多,这里介绍几个:android:hint="请输入数字!"//设 ...

  10. EntityFramework常用查询

    Sql语句.存储过程: 1.无参数查询var model = db.Database.SqlQuery<UserInfo>("select* from UserInfoes &q ...