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 ...
随机推荐
- notebook
1. 2.
- 【题解】【链表】【Leetcode】Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- 1130-host ... is not allowed to connect to this MySql server 开放mysql远程连接 不使用localhost
报错:1130-host ... is not allowed to connect to this MySql server 解决方法: 1. 改表法. 可能是你的帐号不允许从远程登陆,只能在loc ...
- Codeforces Round #376 (Div. 2) A B C 水 模拟 并查集
A. Night at the Museum time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Codeforces Round #373 (Div. 2) A B C 水 贪心 模拟(四舍五入进位)
A. Vitya in the Countryside time limit per test 1 second memory limit per test 256 megabytes input s ...
- CString用法总结
概述:CString是MFC中提供的用于处理字符串的类,是一种很有用的数据类型. 它很大程度上简化了MFC中的许多操作,使得MFC在做字符串操作时方便了很多. 不管怎样,使用CString有很多的特殊 ...
- zookeeper的 目录加密
import org.apache.zookeeper.CreateMode;import org.apache.zookeeper.WatchedEvent;import org.apache.zo ...
- 自我提升mysql
1.某字段更新 自增 1 update table set a=a+1 2.修改某一字段的数据类型 alter table "tablename" modify "co ...
- 如何使用投影看着备注分享自己的PPT
1. 设置多屏幕 一般你的笔记本就是1, 投影是2 2. 设置幻灯片的放映方式 设置幻灯片显示于另外一个监视器 并勾选显示演示者视图 3. 点击放映 就会出现 左上角是ppt本身, 右上角是 ...
- PHP读某一个目录下所有文件和文件夹
废话少说了 直接上代码 <?php function read_dir($dir) { if (!is_dir($dir)) { echo 'not a dir '; return; } if ...