UVALive 7138 The Matrix Revolutions(Matrix-Tree + 高斯消元)(2014 Asia Shanghai Regional Contest)
题目链接: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)的更多相关文章
- UVALive 7148 LRIP(树的分治+STL)(2014 Asia Shanghai Regional Contest)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...
- 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. ...
- UVALive 7143 Room Assignment(组合数学+DP)(2014 Asia Shanghai Regional Contest)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...
- UVALive 7141 BombX(离散化+线段树)(2014 Asia Shanghai Regional Contest)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...
- UVALive 7147 World Cup(数学+贪心)(2014 Asia Shanghai Regional Contest)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...
- UVALive 7139 Rotation(矩阵前缀和)(2014 Asia Shanghai Regional Contest)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...
- UVALive - 6185 Find the Outlier暴力填表+高斯消元+卡eps
https://cn.vjudge.net/problem/UVALive-6185 我真的是服了orz eps 1e5,1e6过不了 开1e2 1e1都能过 题意:给你一个d阶多项式f的f(0),f ...
- The 2019 ICPC Asia Shanghai Regional Contest H Tree Partition k、Color Graph
H题意: 给你一个n个节点n-1条无向边构成的树,每一个节点有一个权值wi,你需要把这棵树划分成k个子树,每一个子树的权值是这棵子树上所有节点权值之和. 你要输出这k棵子树的权值中那个最大的.你需要让 ...
- Matrix 高斯消元Gaussian elimination 中的complete pivoting和partial pivoting
首先科普下Pivoting的含义 一般翻译为“主元”,在对矩阵做某种算法时,首先进行的部分元素.在线性规划的单纯形法中常见.wiki的解释如下:Pivot element(the first elem ...
随机推荐
- 通用数据库操作类,前端easyui-datagrid,form
实现功能: 左端datagrid显示简略信息,右侧显示选中行详细信息,数据库增删改 (1)点击选中行,右侧显示详细信息,其中[新增].[修改].[删除]按钮可用,[保存]按钮禁用 (2)点击[ ...
- .net core 基本概念
asp.net core 是基于 .net core的,所以能够跨平台. 目前存在.NET Framework (CLR), .NET Core (CoreCLR) or Mono,可根据项目的具体情 ...
- 浅析mongodb中group分组
这篇文章主要介绍了浅析mongodb中group分组的实现方法及示例,非常的简单实用,有需要的小伙伴可以参考下. group做的聚合有些复杂.先选定分组所依据的键,此后MongoDB就会将集合依据选定 ...
- worldwind一些资料
worldwind一些资料: http://blog.csdn.net/jk276993857/article/category/710116 http://blog.csdn.net/paul_xj ...
- 【Alpha】Daily Scrum Meeting第六次
一.本次Daily Scrum Meeting主要内容 各队员的任务完成情况 接下去要做的任务有哪些方面的问题 二.项目进展 学号尾数 今日已完成任务 接下去要做 502 统一Excel表头数据的英文 ...
- 记一次windows下物理迁移数据库的过程
背景: 最近因为一次设备故障,导致一台运行windows环境下的机器无法启动,里面有一个正在使用的财务数据库,该数据库也只是每月使用一次,需要把物理数据迁移出来,于是拔出了故障机器的硬盘,通 ...
- Lua数据持久化
1.数据文件 我们可以利用Lua中的table构造式来定义一种文件格式,即文件中的数据是table构造并初始化的代码 ,这种方式对于Lua程序而言是非常方便和清晰的,如: Entry{" ...
- MongoDB-3.2.6 副本集 和主从
yum实例 vim /etc/yum.repos.d/mongodb-org-3.2.repo [mongodb-org-3.2] name=Mongodb baseurl=http://repo.m ...
- CPP - sort
#include "stdafx.h" #include <iostream> #include <string> using namespace std; ...
- 【转】 NGUI 监听按钮除OnClick外其他事件的方法,附简易改编的UIButton类
http://blog.csdn.net/icefantasylcj/article/details/49450555 大家好,我是雨中祈雨.一直以来,CSDN都是我最好的编程助手.这是我在CSDN的 ...