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. Find Minimum in Rotated Sorted Array II

    Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...

  2. PAT (Basic Level) Practise:1033. 旧键盘打字

    [题目链接] 旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现.现在给出应该输入的一段文字.以及坏掉的那些键,打出的结果文字会是怎样? 输入格式: 输入在2行中分别给出坏掉的那些键.以 ...

  3. tomcat直接访问

    解决了:http://blog.csdn.net/zhangyulin54321/article/details/8876320 <Context path="" docBa ...

  4. shamir叠像术 分类: 图像处理 2015-07-08 16:50 17人阅读 评论(1) 收藏

    K=imread('SHNU.bmp'); height=info.Height; width=info.Width; A=zeros(height,width); B=zeros(height,wi ...

  5. 批量kill相关所有进程

    首先,用ps查看进程,方法如下: $ ps -ef …… smx       1822     1  0 11:38 ?        00:00:49 gnome-terminal smx      ...

  6. spark Streaming的Receiver和Direct的优化对比

    Direct 1.简化并行读取:如果要读取多个partition,不需要创建多个输入DStream然后对它们进行union操作.Spark会创建跟Kafka partition一样多的RDD part ...

  7. 越狱Season 1- Episode 18: Bluff

    Season 1, Episode 18: Bluff -Michael: Scofield Scofield Michael Scofield Michael Scofield -Patoshik: ...

  8. Java中相等测试

    一:equals与==的区别 (1)基本数据类型 byte,short,char,int,long,float,double,boolean 此类数据类型的比较需要使用==,此时比较的是他们的值,若相 ...

  9. 为什么说Parcelable 比Serializable更高效

    本文转载自:http://blog.csdn.net/androidzhaoxiaogang/article/details/8172539 什么是序列化,实现序列化的目的是什么? 讨论这个问题之前, ...

  10. 亲测 logminer挖掘

    LogMiner两种使用类型,一种是使用源数据库的数据字典分析DML操作,别一种是摘取LogMiner数据字典到字典文件分析DDL操作.检查下suppplemental logging:SQL> ...