Uva10766 (organising the organisation) generates the tree count-matrix-tree Theorem

Source: Internet
Author: User
/** Question address: * http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & page = show_problem & problem = 1707; * In order to improve the company's work efficiency, the Board of Directors decided to reclassify all employees. * except for one General Manager, all other employees have only one direct supervisor; * Due to the direct interpersonal relationship between employees, A and B may be unwilling to lead the other party directly. * n employees in the company 1 ~ N number, and the board of directors has decided to assign K employees to the general manager. * Jimmy's task is to have a different number of employee classification schemes ;** Algorithm Thought: * if there is no conflict between A and B, there will be an edge between them. * The relationship graph between the employees finally obtained is a spanning tree of the source image; * Although the root of the generative tree is specified, the number of undirected graph spanning trees is irrelevant to the root. * You only need to use the matrix-tree theorem to calculate the number of original graph spanning trees; ** matrix-tree theorem: the number of all different spanning trees of * g is equal to the absolute value of any n-1 primary-child type of the Kirchhoff matrix C [g; * The n-1 primary sub-formula is the new matrix obtained after removing the r row and R column of C [g] For r (1 ≤ r ≤ n, using CR [g]; **/# include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <algorithm> using namespace STD; const int n = 55; typedef long ll; int d [N] [N]; ll C [N] [N]; // Kirchhoff matrix ll det (ll a [] [N], int N) // generate tree count: matrix-tree theorem {ll ret = 1; for (INT I = 1; 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); For (int K = I; k <N; k ++) Swap (A [I] [K], a [J] [k]); ret =-ret ;} if (A [I] [I] = 0) return 0; ret = RET * A [I] [I];} If (Ret <0) ret =-ret; return ret;} int main () {// freopen ("C: \ Users \ admin Istrator \ Desktop \ kd.txt "," r ", stdin); int n, m, K; while (~ Scanf ("% d", & N, & M, & K) {memset (C, 0, sizeof (c); memset (D, 0, sizeof (d); int U, V; while (M --) {scanf ("% d", & U, & V); U --; V --; d [u] [v] = d [v] [u] = 1 ;}for (INT I = 0; I <n; I ++) {int u = 0; for (Int J = 0; j <n; j ++) {if (I! = J &&! D [I] [J]) {u ++; C [I] [J] =-1 ;}} C [I] [I] = u ;} ll res = det (C, N); printf ("% LLD \ n", Res);} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.