• spoj 104 Highways(生成树计数)


    http://www.spoj.pl/problems/HIGH/

      题目写着答案不超过long long,应该是中间过程爆long long了,所以用整型做的时候在辗转相除的过程超时了,后来改成double就过了!

    代码如下:

    View Code
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <cmath>
     5 
     6 using namespace std;
     7 
     8 //typedef long long ll;
     9 typedef double ll;
    10 const double eps = 1e-8;
    11 const int maxSize = 15;
    12 int curSize = maxSize;
    13 
    14 struct Mat {
    15     ll val[maxSize][maxSize];
    16 
    17     Mat() {
    18         for (int i = 0; i < curSize; i++) {
    19             for (int j = 0; j < curSize; j++) {
    20                 val[i][j] = 0;
    21             }
    22         }
    23     }
    24     ll Det() {
    25         ll ret = 1;
    26 
    27         for (int p = 1; p < curSize; p++) {
    28             for (int l = p + 1; l < curSize; l++) {
    29 //                while (val[l][p]) {
    30                 while (fabs(val[l][p]) > eps) {
    31 //                    int k = val[p][p] / val[l][p];
    32                     double k = val[p][p] / val[l][p];
    33 
    34                     for (int i = p; i < curSize; i++) {
    35                         val[p][i] -= val[l][i] * k;
    36                         swap(val[p][i], val[l][i]);
    37                     }
    38                     ret = -ret;
    39                 }
    40             }
    41 //            if (!val[p][p]) return 0;
    42         }
    43 
    44         for (int i = 1; i < curSize; i++) {
    45             ret *= val[i][i];
    46         }
    47 
    48         return ret;
    49     }
    50 } mat;
    51 
    52 void makeMat(int m) {
    53     int s, t;
    54 
    55     mat = Mat();
    56     while (m--) {
    57         scanf("%d%d", &s, &t);
    58         s--; t--;
    59 //        if (!mat.val[s][t]) {
    60         if (fabs(mat.val[s][t]) < eps) {
    61             mat.val[s][t] = mat.val[t][s] = -1;
    62             mat.val[s][s]++; mat.val[t][t]++;
    63         }
    64     }
    65 }
    66 
    67 int main() {
    68     int T, m;
    69 
    70 //    freopen("in", "r", stdin);
    71     scanf("%d", &T);
    72     while (T-- && ~scanf("%d%d", &curSize, &m)) {
    73         makeMat(m);
    74 //        printf("%lld\n", mat.Det());
    75         printf("%.0f\n", mat.Det());
    76     }
    77 
    78     return 0;
    79 }

    ——written by Lyon

  • 相关阅读:
    vue+element实现图片上传
    react----Hooks的基本使用
    js知识点大杂烩
    vue面试题(一)
    从一个字符串中找出重复次数最多的字符?
    百度搜索
    js--拖拽
    js循环嵌套,打印图形
    js--sort()排序
    闰秒调整扫盲
  • 原文地址:https://www.cnblogs.com/LyonLys/p/spoj_104_Lyon.html
Copyright © 2020-2023  润新知