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. 一款jquery编写图文下拉二级导航菜单特效

    一款jquery编写图文下拉二级导航菜单特效,效果非常简洁大气,很不错的一款jquery导航菜单特效. 这款jquery特效适用于很多的个人和门户网站. 适用浏览器:IE8.360.FireFox.C ...

  2. php文本操作方法集合比较第2页

    fgets和fputs.fread和fwrite.fscanf和fprintf 格式化读写函数fscanf和fprintf fscanf函数,fprintf函数与前面使用的scanf和printf 函 ...

  3. iis 下的 selfssl

    当然,如果你想省掉所有这些麻烦也行,最简单的在IIS启动SSL的方法只要3步: 1. 下载 IIS 6.0 Resource Kit Tools: http://www.microsoft.com/d ...

  4. 优化C++程序编译效率的一些方法

    优化是一件非常重要的事情.作为一个程序设计者,你肯定希望自己的程序既小又快.DOS时代的许多书中都提到,“某某编译器能够生成非常紧凑的代码”,换言之,编译器会为你把代码尽可能地缩减,如果你能够正确地使 ...

  5. WPF-数据绑定:日期时间格式

    WPF-数据绑定:日期时间格式绑定后自定义格式的例子. 我刚才遇到的问题是绑定完之后,星期始终显示为英文.需要一个属性ConverterCulture制定区域. 如下: {Binding dateti ...

  6. C#调用Python 脚本语言

    1. 安装IronPython http://pan.baidu.com/s/1qW4jNJ2  下载IronPython 2.7 安装下载下来的安装包 2. 创建项目 创建一个C#的Windows窗 ...

  7. phpcms v9 企业黄页系统发布没有表单出现的解决方案

    第一种解决方案: 第一步:把yp_UTF8压缩文件解压得到:api.caches.phpcms.statics四个文件夹. 第二步:把这四个文件夹分别覆盖已安装好的phpcms系统根目录下的文件夹.这 ...

  8. Web UI Design Patterns 2014

    http://www.uxpin.com/web-design-patterns.html?utm_source=Interaction%20Design%20Best%20Practices%20V ...

  9. linux-CentOS6.4下安装oracle11g详解

    参考地址:http://dengqsintyt.iteye.com/blog/1991930

  10. Careercup - Facebook面试题 - 6139456847347712

    2014-05-01 01:50 题目链接 原题: Microsoft Excel numbers cells ... and after that AA, AB.... AAA, AAB...ZZZ ...