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命令详解之chkconfig命令使用方法

    介绍一个linux常用命令,chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息.谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接. 使用语法:c ...

  2. Firebird数据库相关备忘录

    Firebird数据库中有一些很特别的东西,很好用,但由于平时用的不多,记在这里,以备以后用到时查询. 1.以ADO 的OLE ODBC驱动方式访问 Firebird,可以使用如下连接串: FBCon ...

  3. WPF 气泡尖角在左边、下面、右边、上面

    由于项目需要,在弄一个气泡提示框,根据网上资料,使用Path可以将气泡画出来,下面是我画出来的. 1.气泡尖角在左边的: <Path Stroke="Black" Strok ...

  4. Linux环境下GIT初次使用

    Git是一个功能强大的分布式版本控制系统,最初用来作Linux内核代码管理的. 第一次接触到github是关于一个报道:在2013年1月15日晚间,全球最大的社交编程及代码托管网站GitHub突然疑似 ...

  5. Bash 快捷键

     生活在 Bash shell 中,熟记以下快捷键,将极大的提高你的命令行操作效率. 编辑命令 Ctrl + a :移到命令行首 Ctrl + e :移到命令行尾 Ctrl + f :按字符前移(右向 ...

  6. kendo ui template的用法

    kendo ui template的用法: Kendo UI 框架提供了一个易用,高性能的JavaScript模板引擎.通过模板可以创建一个HTML片段然后可以和JavaScript数据合并成最终的H ...

  7. return的用法

    1.一般的就是用在有返回值的方法中,用来返回方法指定类型的值,同时结束方法执行: 2.可以用在返回值为void的方法中,用来终止方法运行:

  8. C++中的多重继承与虚继承的问题

    1.C++支持多重继承,但是一般情况下,建议使用单一继承. 类D继承自B类和C类,而B类和C类都继承自类A,因此出现下图所示情况: A          A \          / B     C ...

  9. Version of SQLite used in Android?

    sing the emulators (adb shell sqlite3 --version): SQLite 3.7.11: 19-4.4-KitKat 18-4.3-Jelly Bean 17- ...

  10. 【软件工程-Teamwork 2】必应词典软件手机版测试报告

    测试人员:聂健(N).居玉皓(J).吴渊渊(Wy).汪仁贵(Wr).吕佳辉(L).杜冰磊(D) 测试软件:必应词典软件手机版 版本:2.2.0版本(Android) 引言: 我们的测评报告的主体主要分 ...