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"的更多相关文章

  1. oracle SEQUENCE 创建, 修改,删除

    oracle创建序列化: CREATE SEQUENCE seq_itv_collection            INCREMENT BY 1  -- 每次加几个              STA ...

  2. Oracle数据库自动备份SQL文本:Procedure存储过程,View视图,Function函数,Trigger触发器,Sequence序列号等

    功能:备份存储过程,视图,函数触发器,Sequence序列号等准备工作:--1.创建文件夹 :'E:/OracleBackUp/ProcBack';--文本存放的路径--2.执行:create or ...

  3. DG gap sequence修复一例

    环境:Oracle 11.2.0.4 DG 故障现象: 客户在备库告警日志中发现GAP sequence提示信息: Mon Nov 21 09:53:29 2016 Media Recovery Wa ...

  4. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  5. [LeetCode] Sequence Reconstruction 序列重建

    Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...

  6. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  7. [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 ...

  8. [LeetCode] Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  9. [LeetCode] Permutation Sequence 序列排序

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

随机推荐

  1. selenium执行js报错

    selenium执行js报错 Traceback (most recent call last):    dr.execute_script(js)  File "C:\Python27\l ...

  2. 堆(Heap)和二叉堆(Binary heap)

    堆(Heap) The operations commonly performed with a heap are: create-heap: create an empty heap heapify ...

  3. 一张图让你学会LVM

    导读 随着科技的进步,人们不知不觉的就进入了大数据的时代,数据的不断增加我们发现我们的磁盘越来越不够用了,接下来就是令人头疼的事情--加硬盘,数据的备份与还原.LVM就是Linux下专门针对我们数据的 ...

  4. ajax 轮循

    使用 AJAX 进行异步加载轮询操作.简单代码如下: <script> // 执行ajax轮循操作 function polling(){ var xmlhttp; // 判断浏览器--创 ...

  5. zboot/piggyback.c

    /* *    linux/zBoot/piggyback.c * *    (C) 1993 Hannu Savolainen */ /* *    This program reads the c ...

  6. Jquery_笔记

    1.请确保在 <body> 元素的onload事件中没有注册函数,否则不会触发+$(document).ready()事件.

  7. leetcode 155. Min Stack --------- java

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  8. JS中的call()方法的理解

    fn.call(obj,arg1,arg2);这是call()方法的使用形式,apply()是差不多的.作用是用obj对象来替换fn中的this 举个栗子: function A(){ this.co ...

  9. checkbox 点击全选

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. URL 正则表达式

    (http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])? From h ...