题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1805
转载声明:http://hzwer.com/6911.html
转载声明:
http://taosama.github.io/2016/09/22/CSU 1805 Three Capitals(BEST定理、MatrixTree定理)/
题意:给你一个只有A,B,G的三个城市的图,而后A->B 有a条路径, A->G有b条路径, B->G有c条路径,问从A出发,走回A有多少可能的情况.
个人感想:
对于这两个算法的文章确实少之又少,这是我结合了两个博客上我看得明白清楚得条件来总结一下我做的这道题目.
基本理论的就不讲了,因为我转得大神博客上都讲得很清楚(虽然我还是半脸懵逼的),这里有点注意的是,首先对于公式上的生成树计算是有向图,所以计算矩阵树是和无向图的矩阵树有稍稍的不同(这是我个人感觉),其次**,这个公式只适用于有向图**,给的图中只有无向图,而无向图的转换有向图就有一点讲究
这是我看得懂的前题下的转换过程:
首先对于一个合法的欧拉回路 必定是每个点的出度都得是偶数(一进一出)
这时候
deg(A)=a+b=4;
deg(G)=b+c=2;
deg(B)=a+c=4;
所以该图合法,
然后我要枚举出度的边
我们从a来构造这个有向图的欧拉回路,
如上图,既然a有一条出度 atob=1,那么相应剩下的btoa=a-1=2.
因为是欧拉回路,所以我们在所有a出边中就得有 atoc=deg(A)/2-atob=1的边。
然后对于btoc因为要有且必须用 deg(B)/2条边为出度, 所以 btoc的出度边剩下为btoc=deg(B)/2-btoa;
同样地再构造atoc,ctob的边即可…
反正是必须得对于每个点的**(出度==入度边)这样构造的有向图才有有向图欧拉回路。
这就是构图了,其他的为什么最后得乘以个A的出度数**,我也不太理解.我还在思考中…感觉智商不够用啊。
分析:BEST定理、MatrixTree定理
代码:
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
using namespace std;
#define MAX 100010
#define MAXN 1000005
#define maxnode 5
#define sigma_size 30
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lrt rt<<1
#define rrt rt<<1|1
#define middle int m=(r+l)>>1
#define LL long long
#define ull unsigned long long
#define mem(x,v) memset(x,v,sizeof(x))
#define lowbit(x) (x&-x)
#define pii pair<int,int>
#define bits(a) __builtin_popcount(a)
#define mk make_pair
#define limit 10000
//const int prime = 999983;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f;
const double pi = acos(-1.0);
const double inf = 1e18;
const double eps = 1e-6;
const LL mod = 1e9+7;
const ull mx = 133333331;
/*****************************************************/
inline void RI(int &x) {
char c;
while((c=getchar())<'0' || c>'9');
x=c-'0';
while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0';
}
/*****************************************************/
bool isFreeX[5];
LL A[5][5];
LL atob,atoc,btoa,btoc,ctob,ctoa;
LL gauss(int n, int m) {//求行列式
for(int i = 0; i < m; ++i) isFreeX[i] = false;
LL ret = 1, neg = 0;
int r = 1, c = 1;//求n-1阶的行列式,去掉第一阶,所以从1开始
for(; r < n && c < m; ++r, ++c) {
int p = r;
for(; p < n; ++p) if(A[p][c]) break;
if(p == n) {--r; isFreeX[c] = true; continue;}
if(p != r) {
neg ^= 1;
for(int i = c; i <= m; ++i) swap(A[p][i], A[r][i]);
}
//eliminate coefficient
for(int i = r + 1; i < n; ++i) {
while(A[i][c]) {
LL delta = A[i][c] / A[r][c];
for(int j = c; j <= m; ++j) {
A[i][j] += mod - delta * A[r][j] % mod;
A[i][j] %= mod;
}
if(!A[i][c]) break;
neg ^= 1;
for(int j = c; j <= m; ++j) swap(A[r][j], A[i][j]);
}
}
}
if(r != n) return 0;
//0-r-1求n阶行列式,1-r-1求n-1阶行列式
for(int i = 1; i < r; ++i) ret = ret * A[i][i] % mod;
if(neg) ret = (-ret + mod) % mod;
return ret;
}
int deg[5];
LL fact[MAX];
void init(){
fact[0]=1;
for(int i=1;i<=100000;i++) fact[i]=fact[i-1]*i%mod;
}
LL qpow(LL a,LL n){
LL ans=1;
while(n){
if(n&1) ans=ans*a%mod;
a=a*a%mod;
n>>=1;
}
return ans;
}
LL C(int n,int m){
return (fact[n]*qpow(fact[m],mod-2)%mod)*qpow(fact[n-m],mod-2)%mod;
}
int main()
{
int a,b,c;
init();
while(cin>>a>>b>>c){
LL ans=0;
LL aa=a+b,bb=a+c,cc=c+b;
if(aa%2||bb%2||cc%2){
puts("0");continue;
}
for(int x=0;x<=a;x++)
{
atob=x;btoa=a-x;
btoc=bb/2-btoa;atoc=aa/2-x;
ctob=c-btoc;ctoa=b-atoc;
if(atoc<0||atob<0||btoa<0||btoc<0||ctoa<0||ctob<0) continue;
deg[0]=aa/2;
deg[1]=bb/2;
deg[2]=cc/2;
for(int i=0;i<3;i++) A[i][i]=deg[i];
A[0][1]=atob;A[0][2]=atoc;
A[1][0]=btoa;A[1][2]=btoc;
A[2][0]=ctoa;A[2][1]=ctob;
LL ret=(C(a,x)*C(b,atoc)%mod)*C(c,btoc)%mod;
ret=ret*gauss(3,3)%mod;
for(int i=0;i<3;i++) ret=ret*fact[deg[i]-1]%mod;
ret=ret*deg[0]%mod;
ans+=ret;
if(ans>=mod) ans-=mod;
}
cout<<ans<<endl;
}
return 0;
}