关闭

HDU 6064 RXD and numbers(BEST theorem)

标签: 图论算法ACM
470人阅读 评论(0) 收藏 举报
分类:

RXD and numbers

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 40 Accepted Submission(s): 16

Problem Description
RXD has a sequence A1,A2,A3,…An, which possesses the following properties:
1≤Ai≤m
A1=An=1
for all 1≤x≤m, there is at least one position p where Ap=x.
for all x,y, the number of i(1 ≤ i < n) which satisfies Ai=x and Ai+1=y is Dx,y.
One day, naughty boy DXR clear the sequence.
RXD wants to know, how many valid sequences are there.
Output the answer module 998244353.
0≤Di,j<500,1≤m≤400
n≥2

Input
There are several test cases, please keep reading until EOF.
There are about 10 test cases, but only 1 of them satisfies m>50
For each test case, the first line consists of 1 integer m, which means the range of the numbers in sequence.
For the next m lines, in the i-th line, it consists of m integers, the j-th integer means Di,j.
We can easily conclude that n=1+∑mi=1∑mj=1Di,j.

Output
For each test case, output “Case #x: y”, which means the test case number and the answer.

Sample Input
2
1 2
2 1
4
1 0 0 2
0 3 0 1
2 1 0 0
0 0 3 1
4
0 1 0 0
1 0 0 0
0 0 0 1
0 0 1 0

Sample Output
Case #1: 6
Case #2: 18
Case #3: 0

Source
2017 Multi-University Training Contest - Team 3

Recommend
liuyiding

题目大意:

 给出一个m个节点的有向图中,每种起点终点边的条数,求有多少条从1号节点起始的欧拉回路。

解题思路:

 由于是要求有向图的欧拉回路数,很自然想到BEST theorem解决。
 BEST theorem的介绍引用wiki:
BEST theorem
 这里要用到matrix tree的有向图版本,表达能力有限(:з」∠),同样引用wiki:
matrix tree for directed multigraphs
 首先利用BEST theorm求得的欧拉回路数是不定起点的,这里固定起点为1,那么就需要把方案数乘上deg(1),表示同一条欧拉回路,在这里起点不同算作不同的欧拉回路。由于BEST theorm会把重边看作不同的边,而本题会看作相同的边,所以还需要对答案除以mi=1mj=1(Di,j!)
 所以最终答案就是tw(G)(deg(1)!)mi=2(deg(i)1)!/mi=1mj=1(Di,j)!
 总复杂度为O(m3)
 

AC代码

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <string>
#include <map>
#include <set>
#include <list>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define fi first
#define se second
#define mem(a,b) memset((a),(b),sizeof(a))

const LL MOD=998244353;
const int MAXV=400+1;
int V;
LL D[MAXV][MAXV];//从i,到j的边的数目
LL in[MAXV],out[MAXV];//每个结点的入度,出度

struct Matrix
{
    LL a[MAXV][MAXV];
    Matrix()
    {
        memset(a,0,sizeof(a));
    }
    LL det(int n)//求前n行n列的行列式的值
    {
        for(int i=0;i<n;++i)
            for(int j=0;j<n;++j)
                a[i][j]=(a[i][j]%MOD+MOD)%MOD;
        LL ret=1;
        for(int i=0;i<n;i++)
        {
            for(int j=i+1;j<n;j++)
                while(a[j][i])
                {
                    LL t=a[i][i]/a[j][i];
                    for(int k=i;k<n;++k)
                        a[i][k]=((a[i][k]-a[j][k]*t)%MOD+MOD)%MOD;
                    for(int k=i;k<n;++k)
                        swap(a[i][k],a[j][k]);
                    ret=-ret;
                }
            if(!a[i][i])
                return 0;
            ret=ret*a[i][i]%MOD;
        }
        ret=(ret%MOD+MOD)%MOD;
        return ret;
    }
};

LL get_fac(LL x)//计算阶乘
{
    LL res=1;
    for(LL i=2;i<=x;++i)
        res=(res*i)%MOD;
    return res;
}

LL exgcd(LL a, LL b, LL &x, LL &y)
{
    LL d=a;
    if(b)
    {
        d=exgcd(b, a%b, y, x);
        y-=(a/b)*x;
    }
    else
    {
        x=1;
        y=0;
    }
    return d;
}

LL inv(LL a)//计算逆元
{
    LL x, y;
    exgcd(a, MOD, x, y);
    return (MOD+x%MOD)%MOD;
}

void init()//初始化
{
    for(int i=0;i<=V;++i)
        in[i]=out[i]=0;
}

int main()
{
    int cas=1;
    while(~scanf("%d",&V))
    {
        init();
        Matrix mat;
        for(int i=0;i<V;++i)
            for(int j=0;j<V;++j)
            {
                scanf("%lld", &D[i][j]);
                mat.a[i][j]-=D[i][j];
                mat.a[j][j]+=D[i][j];
                in[j]+=D[i][j];
                out[i]+=D[i][j];
            }
        //如果存在点入度不等于出度,则不存在欧拉回路直接输出0
        bool ok=true;
        for(int i=0;i<V;++i)
            if(in[i]!=out[i])
            {
                ok=false;
                break;
            }
        if(!ok)
        {
            printf("Case #%d: 0\n", cas++);
            continue;
        }
        //把根节点移到最后,方便去掉它求行列式
        for(int i=0;i<V;++i)
            swap(mat.a[0][i], mat.a[V-1][i]);
        for(int i=0;i<V;++i)
            swap(mat.a[i][0], mat.a[i][V-1]);
        LL ans=mat.det(V-1);
        for(int i=0;i<V;++i)
            ans=(ans*get_fac(in[i]-(i!=0)))%MOD;
        for(int i=0;i<V;++i)
            for(int j=0;j<V;++j)
                ans=(ans*inv(get_fac(D[i][j])))%MOD;
        printf("Case #%d: %lld\n", cas++, ans);
    }

    return 0;
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:46089次
    • 积分:2179
    • 等级:
    • 排名:第19503名
    • 原创:176篇
    • 转载:2篇
    • 译文:0篇
    • 评论:32条
    文章分类
    最新评论