1. 程式人生 > >UVA 10766 Organising the Organisation(生成樹計數 不取模)

UVA 10766 Organising the Organisation(生成樹計數 不取模)

Problem H
Organising the Organisation

Input: Standard Input

Output: Standard Output

Time Limit: 2 Second

I am the chief of the Personnel Division of a moderate-sized company that wishes to remain anonymous, and I am currently facing a small problem for which I need a skilled programmer's help.


Currently, our company is divided into several more or less independent divisions. In order to make our business more efficient, these need to be organised in a hierarchy, indicating which divisions are in charge of other divisions. For instance, if there are four divisions A, B, C and D we could organise them as in Figure 1, with division A controlling divisions B and D, and division D controlling division C.

One of the divisions is Central Management (division A in the figure above), and should of course be at the top of the hierarchy, but the relative importance of the remaining divisions is not determined, so in Figure 1 above, division C and D could equally well have switched places so that C was in charge over division D. One complication, however, is that it may be impossible to get some divisions to cooperate with each other, and in such a case, neither of these divisions can be directly in charge of the other. For instance, if in the example above A and D are unable to cooperate, Figure 1 is not a valid way to organise the company.

In general, there can of course be many different ways to organise the organisation, and thus it is desirable to find the best one (for instance, it is not a good idea to let the programming people be in charge of the marketing people). This job, however, is way too complicated for you, and your job is simply to help us find out how much to pay the consultant that we hire to find the best organisation for us. In order to determine the consultant's pay, we need to find out exactly how difficult the task is, which is why you have to count exactly how many

 different ways there are to organise the organisation.

Oh, and I need the answer in five hours.

Input

The input consists of a series of test cases, at most 50, terminated by end-of-file. Each test cases begins with three integers n, m, k (1 ≤ n ≤ 50, 0 ≤ m ≤ 1500, 1 ≤ k ≤ n)n denotes the number of divisions in the company (for convenience, the divisions are numbered from 1

 to n), and k indicates which division is the Central Management division. This is followed by m lines, each containing two integers 1 ≤ i, j ≤ n, indicating that division i and division j cannot cooperate (thus, i cannot be directly in charge of j and j cannot be directly in charge of i). You may assume that i and j are always different.

Output

For each test case, print the number of possible ways to organise the company on a line by itself. This number will be at least 1 and at most 1015.

Sample Input                               Output for Sample Input

5 5 2 
3 1 
3 4 
4 5 
1 4 
5 3 
4 1 1 
1 4 
3 0 2 
3 
8 

3


建圖簡單 但是這題不取模 可能會得到的值比較大 要使用long double 

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-10
#define MOD 10009
#define MAXN 110
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;

int Read()
{
    char ch;
    int a = 0;
    while((ch = getchar()) == ' ' | ch == '\n');
    a += ch - '0';
    while((ch = getchar()) != ' ' && ch != '\n')
    {
        a *= 10;
        a += ch - '0';
    }
    return a;
}

void Print(int a)
{
     if(a>9)
         Print(a/10);
     putchar(a%10+'0');
}

int sgn(long double x)
{
    if(fabs(x)<eps) return 0;
    if(x<0) return -1;
    else return 1;
}
long double b[MAXN][MAXN];
long double det(long double a[][MAXN],int n)
{
    int i,j,k,sign=0;
    long double ret=1;
    for(i=0;i<n;i++)
        for(j=0;j<n;j++)
            b[i][j]=a[i][j];
    for(i=0;i<n;i++)
    {
        if(sgn(b[i][i])==0)
        {
            for(j=i+1;j<n;j++)
                if(sgn(b[j][i])!=0)
                    break;
            if(j==n) return 0;
            for(k=i;k<n;k++)
                swap(b[i][k],b[j][k]);
            sign++;
        }
        ret*=b[i][i];
        for(k=i+1;k<n;k++)
            b[i][k]/=b[i][i];
        for(j=i+1;j<n;j++)
            for(k=i+1;k<n;k++)
                b[j][k]-=b[j][i]*b[i][k];
    }
    if(sign&1) ret=-ret;
    return ret;
}
long double a[MAXN][MAXN];
int g[MAXN][MAXN];

