题目链接: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. 2016-2017 ACM-ICPC, NEERC, Southern Subregional Contest

    A. Toda 2 按题意模拟即可. #include <bits/stdc++.h> using namespace std ; typedef pair < int , int ...

  2. IIS运行.NET4.0配置

    IIS运行.NET4.0配置 “/CRM”应用程序中的服务器错误.配置错误说明: 在处理向该请求提供服务所需的配置文件时出错.请检查下面的特定错误详细信息并适当地修改配置文件. 分析器错误消息: 无法 ...

  3. C# CodeFirst(EF框架)代码优先创建数据库

    namespace WebEF.Model{ public class ModelContext:DbContext //继承DBcontext 来自EF框架 { public ModelContex ...

  4. Oracel EBS - Search Report by Response & Group

  5. 关于java.lang.NoSuchMethodError: android.widget.RelativeLayout.setBackground的解决办法

    今天用一个安卓4.0.4版本的手机测试手上的项目,发现logcat弹出这样一个提示“java.lang.NoSuchMethodError: android.widget.RelativeLayout ...

  6. mysql索引的一些知识

    一.MySQL索引类型 mysql里目前只支持4种索引分别是:full-text,b-tree,hash,r-tree b-tree索引应该是mysql里最广泛的索引的了,除了archive基本所有的 ...

  7. 百度地图 获取两点坐标之间的驾车距离(非直线距离) c#

    百度接口了解: http://lbsyun.baidu.com/index.php?title=webapi/route-matrix-api-v2 起点与终点为多对多关系,如果你只想取两个坐标,那就 ...

  8. 《实时控制软件设计》第一周作业 欧梓峰 U201317662 (更新)

    CNC 插补计算程序分析 前言:插补(Interpolation),即机床数控系统依照一定方法确定刀具运动轨迹的过程.一般是已知起点坐标.终点坐标和轨迹,由数控插补计算程序实时的算出各个中间的坐标来拟 ...

  9. js 滚动的文字(走马灯)

    // 滚动的文字 function marqueeScroll() { //var $target = $(".marquee_outer:visible"); if($(&quo ...

  10. solr了解的一些东西

    近一周学习了一下solr,大概了解了一些东西,学会了如何为数据库中数据添加索引,中文分词怎样使用的一些知识 1.在网上的大多资料都是使用solr要安装tomacat,一时很迷茫,tomacat我理解是 ...