Find the number of topological sorts, example TopCoder SRM 654 DIV2 500

Source: Internet
Author: User
Tags sorts

A more interesting topic to be encountered during the week race:

Problem Statement

There is N rooms in Maki's new house. The rooms is numbered from 0 to N-1. Some pairs of rooms is connected by bidirectional passages. The passages has the topology of a tree. That's, there is exactly N-1 of them and it's possible to go from any of the--and other--from the following some sequence of passages.

You are given-vector <int>s a and B that describe the passages. For each valid I, there are a passage that connects the rooms a[i] and b[i]. You is also given an int s. The house had exactly one entrance from the outside, and the entrance leads to the

Niko is helping Maki move to the new house. Maki has exactly N pieces of furniture. The pieces is numbered from 0 to N-1. Niko'll carry them into the house in this order. Each piece of furniture must is placed into a different. Maki does not care which piece goes where, each of the N! Permutations is allowed.

However, not all of those N! Permutations is actually possible. This was because the furniture is large. As soon as a contains a piece of furniture, it is impossible to move other pieces through the this. Thus, Niko must place the furniture carefully. Formally, she can place a new piece of furniture into the the "If all rooms" on the (unique) path between s and X, including s and X, are still empty. Niko is smart and she'll always place the furniture in such a to that she never gets stuck. Thus, at the end each of Maki ' s rooms would contain exactly one piece of furniture.

Calculate and return the number of ways how the furniture can is arranged in Maki's house at the end.

Definition
Class: Oneentrance
Method: Count
Parameters: Vector <int>, vector <int>, int
Returns: Int
Method Signature: int count (vector <int> A, vector <int> B, int s)
(Be sure your method was public)
Limits
Time limit (s): 2.000
Memory Limit (MB): 256
Stack Limit (MB): 256
Constraints
- N'll be between 1 and 9, inclusive.
- a and b would contain exactly N-1 elements each.
- Each element of a and b would be between 0 and N-1, inclusive.
- The graph described by a and b would be a tree.
- s 'll be between 0 and N-1, inclusive.
Examples
0)
{0, 1, 2}
{1, 2, 3}
0
Returns:1
There is only one Solution:niko must fill with the rooms in the order {3,2,1,0}. Thus, piece number 0 would end in the 3, piece number 1 in the 2, and so on.
1)
{0, 1, 2}
{1, 2, 3}
2
Returns:3
In this case Niko can choose one of the three orders: {3,0,1,2}, {0,3,1,2}, or {0,1,3,2}. Note that the-the-the-entrance (in this case, the "2") always gets the last piece of furniture.
2)
{0, 0, 0, 0}
{1, 2, 3, 4}
0
Returns:24
3)
{7, 4, 1, 0, 1, 1, 6, 0}
{6, 6, 2, 5, 0, 3, 8, 4}
4
returns:896
4)
{}
{}
0
Returns:1
Maki ' s new house have only one.

This problem statement are the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly p Rohibited. (c) 2003, TopCoder, Inc. All rights reserved.

This topic is also equivalent to:

Suppose a tree contains n nodes, and now there are N numbers 1-n, the n nodes of the tree are numbered with this n number, requiring that the parent node must be greater than the number of the child node, and how many numbering methods there are.

This problem can be abstracted as a question: the number of topological sequences of all nodes in a tree.

The solution can be done using DFS:

The other int func () represents the number of topological orders for the current tree, and Func () can calculate this: Find all the nodes in the current tree that have a degree of 0, assuming there are k nodes, and for each such node, remove the edge from the tree from that node, and then figure out the tree after the edge is removed. The corresponding func (). The sum of the results of the child Func () is the result of the parent func ().

When all nodes are traversed obsolete, Func () returns 1

The graph representation in the title is represented by two vectors, so a one-way adjacency matrix is constructed based on the two vectors first conn[][]

#include <iostream>#include<vector>#include<string.h>using namespacestd;classoneentrance{ Public:    intCount (Vector <int> A, vector <int> B,ints) {N= A.size () +1; if(N <=1)returnN; MEMSET (Conn,0,sizeof(conn)); memset (Visit,0,sizeof(visit)); Dfsconn (A, B, S,-1);//Use A, B to create adjacency matrix (mono-directed)        returnDfscount (conn);//based on matrix, get the total amount of topological sequence.    }Private:    BOOLconn[9][9]; BOOLvisit[9]; intN =0; voidDfsconn (vector<int> &a, vector<int> &b,intCurintpre) {         for(inti =0; I < a.size (); ++i) {            if(A[i] = = cur && b[i]! =pre) {Conn[a[i]][b[i]]=true;            Dfsconn (A, B, b[i], cur); }            if(B[i] = = cur && a[i]! =pre) {Conn[b[i]][a[i]]=true;            Dfsconn (A, B, a[i], cur); }        }    }    intDfscount (BOOLconn[][9]){        intI, J, zdgrcnt, cnt =0;  for(i =0; i < N; ++i) cnt + = (!visit[i]?1:0); if(CNT = =0)return 1; CNT=0;  for(i =0; i < N; ++i) {            if(Visit[i])Continue; Zdgrcnt=0;  for(j =0; J < N; ++j) {zdgrcnt+ = (Conn[j][i]?1:0); }            if(zdgrcnt = =0){//found one node whose in-order degree is zeroVisit[i] =true; Vector<BOOL> tmp (N,false);  for(j =0; J < N; ++j) {                    if(Conn[i][j]) {Tmp[j]=true; CONN[I][J]=false;//remove its out-order edge.}} CNT+=Dfscount (conn);  for(j =0; J < N; ++j) {                    if(Tmp[j]) conn[i][j] =true; } Visit[i]=false; }        }        returnCNT; }};

DFS contains a lot of repetitive calculations, after all, recursion.

The solution of the dynamic programming can be referred to the answer to the question of HDU 4661.

Portal: http://www.cnblogs.com/GBRgbr/p/3312866.html

Find the number of topological sorts, example TopCoder SRM 654 DIV2 500

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.