int main()
{
//    fread;
    int n,m,k;
    while(scanf("%d%d%d",&n,&m,&k)!=EOF)
    {
        MEM(g,1);
        while(m--)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            u--; v--;
            g[u][v]=g[v][u]=0;
        }
        MEM(a,0);
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                if(i!=j&&g[i][j])
        {
            a[i][i]++;
            a[i][j]=-1;
        }
        long double ans=det(a,n-1);
        printf("%.0Lf\n",ans);
    }
    return 0;
}


相關推薦

UVA 10766 Organising the Organisation成樹計數

Problem H Organising the Organisation Input: Standard Input Output: Standard Output Time Limit: 2 Second I am the chief of the Personne

UVA 10766 Organising the Organisation(成樹計數)

題目連結: UVA 10766 Organising the Organisation 題意: 給出n,m,k,代表一家公司有n個人,編號從1−n,且指定編號為k的人為總經理,然後有m組關係,表示a[i]不想和b[i]有領屬關係,求領屬關係圖的種類數?

UVA-10766 Organising the Organisation

生成樹計數模板題 發現這個的模板有很多種,有的套上來時WA,有的能AC 因為自己不懂怎麼求行列式,看不出有什麼問題,趕緊補習一下 #include<iostream> #include<cstdio> #include<cstring> #includ

kuangbin專題八 UVA10766 成樹計數Organising the Organisation請無視這篇文章

題意: 給出n,m,k,代表一家公司有n個部門,編號1到n,有m組關係,表示i和j不能直接聯通,k代表主管部門,問你有多少種分層方案。另外,這道題的k可以忽略掉,所以他的範圍完全是嚇唬人的。 題解: 抱歉,這道題我真的無法弄的通俗的說出來

kuangbin專題八 URAL1627 Join成樹計數

題意: 給出一個圖,’.’表示臥室,’*’表示儲藏間,每個格子上下左右都有一堵牆,然後需要打通一些臥室的牆(只能是相鄰房間才能打通)使得臥室之間聯通的方案數. 給每個臥室編個號,給可以打通的臥室加邊,就是裸的生成樹計數了. 題解: 打通一

UVA10766(Organising the Organisation)成樹計數-Matrix-Tree定理

