题目链接: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. React使用jquery方式动态获取数据

    好久没写react了,今天有空写一下来react实现实时请求数据,并刷新数据的小demo. 首先我还是选择了jquery方式中自带的ajax获取数据,首先要引用所需的js包 接下来要写一个自定义的js ...

  2. 【BO】WEBI文件打开时提示Illegal access错误

    在infoview中打开WEBI文件时,提示如下错误. 通过查看SCN,找到错误原因是CMC中有一个服务没有启动. 启动这个服务即可: WebIntelligenceProcessingServer

  3. 【hihoCoder】1039 : 字符消除

    题目:http://hihocoder.com/problemset/problem/1039 给定一个字符串s,只包含'A', 'B', 'C'三种字符 1. 向 s 的任意位置 (包括头和尾) 中 ...

  4. DOS 命令For精解示例

    最基本形态: 在cmd 窗口中:for %I in (command1) do command2 在批处理文件中:for %%I in (command1) do command2 在批处理中,FOR ...

  5. 动态调用webservice(部分转载)

    动态调用webservice,做个笔记: public class WSHelper { /// < summary> /// 动态调用web服务 /// < /summary> ...

  6. springboot+dubbo

    使用springboot搭建dubbo服务,首先封装出springboot-dubbo的项目,引入在服务上加直接@Service直接使用,方便于拆封统一管理. package hello.dubbo. ...

  7. MVC中使用EF的技巧集

    1.从数据库生成模型后,再次更新模型时,之前设置的验证规则会丢失. 解决方法:在Models文件夹中新建一个空白类,把它命名为shujuyanzh.cs(类名可以自定),然后把Models中自动生成的 ...

  8. 20145220&20145209&20145309信息安全系统设计基础实验报告(4)

    20145220&20145209&20145309信息安全系统设计基础实验报告(4) 实验报告链接: http://www.cnblogs.com/zym0728/p/6132246 ...

  9. 【转】 NGUI 监听按钮除OnClick外其他事件的方法,附简易改编的UIButton类

    http://blog.csdn.net/icefantasylcj/article/details/49450555 大家好,我是雨中祈雨.一直以来,CSDN都是我最好的编程助手.这是我在CSDN的 ...

  10. (。・・)ノ~java常见错误

    空指针错误 刚开始接触数组的时候,最容易出现的错误就是空指针错误.所谓空指针错误就是,一个引用指向了一个空的地址,而空的地址,没有地址,更没有数据,这时候用这引用去和别的数据对比,显然要出错.为了避免 ...