HackerRank "Favorite sequence"
Typical topological sorting problem .. why is it 'difficult'?
#include <iostream>
#include <string>
#include <vector>
#include <queue>
using namespace std; #define MAX_CNT 1000001
vector<long> outdeg(MAX_CNT);
vector<long> inc[MAX_CNT]; void add_edge(long a, long b)
{
outdeg[a]++;
inc[b].push_back(a);
} int main()
{
// Get input and compose graph
vector<long> ar(MAX_CNT), used(MAX_CNT);
long n; cin >> n;
for (int i = ; i <= n; i++)
{
long q; cin >> q;
for (int j = ; j <= q; j++)
{
cin >> ar[j];
used[ar[j]] = ;
}
// setup edge\degree info
for (int j = ; j <= q; j++)
{
add_edge(ar[j], ar[j - ]);
}
} // maintain a min-heap of all leaves
priority_queue<long, vector<long>,greater<long>> leaves;
for (int i = ; i <= MAX_CNT; i++)
{
if (outdeg[i] == && used[i] == )
leaves.push(i);
} // iteratively, we pop\push old\new leaves
vector<long> ans;
while (leaves.size())
{
long v = leaves.top();
leaves.pop();
ans.push_back(v);
for (int i = ; i<inc[v].size(); i++)
{
long id = inc[v][i];
outdeg[id]--;
if (!outdeg[id]) leaves.push(id);
}
} for (int i = ; i<ans.size(); i++)
{
if (i)cout << " ";
cout << ans[i];
}
cout << endl;
return ;
}
HackerRank "Favorite sequence"的更多相关文章
- oracle SEQUENCE 创建, 修改,删除
oracle创建序列化: CREATE SEQUENCE seq_itv_collection INCREMENT BY 1 -- 每次加几个 STA ...
- Oracle数据库自动备份SQL文本:Procedure存储过程,View视图,Function函数,Trigger触发器,Sequence序列号等
功能:备份存储过程,视图,函数触发器,Sequence序列号等准备工作:--1.创建文件夹 :'E:/OracleBackUp/ProcBack';--文本存放的路径--2.执行:create or ...
- DG gap sequence修复一例
环境:Oracle 11.2.0.4 DG 故障现象: 客户在备库告警日志中发现GAP sequence提示信息: Mon Nov 21 09:53:29 2016 Media Recovery Wa ...
- Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Sequence Reconstruction 序列重建
Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [LeetCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [LeetCode] Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
随机推荐
- 初探Openstack Neutron DVR
目前在Juno版本的trunk中已经合入了DVR相关的代码,我的理解是在Juno版本中DVR是一个experimental feature.最好需要稳定一个版本以后再上生产环境.之前写过一篇博文是DV ...
- R处理xml文件
最近处理数据的时候需要处理一些xml文件,但是xml文件的格式之前并不是很熟悉.幸好R有一些函数可以帮助快速处理xml文件.其中的xmlToList函数尤其方便,可以使你从陌生的XML迅速回到熟悉的R ...
- 推荐!Sublime Text 最佳插件列表
本文由 伯乐在线 - 艾凌风 翻译,黄利民 校稿.英文出处:ipestov.com.欢迎加入翻译组. 本文收录了作者辛苦收集的Sublime Text最佳插件,很全. 最佳的Sublime Text ...
- 2014年IT互联网行业薪酬待遇
以下均为应届毕业生的起薪待遇: 一.民企 1. 百度 13k*14.6,special 14~17k*14.6 开发类 13K*14.6 (2014) 测试类.前端类 12K*14.6 (2014) ...
- php防注入留言板(simple)
新手学php,试手案例便是留言板.以前未连接数据库时,我是直接将用户输入的留言写入到一个txt,然后再从txt读取显示(~.~别鄙视). 最近学习了php访问MySQL数据库的一些知识,重写了一下留言 ...
- Jquery attr判断服务器单选按钮失败
在项目中用 jquey的attr方法获取服务器控件的单选按钮checked属性会不成功, 单选选中改变,用attr获取不到最新的值,但是用 $("#rdbPartySend").i ...
- [转载]新手入门:Spring的一些学习方法及意见
原文地址:新手入门:Spring的一些学习方法及意见作者:飞扬飞扬xyz Spring简介: 是一个开源框架,是为了解决企业应用程序开发复杂性而创建的.框架的主要优势之一就是其分层架构,分层架构允许您 ...
- 268. Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- Codeforces Round #374 (Div. 2) A B C D 水 模拟 dp+dfs 优先队列
A. One-dimensional Japanese Crossword time limit per test 1 second memory limit per test 256 megabyt ...
- 小三角图标如何用CSS写
上三角▲ 1 width: 0; 2 height: 0; 3 line-height: 0; 4 font-size: 0; 5 border-width: 10px; 6 border-s ...