求拓扑排序的数量,例题 topcoder srm 654 div2 500

周赛时遇到的一道比较有意思的题目:

Problem Statement

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

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

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

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

Calculate and return the number of ways how the furniture can be 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 is public)

Limits

    
Time limit (s): 2.000
Memory limit (MB): 256
Stack limit (MB): 256

Constraints

- N will be between 1 and 9, inclusive.
- a and b will contain exactly N-1 elements each.
- Each element of a and b will be between 0 and N-1, inclusive.
- The graph described by a and b will be a tree.
- s will 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 the rooms in the order {3,2,1,0}. Thus, piece number 0 will end in room 3, piece number 1 in room 2, and so on.
1)  
    
{0, 1, 2}
{1, 2, 3}
2
Returns: 3
In this case Niko can choose one of three orders: {3,0,1,2}, {0,3,1,2}, or {0,1,3,2}. Note that the room with the entrance (in this case, room 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 has only one room.

This problem statement is 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 prohibited. (c)2003, TopCoder, Inc. All rights reserved.

这道题目也等同于:

假设一个Tree含有N个结点,现在有N个数字 1-N,将这个Tree的N个结点用这N个数字编号,要求父结点一定要大于子结点的编号,一共有多少种编号方式。

这种题目都可以抽象为一个问题:求一个Tree 所有结点的拓扑序列的个数。

求解方式可以用DFS来做:

另int Func() 表示当前Tree 的拓扑排序数量,Func() 可以这么计算:找到当前Tree中所有入度为0的结点,假设有K个这样的结点,对于每个这样的结点,从Tree去掉从该结点出发的边,然后算出去掉边后的Tree 对应的Func()。子Func()的结果的总和,就是 父Func()的结果。

当所有结点都遍历过时,Func() 返回 1

题目中的图表示是用两个vector 表示,所以要先根据这两个vector 构建出单向邻接矩阵 conn[][]

#include<iostream>
#include<vector>
#include<string.h>
using namespace std;

class OneEntrance{
public:
    int count(vector <int> a, vector <int> b, int s){
        N = a.size() + 1;
        if(N <= 1) return N;
        memset(conn, 0, sizeof(conn));
        memset(visit, 0, sizeof(visit));
        dfsconn(a, b, s, -1);    //use a, b to create adjacency matrix(mono-directed)
        return dfscount(conn);    //based on matrix, get the total amount of topological sequence.
    }
private:
    bool conn[9][9];
    bool visit[9];
    int N = 0;
    void dfsconn(vector<int> &a, vector<int> &b, int cur, int pre){
        for(int i = 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);
            }
        }
    }
    int dfscount(bool conn[][9]){
        int i, 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 zero
                visit[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;
            }
        }
        return cnt;
    }
};

dfs 中包含了很多重复计算,毕竟是递归。

动态规划的解法可以参考对 HDU 4661 这一题的解答

传送门: http://www.cnblogs.com/GBRgbr/p/3312866.html

时间: 2024-11-09 20:39:05

求拓扑排序的数量,例题 topcoder srm 654 div2 500的相关文章

Topcoder SRM 619 DIv2 500 --又是耻辱的一题

这题明明是一个简单的类似约瑟夫环的问题,但是由于细节问题迟迟不能得到正确结果,结果比赛完几分钟才改对..耻辱. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #define ll long long using namespace std; #define NN 370000 class Choo

Topcoder SRM 654 Div2 1000

Problem 给一个长度为N(N∈[1,2000])的数列An(An∈[?100,100]),设ans=A1?A2?...?An,下面进行M(M∈[1,2000])次操作,每次将A的p[i]的值修改为v[i],即A[p[i]]=v[i],每次只允许加入最多2个括号进入ans等式,问ans的最大值可以是多少? Solution dp.. 设dp[i][j]表示从 1 到 i-1 已经有 j 个括弧时候的最大值,j=0时表示没有括弧,j=1时表示(这样子,j=2时表示(( 这个样子,j=3时表示(

Topcoder SRM 648 Div2 1000

Problem 给一个长度为N的字符串S,S只含有'A'.'B'.'C'三种元素.给定一个K,要求返回字符串S,使得S中恰好有K对pair(i,j)满足 0=<i<j<N,且 S[i]<S[j].若不存在,则返回空串. Limits Time Limit(ms): 2000 Memory Limit(MB): 256 N: [3, 30] K: [0, N*(N-1)/2 ] Solution 设S中含有n1个'A',n2个'B',n3个'C',设num=n1*n2+n1*n3+n

topcoder SRM 610 DIV2 TheMatrix

题目的意思是给一个01的字符串数组,让你去求解满足棋盘条件的最大棋盘 棋盘的条件是: 相邻元素的值不能相同 此题有点像求全1的最大子矩阵,当时求全1的最大子矩阵是用直方图求解的 本题可以利用直方图求解 首先找到子矩阵的两个顶点坐标(x0,y0),(x1,y1) 我们能遍历开始和结束列,y0=i, y1=j, 我们可以在y0和y1之间寻找满足条件的的最大棋盘,即在y0和y1之间找高度最大的满足条件的子矩阵 然后遍历i,j取最大的即可 对于满足条件的子行,有两种情况,一种是010101...,另一种

topcoder SRM 624 DIV2 CostOfDancing

排个序,求前k个元素和即可 int minimum(int K, vector <int> danceCost) { sort(danceCost.begin(),danceCost.end()); return accumulate(danceCost.begin(),danceCost.begin()+K,0); } topcoder SRM 624 DIV2 CostOfDancing

Topcoder SRM 626 DIV2 SumOfPower

本题就是求所有连续子数列的和 开始拿到题目还以为求的时数列子集的和,认真看到题目才知道是连续子数列 循环遍历即可 int findSum(vector <int> array) { int sum = 0; for(int i = 0 ; i < array.size(); ++ i ){ for(int j = i; j < array.size(); ++ j){ for(int k = i ; k <= j ; ++ k){ sum +=array[k]; } } }

topcoder SRM 618 DIV2 WritingWords

只需要对word遍历一遍即可 int write(string word) { int cnt = 0; for(int i = 0 ; i < word.length(); ++ i){ cnt+=word[i]-'A'+1; } return cnt; } topcoder SRM 618 DIV2 WritingWords,布布扣,bubuko.com

topcoder SRM 618 DIV2 MovingRooksDiv2

一开始Y1,Y2两个参数看不懂,再看一遍题目后才知道,vector<int>索引代表是行数,值代表的是列 此题数据量不大,直接深度搜索即可 注意这里深度搜索的访问标识不是以前的索引和元素,而是一个交换元素后的整个状态vector<int>,这样可以避免重复元素的搜索 set<vector<int> > visit; bool flag; void dfs(vector<int>& src, vector<int>& d

topcoder SRM 618 DIV2 LongWordsDiv2

此题给出的条件是: (1)word的每个字母都是大写字母(此条件可以忽略,题目给的输入都是大写字母) (2) 相等字符不能连续,即不能出现AABC的连续相同的情况 (3)word中不存在字母组成xyxy的形式,即不存在第一个字符和第3个字符相等同时第2个字符和第4个字符相等的情况 对于第(2)种情况,只需要考虑word[i]!=word[i-1]即可 对于第(3)种情况,用一个4重循环遍历每种可能的情况,然后第一个字符和第3个字符相等同时第2个字符和第4个字符相等,则输出“DisLikes”即可