uva 11174

刘书上例题

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <sstream>
#include <string>
#include <cstring>
#include <algorithm>
#include <iostream>
#define maxn 40010
#define INF 0x7fffffff
#define inf 10000000
#define MOD 1000000007
#define ull unsigned long long
#define ll long long
using namespace std; ll fac[maxn];
int c[maxn];
bool vis[maxn];
vector<int> g[maxn]; void init()
{
fac[1] = 1;
for(int i = 2; i < maxn; ++ i) fac[i] = fac[i-1]*i%MOD;
} ll pow_mod(ll a, ll p, ll n)
{
if(p == 0) return 1;
ll ans = pow_mod(a, p/2, n);
ans = ans*ans%n;
if(p%2) ans = ans*a%n;
return ans;
} void dfs(int u)
{
c[u] = 1;
vis[u] = true;
for(int i = 0; i < (int)g[u].size(); ++ i)
{
if(!vis[g[u][i]]) dfs(g[u][i]);
c[u] += c[g[u][i]];
}
} int main()
{
int t, m, n;
scanf("%d", &t);
init();
while(t --)
{
memset(vis, 0, sizeof(vis));
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; ++ i) g[i].clear();
for(int i = 0; i < m; ++ i)
{
int u, v;
scanf("%d%d", &u, &v);
g[v].push_back(u);
}
for(int i = 1; i <= n; ++ i)
if(!vis[i]) dfs(i);
long long ans = fac[n];
for(int i = 1; i <= n; ++ i)
{
long long k = pow_mod(c[i], MOD - 2, MOD);
ans = ans * k % MOD;
}
printf("%lld\n", ans);
}
return 0;
}
上一篇:APUE第4章 文件和目录


下一篇:windows下Apache2.4配置php的解析运行