Test instructions: N personal queue, M-parent-child relationship, requires the father must be in front of the son (not necessarily adjacent), ask the maximum number of rows?
Idea: Father must be in front of the son, that is to say, father and son position is not interchangeable, then we may as well as father and son as a number, such as
2 is the father of 4 and 5, 3 is the father of 6, so we might as well consider these 5 people as 22233 in line, then the Total row is 5!/(3!*2!) , note that each parallel father, their sub-tree is consistent with the principle of multiplication, for those who do not have a father we give a virtual father 0, so that it is convenient for us to build up ~ because the factorial will be very large, so we can not directly obtain A/b value, then use multiplication inverse element good ~ (Do not know can go to see the expansion of Euclid), the Count can refer to the Great White Book P103 page.
The code is as follows:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include < vector>using namespace std; #define MAXN 40005#define mod 1000000007#define ll long longll JC[MAXN], ARR[MAXN],VIS[MAXN ], S[maxn];int T, M, N, A, b;vector<int>g[maxn];void gcd (ll A, ll B, ll& D, ll& x, ll& y) {if (!b) {d=a ; x=1;y=0;} ELSE{GCD (b, a%b, D, y, x); y-=x* (A/b);}} ll F (ll A, ll N) {ll d,x,y; GCD (A,n,d,x,y); Return d==1? (X+n)%n:-1;} int dfs (int u) {int num=0;for (int i=0; i<g[u].size (); i++) {Num+=dfs (g[u][i]);} Vis[u]=1;s[u]=++num;return s[u];} int main () {jc[0]=jc[1]=arr[1]=1;for (int i=2; i<maxn; i++) {jc[i]= (jc[i-1]*i)%mod;arr[i]=f (i, mod);} scanf ("%d", &t), while (t--) {scanf ("%d%d", &n, &m), memset (Vis, 0, sizeof (VIS)); for (int i=1; i<=n; i++) g[i ].clear (); for (int i=1; i<=m; i++) {scanf ("%d%d", &a, &b); G[b].push_back (a); }for (int i=1; i<=n; i++) {if (!vis[i]) DFS (i);} ll ans=jc[n];for (int i=1; i<=n; i++) {ans= (ans*arr[s[I]])%mod;} printf ("%lld\n", ans);} return 0; }
UVA11174 J.stand in a line (count + inverse)