zoj 1346 (求满足拓扑排序的序列个数)

Comparing Your Heroes

时间限制: 1 Sec  内存限制: 128 MB
提交: 9  解决: 6
[提交] [状态] [讨论版] [命题人:admin]

题目描述

Nowadays many students wouldn't attend classes in university, instead, they stay in dormitory playing Electronic games on computers. One of the most popular games among the boys is KOF (the King Of Fighters), a kind of action game presented by SNK corporation. This series of games becomes so successful that SNK has even created comics and story for it. Although in the story IORI and KYO are the definitely two strongest heroes, it would not affect players' true affection of other characters. Actually, every player has his own favorite heroes.

As a fanatical fan of the KOF game, you're going to help the other players to find out the ranking of their favorite heroes. Players would only provide information of comparisons between the heroes. This sometimes can lead to confusion: maybe not only one ranking satisfies the comparisons. So at first you'd like to find out the number of the rankings that satisfy a specific player's comparisons.

 

 

输入

The input consists of several test cases. Each test case begins with a positive integer N on a line, indicating the number of the comparisons. The following N lines are the comparisons between the heroes. Each one is a line containing 2 names of the heroes separated by a space, means the player prefer the first hero to the second. The name of the hero is a sequence of at most 10 upper case letters. No two comparisons would be same, and you can always assure one's favorite heroes would not exceed 16.

 

输出

For each test case, print out the number of the rankings that satisfy the comparisons on a single line. If no such ranking exists, just print out 0.

 

 

样例输入

复制样例数据

4
IORI KYO
MARY SHERMIE
KYO ANDY
SHERMIE ANDY
3
IORI KYO
KYO CLARK
CLARK IORI

样例输出

6
0

 

来源/分类

ZJU 2002 

 

题目大意:题目让求满足拓扑排序的序列的个数。

解题思路:不会写。。补题的时候才明白,排列总数等于除去当前入度为0的数之后,剩下的数的排列总数。

当只有1个数时满足要求的排列数为1.所以我们可以在回溯的时候统计答案。

具体过程:连边,统计每个点的入度,状态压缩后 dp数组初始化(只有一个点时答案为1)。

然后就是依次枚举当前入度为0的点,再把当前点去掉就行。

 

#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define LL long long
#define sca(x) scanf("%d",&x)
#define lowb(x) (x&(-x))
#define pb(x) push_back(x)
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define per(i,j,k) for(int i=j;i>=k;i--)
#define pri(x) printf("%d\n",x);
#define N 405
#define inf 0x3f3f3f3f
const LL mod=1e9+7;

mapmmp;
int g[20][20];
LL dp[1<<17];
int in[20];
int id;
LL dfs(LL now)
{
    if(dp[now]) return dp[now];
    dp[now]=0;
    for(int i=0;i>n)
    {
        id=0;
        string a,b;
        mmp.clear();
        memset(g,0,sizeof(g));
        memset(dp,0,sizeof(dp));
        memset(in,0,sizeof(in));
        rep(i,1,n)
        {
           cin>>a>>b;
           if(mmp.find(a)==mmp.end())
           {
               mmp[a]=id++;
           }
           if(mmp.find(b)==mmp.end())
           {
               mmp[b]=id++;
           }
           g[mmp[a]][mmp[b]]=1;
           in[mmp[b]]++;
        }
        for(int i=0;(1<

 

你可能感兴趣的:(dp,拓扑排序)