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. 设置viewport进行缩放

    <meta name="viewport" content="width=320,maximum-scale=1.3,user-scalable=no"& ...

  2. dede文章摘要字数的设置方法

    本文转自:http://blog.csdn.net/yxwmzouzou/article/details/17491991 在织梦系统中(针对5.7版本),文章摘要(可以通过以下四种相关标签调用)被设 ...

  3. The u32 classifier

    The u32 classifier The U32 filter is the most advanced filter available in the current implementatio ...

  4. IE6无法加载CSS

    问题:写了个页面,IE7和IE8下正常,但发现IE6下竟然没有加载css样式. 找原因,发现是因为CSS文件的编码不正确,页面是采用utf-8编码的,但样式表是复制过来的,编码是ANSI,这种情况下在 ...

  5. 在双系统( Win7 + Ubuntu )环境下正常删除Ubuntu

    前言 首先切记一点:如果你和我一样使用ubuntu的目的是学习操作系统和Unix网络编程等,那么学习环境搭建好了以后切不要随意更新软件.否则系统崩溃,你又得重新配置各种环境,有多麻烦你懂的. 万一你像 ...

  6. 纯代码写UI的时候,如何指定style?

    有的时候,需要使用纯代码实现Android UI,那么这个时候如何指定某个UI组件的样式呢? 一般来说,UI组件都有一些set方法可供使用,以调整一些UI属性,从而达到调整样式的目的. 但是,情况并非 ...

  7. ZPPR001-(展bom)

    ************************************************************************ Title : ZPPR010 ** Applicat ...

  8. android 软键盘不遮挡页面上的控件

    只需要加android:windowSoftInputMode="adjustPan"就可以如: <activity android:name=".Enhance_ ...

  9. css link和@import区别

    1.link语法结构 <link href="CSSurl路径" rel="stylesheet" type="text/css" / ...

  10. spark yarn-cluster 和 yarn-client提交的配置

    1. spark conf 目录下需要配置进去hadoop home 2.需要spark 提交的配置文件 加上‘-- master yarn-cluster/yarn-client’设置提交的模式