题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=648&page=show_problem&problem=5150

题目大意:给一幅N个点M条边的无向图,有一些边,其中一部分只能涂红色,一部分只能涂黑色,一部分两种颜色都可以涂。现要求红色的边不超过K条的生成树个数模1e9+7的值。

思路:感谢昂神滋磁,贴链接:http://sd-invol.github.io/2015/05/31/Matrix-Tree-Polynomial/

由于不会范德蒙德矩阵,也不会拉格朗日插值,只好乖乖高斯消元了……

代码(0.429S):

 #include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
typedef long long LL;
typedef vector<vector<int> > Mat; const int MAXV = ;
const int MAXE = MAXV * MAXV;
const int MOD = 1e9 + ; void debug(const Mat &a) {
puts("#debug:");
for(auto &i : a) {
for(auto j : i) printf("%d ", j);
puts("");
}
} int inv(int x) {
if(x == ) return ;
return LL(MOD - MOD / x) * inv(MOD % x) % MOD;
} int det(Mat &a, int n) {
LL res = ;
for(int i = ; i < n; ++i) {
if(a[i][i] == ) return ;
for(int j = i + ; j < n; ++j) {
LL t = LL(a[j][i]) * inv(a[i][i]) % MOD;
for(int k = i; k < n; ++k) {
a[j][k] -= (a[i][k] * t) % MOD;
if(a[j][k] < ) a[j][k] += MOD;
}
}
res = (res * a[i][i]) % MOD;
}
return res;
} void guass(Mat &a, int n) {
for(int i = ; i < n; ++i) {
for(int j = i + ; j < n; ++j) {
LL t = LL(a[j][i]) * inv(a[i][i]) % MOD;
for(int k = i; k <= n; ++k) {
a[j][k] -= (a[i][k] * t) % MOD;
if(a[j][k] < ) a[j][k] += MOD;
}
}
}
for(int i = n - ; i >= ; --i) {
for(int j = i + ; j < n; ++j) {
a[i][n] -= (LL(a[i][j]) * a[j][n]) % MOD;
if(a[i][n] < ) a[i][n] += MOD;
}
a[i][n] = LL(a[i][n]) * inv(a[i][i]) % MOD;
}
} int la[MAXE], lb[MAXE], kind[MAXE];
int T, n, m, k; int get_column(int a) {
Mat mat(n, vector<int>(n));
for(int i = ; i < m; ++i) {
int t = (kind[i] & ) * a + (kind[i] >> );
mat[la[i]][la[i]] += t;
mat[lb[i]][lb[i]] += t;
mat[la[i]][lb[i]] = mat[lb[i]][la[i]] = (t > ? MOD - t : );
}
return det(mat, n);
} int solve() {
Mat mat(n, vector<int>(n + ));
for(int i = ; i < n; ++i) {
LL tmp = ;
for(int j = ; j < n; ++j)
mat[i][j] = tmp, tmp = (tmp * i) % MOD;
mat[i][n] = get_column(i);
}
//debug(mat);
guass(mat, n); int res = ;
for(int i = ; i <= k; ++i) {
res += mat[i][n];
if(res >= MOD) res -= MOD;
}
return res;
} int main() {
scanf("%d", &T);
for(int t = ; t <= T; ++t) {
scanf("%d%d%d", &n, &m, &k);
for(int i = ; i < m; ++i) {
scanf("%d%d%d", &la[i], &lb[i], &kind[i]);
la[i]--, lb[i]--;
}
printf("Case #%d: %d\n", t, solve());
}
}

