*[codility]Country network
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的更多相关文章
- 【Android】4.2 资源限定符和可视化选项
分类:C#.Android.VS2015:创建日期:2016-02-06 在设计界面中,所有资源都可以被限定为使用哪个国家或地区的语言.例如,将字符串资源限定为默认使用中文等. 将字符串资源限定为默认 ...
- 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 ...
- zjuoj 3604 Tunnel Network
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3604 Tunnel Network Time Limit: 2 Secon ...
- 神经网络第三部分:网络Neural Networks, Part 3: The Network
NEURAL NETWORKS, PART 3: THE NETWORK We have learned about individual neurons in the previous sectio ...
- NEURAL NETWORKS, PART 3: THE NETWORK
NEURAL NETWORKS, PART 3: THE NETWORK We have learned about individual neurons in the previous sectio ...
- 递归神经网络(Recursive Neural Network, RNN)
信息往往还存在着诸如树结构.图结构等更复杂的结构.这就需要用到递归神经网络 (Recursive Neural Network, RNN),巧合的是递归神经网络的缩写和循环神经网络一样,也是RNN,递 ...
- 转: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 ...
- 「Baltic2015」Network
题目描述 原文 The government of Byteland has decided that it is time to connect their little country to th ...
- Recurrent Neural Network(2):BPTT and Long-term Dependencies
在RNN(1)中,我们将带有Reccurent Connection的node依照时间维度展开成了如下的形式: 在每个时刻t=0,1,2,3,...,神经网络的输出都会产生error:E0,E1,E2 ...
随机推荐
- C#简单实现发送手机短信
偶然想起,像编写一个从电脑向手机发送短信的程序,从网上查找到有三种方式:(1)使用webservice接口发送手机短信,这个可以使用sina提供的webservice进行发送,但是需要进行注册;(2) ...
- linux lsof命令的使用
lsof(list open files)是一个列出当前系统打开文件的工具.在UNIX环境下,任何事物都是以文件的形式存在的,通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件. 像传输协议(T ...
- linux服务器修改ftp默认21端口方法
1.登录服务器,打开vsftp.conf文件 # vim /etc/vsftpd/vsftpd.conf 2.在文件末尾增加listen_port=8021 #remote_charset=CP125 ...
- 【zendstudio】如何利用zendstudio新建 或导入php项目
一.利用ZendStudio创建 PHP Project 1. 打开ZendStudio, 选择:File à New à PHP Project, 如下图所示: 于是弹出如下界面: 在”Projec ...
- Linux系统下sendmail发送邮件失败的问题
问题是:安装完sendmail,启动服务后,发送邮件第一次发送成功,后面再次无论怎么发送都不行,换邮箱也不行.在确认我的邮件发送格式正确无误后,想到查看邮件发送日志: [root@backu ...
- Python开发【第一篇】Python基础之正则表达式补充
正则表达式 一简介:就其本质而言,正则表达式(或RE)是一种小型的.高度专业化的标称语言,(在Python中)它内嵌在Python中,并通过re模块实现.正则表达式模式被编译成一系列的字节码,然后由用 ...
- Ztack学习笔记(3)-系统启动分析
一 系统启动 //OSAL.cvoid osal_start_system( void ) { #if !defined ( ZBIT ) && !defined ( UBIT ) f ...
- 浅谈 WPF控件
首先我们必须知道在WPF中,控件通常被描述为和用户交互的元素,也就是能够接收焦点并响应键盘.鼠标输入的元素.我们可以把控件想象成一个容器,容器里装的东西就是它的内容.控件的内容可以是数据,也可以是控件 ...
- Posix 信号量
作用 信号量的值为0或正整数,就像红灯与绿灯,用于指示当前是否可以接受任务. 信号量对进程和线程都适用. gcc编译时需加-lpthread 基本函数 信号量的相关函数与标准文件函数非常相似,可以理解 ...
- [原]项目进阶 之 持续构建环境搭建(二)Nexus私服器
上一篇博文项目进阶 之 持续构建环境搭建(一)架构中,我们大致讲解了一下本系列所搭建环境的基本框架,这次开始我们进入真正的环境搭建实战.重点不在于搭建的环境是否成功和完善,而是在搭建过程中充分认识到每 ...