UvaLive 6600 Spanning trees in a secure lock pattern 矩阵行列式

链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4611

题意:给一个N*N个点的矩阵(N<=6),每个点只能和周围八个点相连,问有多少种生成树的方式。

思路:题里给的很明白,就是列一个每个点的边的矩阵,然后求子矩阵的行列式就可以了,因为N只有6,所以打表就可以了。

打表代码:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define eps 1e-8
#define INF 0x7fffffff
#define PI acos(-1.0)
#define seed 31//131,1313
typedef long long LL;
typedef unsigned long long ULL;
using namespace std;
#define MOD 1000
#define maxn 40
#define maxm 40
struct Matrix
{
    int n,m;
    double a[maxn][maxm];
    void change(int c,int d)
    {
        n=c;
        m=d;
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
                a[i][j]=0;
    }
    void Copy(const Matrix &x)
    {
        n=x.n;
        m=x.m;
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
                a[i][j]=x.a[i][j];
    }
    void build(int n)
    {
        change(n*n,n*n);
        for(int i=0; i<n*n; i++)
        {
            if(i%n!=0)
            {
                a[i][i-1]=-1;
                a[i-1][i]=-1;
                a[i][i]++;
                a[i-1][i-1]++;
            }
            if(i%n!=0&&i/n!=0)
            {
                a[i][i-n-1]=-1;
                a[i-n-1][i]=-1;
                a[i][i]++;
                a[i-n-1][i-n-1]++;
            }
            if(i%n!=0&&i/n!=n-1)
            {
                a[i][i+n-1]=-1;
                a[i+n-1][i]=-1;
                a[i][i]++;
                a[i+n-1][i+n-1]++;
            }
            if(i/n!=n-1)
            {
                a[i][i+n]=-1;
                a[i+n][i]=-1;
                a[i][i]++;
                a[i+n][i+n]++;
            }
        }
    }
    double det()
    {
        for(int i=1; i<n; i++)
        {
            for(int j=0; j<i; j++)
                if(a[i][j]!=0)
                {
                    for(int k=j+1; k<m; k++)
                        a[i][k]-=(a[j][k]*a[i][j]/a[j][j]);
                    a[i][j]=0;
                }
        }
        double ans=1;
        for(int i=0; i<n-1; i++)
            ans*=a[i][i];
        return ans;
    }
};
int main()
{
    int t;
    scanf("%d",&t);
    Matrix A;
    A.build(t);
    printf("%.0f\n",A.det());
    return 0;
}

AC代码:

int main()
{
    char ss[10][40]={"1","16","17745","1064918960","3271331573452806","504061943351319050000000"};
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int a;
        scanf("%d",&a);
        puts(ss[a-1]);
    }
}
时间: 2024-12-10 08:43:16

UvaLive 6600 Spanning trees in a secure lock pattern 矩阵行列式的相关文章

11.5最小生成树(Minimum Spanning Trees)

11.5最小生成树(Minimum Spanning Trees) 对加权图求使得权值和最小的生成树,即为最小生成树,基于以点为基准和以边为基准,有两种求最小生成树的方法:Prim算法和Kruskal 最小生成树的具体算法实现 原文地址:https://www.cnblogs.com/SpicyArticle/p/12150738.html

多线程程序设计学习(7)read-write lock pattern

Read-Write Lock Pattern[读写]一:Read-Write Lock Pattern的参与者--->读写锁--->数据(共享资源)--->读线程--->写线程 二Read-Write Lock Pattern模式什么时候使用---> * 为了多线线程环境下保护数据安全,我们必须避免的冲突 * 一个线程读取,另一个线程写入的read-write conflick * 一个线程写入,另一个线程写入的write-write conflick * 一个线程读取,另

Android Lock Pattern 图案解锁

参考链接:http://www.cnblogs.com/dyingbleed/archive/2012/12/03/2800007.html http://blog.csdn.net/way_ping_li/article/details/8925936 Android Lock Pattern 图案解锁,码迷,mamicode.com

Minimum Spanning Trees

最小生成树 Introduction 图的生成树是它的一棵含有其所有顶点的无环连通子图.一幅加权无向图的最小生成树(MST)是它的一棵权值(树中所有边的权值之和)最小的生成树. Greedy Algorithm 假定图是连通的,且各个边有不同的权值,这样图就会存在唯一一棵最小生成树. Cut Property 切分将图的所有点分为两个非空且不重复的集合,横切边(crossing edge)指连接两个集合的边. 切分定理:在一幅加权图中,给定任意的切分,它的横切边中权重最小者必然属于图的最小生成树

【C++设计模式】单件类与DCLP(Double Check Lock Pattern)的风险

[单件类] 保证只能有一个实例化对象,并提供全局的访问入口. [设计注意事项] 1.阻止所有实例化的方法: private 修饰构造函数,赋值构造函数,赋值拷贝函数. 2.定义单实例化对象的方法: a.使用static 修饰 b.使用new+delete的方法 3.多线程版本: 使用双检测锁定,即先检测单实例对象是否存在:不存在,使能"锁",再次判断实例是否存在,不存在就创建该单实例对象. A.单层锁示例: Singleton* Singleton::getInstance() { L

【HDU 4408】Minimum Spanning Tree(最小生成树计数)

Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Kruskal algorithm of minimum spanning tree, XXX finds that there might be multiple solutions. Given an undirected weighted graph with n (1<=n<=100) vertex

HDOJ 题目4408 Minimum Spanning Tree(Kruskal+Matrix_Tree)

Minimum Spanning Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1408    Accepted Submission(s): 450 Problem Description XXX is very interested in algorithm. After learning the Prim algori

HDU 4408 Minimum Spanning Tree 最小生成树计数

Minimum Spanning Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) [Problem Description] XXX is very interested in algorithm. After learning the Prim algorithm and Kruskal algorithm of minimum spanning tree, XXX

Project 2 - Spanning Tree Protocol

Project 2 - Spanning Tree ProtocolIn the lectures, you learned about Spanning Trees, which can be used to prevent forwardingloops on a layer 2 network. In this project, you will develop a simplified distributed version ofthe Spanning Tree Protocol th