Topological Ordering
Time limit4sMemory limit512 MB
Given a DAG of up to 20 vertices, count the topological orderings in which vertex j comes before vertex i, for every ordered pair (i, j).
- Level
Medium7 of 10
- Topics
- Dynamic programming, Bit manipulation, Topological sort
- Solved
- No attempts yet
Problem
The topological ordering of a directed acyclic graph is a permutation of its vertices such that, for each arc, its source comes before its target in the permutation.
You are given a directed acyclic graph. For each pair of vertices , count the number of topological orderings in which vertex comes before vertex .
Input
The first line contains a single integer , the number of test cases. Each test case follows.
The first line of each test case contains two integers and , the number of vertices and arcs (, ). Each of the next lines contains two integers and , denoting an arc from vertex to vertex ().
There are at most 100 test cases. In at most 5 of them, .
Output
For each test case, print lines of numbers each. The -th number in the -th line equals the number of topological orderings in which vertex comes before vertex . When , the number is 0.