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):
1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 #include <iostream> 5 #include <vector> 6 using namespace std; 7 typedef long long LL; 8 typedef vector<vector<int> > Mat; 9 10 const int MAXV = 55; 11 const int MAXE = MAXV * MAXV; 12 const int MOD = 1e9 + 7; 13 14 void debug(const Mat &a) { 15 puts("#debug:"); 16 for(auto &i : a) { 17 for(auto j : i) printf("%d ", j); 18 puts(""); 19 } 20 } 21 22 int inv(int x) { 23 if(x == 1) return 1; 24 return LL(MOD - MOD / x) * inv(MOD % x) % MOD; 25 } 26 27 int det(Mat &a, int n) { 28 LL res = 1; 29 for(int i = 1; i < n; ++i) { 30 if(a[i][i] == 0) return 0; 31 for(int j = i + 1; j < n; ++j) { 32 LL t = LL(a[j][i]) * inv(a[i][i]) % MOD; 33 for(int k = i; k < n; ++k) { 34 a[j][k] -= (a[i][k] * t) % MOD; 35 if(a[j][k] < 0) a[j][k] += MOD; 36 } 37 } 38 res = (res * a[i][i]) % MOD; 39 } 40 return res; 41 } 42 43 void guass(Mat &a, int n) { 44 for(int i = 0; i < n; ++i) { 45 for(int j = i + 1; j < n; ++j) { 46 LL t = LL(a[j][i]) * inv(a[i][i]) % MOD; 47 for(int k = i; k <= n; ++k) { 48 a[j][k] -= (a[i][k] * t) % MOD; 49 if(a[j][k] < 0) a[j][k] += MOD; 50 } 51 } 52 } 53 for(int i = n - 1; i >= 0; --i) { 54 for(int j = i + 1; j < n; ++j) { 55 a[i][n] -= (LL(a[i][j]) * a[j][n]) % MOD; 56 if(a[i][n] < 0) a[i][n] += MOD; 57 } 58 a[i][n] = LL(a[i][n]) * inv(a[i][i]) % MOD; 59 } 60 } 61 62 int la[MAXE], lb[MAXE], kind[MAXE]; 63 int T, n, m, k; 64 65 int get_column(int a) { 66 Mat mat(n, vector<int>(n)); 67 for(int i = 0; i < m; ++i) { 68 int t = (kind[i] & 1) * a + (kind[i] >> 1); 69 mat[la[i]][la[i]] += t; 70 mat[lb[i]][lb[i]] += t; 71 mat[la[i]][lb[i]] = mat[lb[i]][la[i]] = (t > 0 ? MOD - t : 0); 72 } 73 return det(mat, n); 74 } 75 76 int solve() { 77 Mat mat(n, vector<int>(n + 1)); 78 for(int i = 0; i < n; ++i) { 79 LL tmp = 1; 80 for(int j = 0; j < n; ++j) 81 mat[i][j] = tmp, tmp = (tmp * i) % MOD; 82 mat[i][n] = get_column(i); 83 } 84 //debug(mat); 85 guass(mat, n); 86 87 int res = 0; 88 for(int i = 0; i <= k; ++i) { 89 res += mat[i][n]; 90 if(res >= MOD) res -= MOD; 91 } 92 return res; 93 } 94 95 int main() { 96 scanf("%d", &T); 97 for(int t = 1; t <= T; ++t) { 98 scanf("%d%d%d", &n, &m, &k); 99 for(int i = 0; i < m; ++i) { 100 scanf("%d%d%d", &la[i], &lb[i], &kind[i]); 101 la[i]--, lb[i]--; 102 } 103 printf("Case #%d: %d\n", t, solve()); 104 } 105 }
View Code
转载于:https://www.cnblogs.com/oyking/p/4598601.html
UVALive 7138 The Matrix Revolutions(Matrix-Tree + 高斯消元)(2014 Asia Shanghai Regional Contest)...相关推荐
- 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 7139 Rotation(矩阵前缀和)(2014 Asia Shanghai Regional Contest)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...
- Matrix 高斯消元Gaussian elimination 中的complete pivoting和partial pivoting
首先科普下Pivoting的含义 一般翻译为"主元",在对矩阵做某种算法时,首先进行的部分元素.在线性规划的单纯形法中常见. wiki的解释如下: Pivot element (t ...
- 2020 ICPC 济南 A Matrix Equation (高斯消元)
题目链接A-Matrix Equation 第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(济南) 题目描述 We call a matrix "01 Square" i ...
- 2020 icpc济南 A - Matrix Equation (高斯消元求自由元个数)
链接: A - Matrix Equation 题意: 给一个 A 矩阵 一个 B 矩阵(矩阵元素为 0 或 1),求有多少个 C 矩阵 满足 A X C = B . C (叉乘 和 点乘). 思路: ...
- 2020ICPC济南站 A题 Matrix Equation高斯消元求异或方程组
2020ICPC济南站 A题 Matrix Equation高斯消元求异或方程组 题意 思路 Code() 传送门: https://ac.nowcoder.com/acm/contest/10662 ...
- 2020 ACM / ICPC 济南 A Matrix Equation (高斯消元、乘法原理)
整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 题目链接 给你定义两种 010101 矩阵上的运算: Xi,j×Yi,j=(∑k=1NXi,kYk,j ...
- UVALive 7455 Linear Ecosystem (高斯消元)
Linear Ecosystem 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/B Description http://7xj ...
- Rocksdb Ribbon Filter : 结合 XOR-filter 以及 高斯消元算法 实现的 高效filter
文章目录 前言 XOR-filter 实现原理 xor filter 的构造原理 xor filter 构造总结 XOR-filter 和 ADD-filter对比 XOR-filter 在计算上的优 ...
最新文章
- thirft支持双向通信
- 安卓开发 adb命令使用
- Undefined symbols for architecture i386问题解决方法
- uwp应用在debug模式下运行正常,编译为release版本的时候抛出异常
- 保驾护航金三银四,含BATJM大厂
- zemax评价函数编辑器_ZEMAX与光学设计案例:激光扩束系统详细设计与公差分析(二)...
- 《财富》2020中国40岁以下商界精英榜出炉:张一鸣位列榜首
- MySQL高级-MySQL并发参数调整
- 海康威视、大华监控摄像头rtsp地址规则
- 速修复!这个严重的 Apache Struts RCE 漏洞补丁不完整
- windows 和 ubuntu服务器之间用Xshell互传文件
- Eclipse 模板使用
- java 数组有序_Java有序数组
- 基础的风光摄影技术控制
- 苹果怎么锁定计算机,苹果电脑如何锁定屏幕-mac锁定屏幕教程 - 河东软件园
- ❤️「Python」初阶,必看系列, 万字只为你,建议点赞收藏~❤️
- 牛客习题总结38(7月13日)
- 关于微软虚拟机更新后密码问题
- 将U盘的图标改成自己喜欢的图案
- 并行算法设计与性能优化 刘文志 第2章 现代处理器特性
热门文章
- sysctl mysql_服务器优化——Sysctl、Apache、MySQL
- 用函数求10个数的平均值_Excel AVERAGEIF函数条件求平均值
- jstat 内存泄漏_一次Java内存泄漏的排查!要了自己的老命!
- try...catch的方式处理多个异常
- DBCP使用BasicdataSource连接(两种单例模式-----饿汉和懒汉模式)
- gomarket服务器位置,ANZHI安智市场 Gomarket
- java path设置错误_linux下环境变量PATH设置错误的补救
- mysql case break_按月转移日志表中日志时,mysql总是报‘MySQL server has gone away’这样的错!...
- 三角形周长最短问题_谈“最短”
- mysql能安装的版本下载失败_mysql 安装失败 每次都安装失败 每个版本都失败