UVALive 7138 The Matrix Revolutions(Matrix-Tree + 高斯消元)(2014 Asia Shanghai Regional Contest)的更多相关文章

  1. UVALive 7148 LRIP(树的分治+STL)(2014 Asia Shanghai Regional Contest)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...

  2. UVALive 7146 Defeat the Enemy(贪心+STL)(2014 Asia Shanghai Regional Contest)

    Long long ago there is a strong tribe living on the earth. They always have wars and eonquer others. ...

  3. UVALive 7143 Room Assignment(组合数学+DP)(2014 Asia Shanghai Regional Contest)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...

  4. UVALive 7141 BombX(离散化+线段树)(2014 Asia Shanghai Regional Contest)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...

  5. UVALive 7147 World Cup(数学+贪心)(2014 Asia Shanghai Regional Contest)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...

  6. UVALive 7139 Rotation(矩阵前缀和)(2014 Asia Shanghai Regional Contest)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...

  7. UVALive - 6185 Find the Outlier暴力填表+高斯消元+卡eps

    https://cn.vjudge.net/problem/UVALive-6185 我真的是服了orz eps 1e5,1e6过不了 开1e2 1e1都能过 题意:给你一个d阶多项式f的f(0),f ...

  8. The 2019 ICPC Asia Shanghai Regional Contest H Tree Partition k、Color Graph

    H题意: 给你一个n个节点n-1条无向边构成的树,每一个节点有一个权值wi,你需要把这棵树划分成k个子树,每一个子树的权值是这棵子树上所有节点权值之和. 你要输出这k棵子树的权值中那个最大的.你需要让 ...

  9. Matrix 高斯消元Gaussian elimination 中的complete pivoting和partial pivoting

    首先科普下Pivoting的含义 一般翻译为“主元”,在对矩阵做某种算法时,首先进行的部分元素.在线性规划的单纯形法中常见.wiki的解释如下:Pivot element(the first elem ...

随机推荐

  1. Codeforces632E Thief in a Shop(NTT + 快速幂)

    题目 Source http://codeforces.com/contest/632/problem/E Description A thief made his way to a shop. As ...

  2. [BZOJ3874][AHOI2014] 宅男计划

    Description 外卖店一共有N种食物,分别有1到N编号.第i种食物有固定的价钱Pi和保质期Si.第i种食物会在Si天后过期.JYY是不会吃过期食物的.比如JYY如果今天点了一份保质期为1天的食 ...

  3. type of 操作符和instanceof操作符的区别以及使用方法

    经常见到用typeof和instanceof检测一个变量类型,作为前端小白经常不知道这两者具体的详细用法和区别,今天就整理一下谨记! javaScript中有6中数据类型: 1.Undefinde 2 ...

  4. java分享第七天-03(递归打印文件目录的树状结构)

    public static void main(String[] args) { File file= new File("e:/list"); printFile(file, 0 ...

  5. Liunx下的有关于tomcat的相关操作 && Liunx 常用指令

    先记录以下liunx下的有关于tomcat的相关操作 查看tomcat进程: ps-ef|grep java (回车) 停止tomcat进程: kill -9 PID (进程号如77447) (回车) ...

  6. javaScript中其他类型的值转换为Boolean类型

    将javaScript中其他任意类型的值转换为对应Boolean类型的值. 一  将number类型的值转换为Boolean类型 数值为0: var myBoolean = new Boolean(0 ...

  7. hihoCoder 1183 连通性一·割边与割点(Tarjan求割点与割边)

    #1183 : 连通性一·割边与割点 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 还记得上次小Hi和小Ho学校被黑客攻击的事情么,那一次攻击最后造成了学校网络数据的丢 ...

  8. [不好分类]SD卡无法读取,显示RAW

    上周同事拿来了一个8G的SD卡,插入读卡器后显示“需要格式化”.无法读取.文件格式处显示“RAW”,磁盘大小显示0字节. 处理步骤如下: 1.按照提示,格式化,选择“快速格式化”. 2.采用数据恢复软 ...

  9. RFID电子标签天线的印刷

    RFID 电子标签技术又称RFID(Radio FrequencyIdentification)射频识别技术,是一种非接触式的自动识别技术,通过相距几厘米到几米距离内传感器发射的无线电波,可以读取RF ...

  10. CSS定位的三种机制:普通流、绝对定位和浮动

    1.普通流: position : static – 元素框正常生成.即上述不对元素进行任何样式设置的默认形态. position : relative (此时设置top, right, bottom ...