Tree Time limit:16000/8000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): Accepted submission (s): 13
Problem Description Soda has a undirected graph with n vertices and m edges. He is playing a game with Q rounds. In each round, he randomly chooses a edge from the graph. After Qrounds, he removes the duplicated edges. If the remaining edges form a tree, soda wins the game.
Soda wants to know the number of different ways to choose edge in each round that'll make him win the game.
As the answer would be very large, soda gives you another integer p. You should print the answer modulo p.
Note that the ways is considered different if at least in one round soda chooses different edges.
Input There is multiple test cases. The first line of input contains an integer T, indicating the number of the test cases. For each test case:
The first line contains four integers n,m,p,q (2≤n≤100,1≤m≤n (n−1) 2,1≤p,q≤109). Each of the following m lines contains a pair of integers x and y, so show that an edge exists between vertices x an D y (1& #8201;\le& #8201;x,& #8201;y& #8201;\le& #8201;n,& #8201;x& #8201;\ne& #8201; y). For each pair of vertices there is at the most one edge between them and no edge connects a vertex to itself.
Output for each test case, print the answer modulo p
Sample Input
2 6 6 23 10 6 3 6 4 5 1 2 5 1 4 5 4 6 5 23 5 5 6 4 6 3 1 5 1 1 2
Sample Output
16 5
Source bestcoder Round #58 (Div.1)
This is a very comprehensive topic:
To some sides, there is a Q operation, each time the election of an edge, q times after the duplicate side deleted. Ask how many kinds of extraction can make the final result a tree.
Method:
1. For a tree, the equivalent of the Q operation is selected n-1 species edge, because the graph of any one of the tree's sub-graph, the number of options selected is the same
Definition Dp[i][j] Indicates the number of scenarios in which J edges are selected for the i-th operation dp[i+1][j] = dp[i][j]*j+dp[i-1][j-1]* (n-1-j)
The transfer is either shifted from the selection of the J-1 bar, or from the J-Bar selected. Construct the NxN matrix, add the coefficients, and then solve them with a fast power.
2. The number of cases in which a graph can form a tree
Transferred from: http://www.cnblogs.com/kuangbin/archive/2013/07/27/3219707.html
Matrix-tree theorem (Kirchhoff matrix-tree theorem). Matrix-tree theorem is one of the most powerful weapons to solve spanning tree counting problem. It was first proven in 1847 by Kirchhoff. Before we introduce the theorem, we first define several concepts:
1, G of the degree matrix D[g] is a n*n matrix, and satisfies: when I≠j, dij=0; when I=j, Dij equals the degree of VI.
2, the adjacency matrix of G A[g] is also a n*n matrix, and satisfies: If VI, VJ between the side is directly connected, then Aij=1, otherwise 0.
We define the Kirchhoff matrix of G (also called the Laplace operator) C[g] to C[g]=d[g]-a[g], then the matrix-tree theorem can be described as: the number of all the different spanning trees of G equals its Kirchhoff matrix C[g] The absolute value of any determinant of a n-1-order master. The so-called n-1-order master, that is, for R (1≤r≤n), will C[g] the first R row, the first r column is removed after the new matrix, with Cr[g].
Then the problem becomes the determinant of the matrix. Previously, we used the method of finding the main element of a column to do a problem with the modulus being prime.
The title of the Modified blog: http://blog.csdn.net/firenet1/article/details/38065929
It is more difficult to take the composite of the problem.
In fact, the problem to be considered is to deal with the position of a[i][i], how to place the position of A[j][i] (J > I) and not 0 of the row of column I into 0.
Consider A = A[i][i], B = a[j][i]
Not once assumed abs (a) > abs (b) (other cases can be exchanged)
Then make a = a-b*a/b
After doing so a few times, A, B will appear a 0, the end can be.
Then Judge A[i][i] is not 0, is the words and the line J can be exchanged.
============== complexity analysis, A[i][i] is not 0, each operation of the new A[i][i] <= the old a[i][i], so the complexity of the averaging is relatively low.
This method of elimination of the code is also very good to write, relatively simple-----------------(---666 is not what I thought, I was to consult others, and then he thought out of)
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <math.h>
using namespace std;
#define LL Long Long
#define MAXN 101
int p;
struct node{
int MAT[MAXN][MAXN];
void Init () {
memset (mat,0,sizeof (MAT));
}
void O