PFDEP - Project File Dependencies

project managers, such as the UNIX utility make, are used to maintain large software projects made up from many components. Users write a project file specifying which components (called tasks) depend on others and the project manager can automatically update the components in the correct order.


Write a program that reads a project file and outputs the order in which the tasks should be performed.


For simplicity we represent each task by an integer number from $1, 2, \ldots, N$ (where $N$ is the total number of tasks). The first line of input specifies the number $N$ of tasks and the number $M$ of rules, such that $N \le 100$, $M \le 100$.

The rest of the input consists of $M$ rules, one in each line, specifying dependencies using the following syntax:

$T_0$ $k$ $T_1$ $T_2$ $\ldots$ $T_k$

This rule means that task number $T_0$ depends on $k$ tasks $T_1, T_2, \ldots, T_k$ (we say that task $T_0$ is the target and $T_1, \ldots, T_k$ are dependents).

Note that tasks numbers are separated by single spaces and that rules end with a newline. Rules can appear in any order, but each task can appear as target only once.

Your program can assume that there are no circular dependencies in the rules, i.e. no task depends directly or indirectly on itself.


The output should be a single line with the permutation of the tasks $1 \ldots N$ to be performed, ordered by dependencies (i.e. no task should appear before others that it depends on).

To avoid ambiguity in the output, tasks that do not depend on each other should be ordered by their number (lower numbers first).


5 4
3 2 1 5
2 2 5 3
4 1 3
5 1 1

1 5 3 2 4

hide comments
rahulpadhy: 2016-08-25 20:10:09

Last edit: 2016-08-25 22:34:25
Ram: 2016-03-31 09:21:30

@aditya730, Thanks. That helped. Tried topo sort using dfs but in the end solved using indegree method.

coder_shishir: 2016-02-29 12:37:05

excellent topo..:)

aditya730: 2016-01-31 12:24:42

If you are unable to think of any possible logic,then you probably haven't learnt the indegree method for topological sorting.This can be solved by slightly modifying the indegree method.

Deepak : 2016-01-19 21:13:44

nice and easy.AC in one go.

manas0008: 2016-01-12 17:26:23

My first code in C++ !!....used set in topological sort.
ac in one go!!!
Nice problem!

Projjal Kundu: 2015-12-14 20:44:36

use set instead of queue in topological sort for lexicographic sorting

Amitayush Thakur: 2015-10-22 09:41:09

The lexicographical ordering is a bit difficult to achieve. Btw toposort is easy :P

Anubhav Gupta: 2015-06-02 00:34:33

topological sort

Priya: 2015-05-25 16:49:01

what is the test case by dewan
it doesnt show previous comments..please someone tell

Added by:overwise
Time limit:0.405s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:ACM ICPC -- SWERC 2001