/* *題目地址: *http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1707; * *題目大意:

STP成樹協議--學習筆記

STP(生成樹協議)--學習筆記一·STP STP協議在邏輯上斷開網絡的環路,防止廣播風暴的產生,而一旦正在使用的線路出現故障,邏輯上被斷開的線路又被連同,起到了冗余備份的作用。解決二層環路的問題。 二·生成樹工作原理 生成樹協議的算法過程可以歸納為三個步驟:選擇根網橋、選擇根端口、選擇指定端口。  (1)選

Gym-101630C:Connections成樹&構造

int conn namespace std size long long 生成 class ons 題意:給定N點,M條有向邊,滿足任意點可以到達任意點。現在叫你保留2*N邊,任然滿足任意點可以到達任意點,輸出刪除的邊。 思路:從1出發,DFS,得到一顆生成樹,有N-1

BZOJ1494: [NOI2007]成樹計數Berlekamp-Massey演算法

傳送門 題解: 直接打表+BM算出遞推式,BM具體實現可以戳這裡 附上一份其醜無比的BM程式碼: const int L=4e2; namespace bm { int cnt,a[N],fail[N],delta[N]; vector <int> R[N]

計蒜客:灌溉成樹

https://nanti.jisuanke.com/t/34 到了旱季農業生產的灌溉就成了一個大問題。為了保證灌溉的順利,某縣政府決定投資為各個村之間建立灌溉管道。 輸入第1行包括一個整數N,表示某縣的村莊的數量。(3≤N≤100),第2行-結尾為一個N×N的矩陣,表示每個村莊之間的距

Uva - 10047 】The Monocycle搜尋,bfs記錄狀態

題幹: Uva的題目就不貼上題幹了,,直接上題意吧。 有你一個獨輪車,車輪有5種顏色,為了5中顏色的相對位置是確定的。有兩種操作:1.滾動:輪子必須沿著順時針方向滾動,每滾動一次會到達另一個格子,著地的顏色會改變(順時針轉)。例如當前是綠色著地下一次就是黑色,依次是紅藍白。2.轉動:就是

網橋成樹網橋和源路由網橋

生成樹網橋: 網橋收到一個數據幀以後,執行地址表擴充和幀轉發,地址表擴充是指檢視信源結點地址,進行地址表的擴充,從而使網橋瞭解哪些節點在哪些子網中;幀轉發是指如果本地址表有信源地址裡所有的結點,則直接進行轉發;如果沒有,則傳送給本網橋所連線的所有子網(廣播) 此種方法得以

UVA 11481 Arrange the Numbers 簡單容斥

#include<bits/stdc++.h> using namespace std; #define debug puts("YES"); #define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++) #defin

資料結構——圖的連通性成樹、最小生成樹、生成森林

1、求圖的生成樹(或生成森林)       生成樹:是一個極小連通子圖,它含有圖中全部n個頂點,但只有n-1條邊。       生成森林: 由若干棵生成樹組成,含全部頂點,但構成這些樹的邊是最少的。    

AtCoder Regular Contest 093 E: Bichrome Spanning Tree成樹

包含 bool font return continue 找到 col include 情況下 Bichrome Spanning Tree 題意: 給出一個n個點,m條邊的無向連通圖,現在要給每條邊染色,可以染成黑色或者白色。 現在要求在染色完畢後,找出一個至少包含

hdu4305Lightning 成樹計數基爾霍夫矩陣+高斯消元+逆元

題意:比較裸的生成樹計數問題。   如何處理生成樹計數問題? 基爾霍夫矩陣: if i==j  Kir[i][j] = i的度數 if i!=j   Kir[i][j] = i到j的平行邊的個數的負數 即,基爾霍夫矩陣 = 度數矩陣 - 鄰接矩陣 將基爾霍夫矩陣刪去第i

成樹計數Matrix-Tree定理

以下轉載自http://blog.csdn.net/jarily/article/details/8901363 /* *演算法引入: *給定一個無向圖G,求它生成樹的個數t(G); * *演算法思想: *(1)G的度數矩陣D[G]是一個n*n的矩陣,並且滿足:

資料結構學習筆記20---圖的應用成樹與最小生成樹

上一篇部落格寫了圖的基本儲存於遍歷,在此基礎上,此篇部落格將會介紹圖的主要應用—–生成樹與最小生成樹。 (一)生成樹 定義:我總感覺書上定義比較繁瑣,因此就自己簡單定義了一下(可能不對哦),生成樹其實就是:對於一棵樹G,若頂點數為n,則在原來圖的基礎上把

【BZOJ1494】【NOI2007】成樹計數動態規劃,矩陣快速冪

題面 Description 最近,小棟在無向連通圖的生成樹個數計算方面有了驚人的進展,他發現: ·n個結點的環的生成樹個數為n。 ·n個結點的完全圖的生成樹個數為n^(n-2)。這兩個發現讓小棟欣喜若狂,由此更加堅定了他繼續計算生成樹個數的 想法,他

HDU 4305 Lightning 成樹計數+矩陣樹定理+逆元

題意:給你n個點,如果兩個點的距離小於等於r那麼就連一條邊,讓你求生成樹的個數。 題解: 對於無向圖G,它的Kirchhoff矩陣C定義為它的度數矩陣D減去它的鄰接矩陣A。顯然,這樣的定義滿足剛才描述的性質。 有了Kirchhoff矩陣這個工具,我們可以引入Matri