UVA 10766 Organising the Organisation(生成树计数)

题目链接:
UVA 10766 Organising the Organisation
题意:
给出 n,m,k ,代表一家公司有 n 个人,编号从1n,且指定编号为 k 的人为总经理,然后有m组关系,表示 a[i] 不想和 b[i] 有领属关系,求领属关系图的种类数?
数据范围:
1n50,1mn,0k1500
分析:
我觉得 k 的范围好像不大对,但是因为这道题实际上和k值无关,所以也就无关紧要了( ? )
把关系图看成一颗生成树,其实把所有可以存在领属关系的点看成可以连边,那么就是求生成树的个数了。
需要知道每个点的度数和点与点能够连边。
我是先默认每个点的度数为n1,默认任意两点可以 ,然后对于每个不能连边的 a,b ,将邻接矩阵 C[a][b]=C[b][a]=0 ,同时 degree[a],degree[b] .但是这道题有重边啊,这样子一来 degree[a],degree[b] 会多减了,所以需要先判断 C[a][b] 是否为 0 ,然后才决定是否degree[a],degree[b]

这道题还要用 longdouble 才能保证精度。。。
输入输出可以使用 cincout

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <climits>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iomanip>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
typedef long long ll;
const int MAX_N = 110;
const double eps = 1e-8;

int degree[MAX_N];
long double C[MAX_N][MAX_N];

int sgn(long double x)
{
    if(fabs(x) < eps) return 0;
    else if(x > 0) return 1;
    else return -1;
}

long double det(long double mat[][MAX_N], int n)
{
    long double res = 1.0;
    int cnt = 0;
    for(int i = 0; i < n; ++i) {
        if(sgn(mat[i][i]) == 0) {
            for(int j = i + 1; j < n; ++j) {
                if(sgn(mat[j][i] != 0)) {
                    for(int k = i; k < n; ++k) {
                        swap(mat[i][k], mat[j][k]);
                    }
                    cnt++;
                    break;
                }
            }
        }
        if(sgn(mat[i][i]) == 0) return 0;
        res *= mat[i][i];

        for(int j = i + 1; j < n; ++j) {
            mat[j][i] /= mat[i][i];
        }
        for(int j = i + 1; j < n; ++j) {
            for(int k = i + 1; k < n; ++k) {
                mat[j][k] -= mat[j][i] * mat[i][k];
            }
        }
    }
    if(cnt & 1) res = -res;
    return res;
}

int main()
{
    int n, m, k;
    while(cin >> n >> m >> k) {
        memset(degree, 0, sizeof(degree));
        memset(C, 0, sizeof(C));

        for(int i = 0; i < n; ++i) {
            degree[i] = n - 1; //默认每个点的入度为n - 1
            for (int j = 0; j < n; ++j) { //先默认任意两点可以建边
                C[i][j] = -1;
            }
        }

        for(int i = 0; i < m; ++i) {
            int a, b;
            cin >> a >> b;
            a--, b--;
            if(sgn(C[a][b]) == 0) continue; //会有重边啊!!!
            C[a][b] = C[b][a] = 0; //不能建边
            degree[a]--, degree[b]--;  
        }
        for(int i = 0; i < n; ++i) {
            C[i][i] = degree[i];
        }
        cout << fixed << setprecision(0) << det(C, n - 1) << endl;
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值