http://acm.timus.ru/problem.aspx?space=1&num=1627

给一个无向图,问可以有多少生成树

参照     周冬《生成树的计数及其应用》

代码:

import java.io.File;
import java.io.FileNotFoundException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Scanner; class Fraction {
public BigInteger x;
public BigInteger y; public Fraction(int xx, int yy) {
x = BigInteger.valueOf(xx);
y = BigInteger.valueOf(yy);
} static public BigInteger gcd(BigInteger x, BigInteger y) { if (x.mod(y).compareTo(BigInteger.ZERO) == 0) {
return y;
} else {
return gcd(y, x.mod(y));
}
} public Fraction add(Fraction f) {
Fraction ff = new Fraction(0, 1);
ff.y = this.y.multiply(f.y);
ff.x = this.x.multiply(f.y).add(this.y.multiply(f.x));
BigInteger z = Fraction.gcd(ff.x, ff.y);
ff.x = ff.x.divide(z);
ff.y = ff.y.divide(z);
return ff;
} public Fraction subtract(Fraction f) {
Fraction ff = new Fraction(0, 1);
ff.y = this.y.multiply(f.y);
ff.x = this.x.multiply(f.y).subtract(this.y.multiply(f.x));
BigInteger z = Fraction.gcd(ff.x, ff.y);
ff.x = ff.x.divide(z);
ff.y = ff.y.divide(z);
return ff;
} public Fraction multiply(Fraction f) {
Fraction ff = new Fraction(0, 1);
ff.y = this.y.multiply(f.y);
ff.x = this.x.multiply(f.x);
BigInteger z = Fraction.gcd(ff.x, ff.y);
ff.x = ff.x.divide(z);
ff.y = ff.y.divide(z);
return ff;
} public Fraction divide(Fraction f) {
Fraction ff = new Fraction(0, 1);
ff.y = this.y.multiply(f.x);
ff.x = this.x.multiply(f.y);
BigInteger z = Fraction.gcd(ff.x, ff.y);
ff.x = ff.x.divide(z);
ff.y = ff.y.divide(z);
if (ff.y.compareTo(BigInteger.ZERO) < 0) {
ff.y = ff.y.abs();
ff.x = ff.x.multiply(BigInteger.valueOf(-1L));
}
return ff;
} public boolean equals(Fraction f) {
if (this.x.equals(f.x) && this.y .equals(f.y)) {
return true;
}
return false;
} } public class Main { /**
* @param args
* @throws FileNotFoundException
*/
public static int N = 100; public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
Fraction[][] a = new Fraction[N][N]; for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
a[i][j] = new Fraction(0, 1);
}
}
int[][] k = new int[N][N];
int[] X = { 0, 0, -1, 1 };
int[] Y = { 1, -1, 0, 0 }; int n = in.nextInt();
int m = in.nextInt();
int ln = 0;
for (int i = 0; i < n; ++i) {
String s = in.next();
for (int j = 0; j < m; ++j) {
if (s.charAt(j) == '.') {
k[i][j] = (++ln);
}
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (k[i][j] > 0) {
for (int w = 0; w < 4; ++w) {
int x = i + X[w];
int y = j + Y[w];
if (x >= 0 && x < n && y >= 0 && y < m && k[x][y] > 0) {
a[k[i][j]][k[x][y]] = new Fraction(-1, 1);
a[k[i][j]][k[i][j]] = a[k[i][j]][k[i][j]]
.add(new Fraction(1, 1));
}
}
}
}
}
if (ln > 1) {
System.out.println(valueOfMatrix(a, ln - 1));
}else{
System.out.println(1);
}
} public static BigInteger valueOfMatrix(Fraction[][] a, int n) {
// TODO Auto-generated method stub
BigInteger MOD = BigInteger.valueOf(1000000000L);
dfs(a, n);
Fraction v = new Fraction(1, 1);
for (int i = 1; i <= n; ++i) {
v = v.multiply(a[i][i]);
}
return v.x.mod(MOD);
} public static void dfs(Fraction[][] a, int n) {
if (n == 1)
return;
int l = n;
Fraction fractionZORE = new Fraction(0, 1);
while (l >= 1 && a[l][n].equals(fractionZORE)) {
--l;
}
if (l < 1) {
dfs(a, n - 1);
} else {
if (l < n) {
for (int j = 1; j <= n; ++j) {
Fraction z = a[l][j];
a[l][j] = a[n][j];
a[n][j] = z;
}
} for (int i = 1; i < n; ++i) {
if (!a[i][n].equals(fractionZORE)) {
Fraction z = a[i][n].divide(a[n][n]);
for (int j = 1; j <= n; ++j) {
a[i][j] = a[i][j].subtract(a[n][j].multiply(z));
}
}
}
dfs(a, n - 1);
}
} }

  

1627. Join的更多相关文章

  1. KUANGBIN带你飞

    KUANGBIN带你飞 全专题整理 https://www.cnblogs.com/slzk/articles/7402292.html 专题一 简单搜索 POJ 1321 棋盘问题    //201 ...

  2. kuangbin带你飞 生成树专题 : 次小生成树; 最小树形图;生成树计数

    第一个部分 前4题 次小生成树 算法:首先如果生成了最小生成树,那么这些树上的所有的边都进行标记.标记为树边. 接下来进行枚举,枚举任意一条不在MST上的边,如果加入这条边,那么肯定会在这棵树上形成一 ...

  3. [kuangbin带你飞]专题1-23题目清单总结

    [kuangbin带你飞]专题1-23 专题一 简单搜索 POJ 1321 棋盘问题POJ 2251 Dungeon MasterPOJ 3278 Catch That CowPOJ 3279 Fli ...

  4. ACM--[kuangbin带你飞]--专题1-23

    专题一 简单搜索 POJ 1321 棋盘问题POJ 2251 Dungeon MasterPOJ 3278 Catch That CowPOJ 3279 FliptilePOJ 1426 Find T ...

  5. URAL - 1627:Join (生成树计数)

    Join 题目链接:https://vjudge.net/problem/URAL-1627 Description: Businessman Petya recently bought a new ...

  6. SQL Server-聚焦IN VS EXISTS VS JOIN性能分析(十九)

    前言 本节我们开始讲讲这一系列性能比较的终极篇IN VS EXISTS VS JOIN的性能分析,前面系列有人一直在说场景不够,这里我们结合查询索引列.非索引列.查询小表.查询大表来综合分析,简短的内 ...

  7. SQL Server-聚焦NOT IN VS NOT EXISTS VS LEFT JOIN...IS NULL性能分析(十八)

    前言 本节我们来综合比较NOT IN VS NOT EXISTS VS LEFT JOIN...IS NULL的性能,简短的内容,深入的理解,Always to review the basics. ...

  8. Nested Loops join时显示no join predicate原因分析以及解决办法

    本文出处:http://www.cnblogs.com/wy123/p/6238844.html 最近遇到一个存储过程在某些特殊的情况下,效率极其低效, 至于底下到什么程度我现在都没有一个确切的数据, ...

  9. c# Enumerable中Aggregate和Join的使用

    参考页面: http://www.yuanjiaocheng.net/ASPNET-CORE/asp.net-core-environment.html http://www.yuanjiaochen ...

随机推荐

  1. repeater留言板[转]

    做了一个网站,其中的在线留言板块是用Repeater来显示留言的,这样可以用少的代码还实现多的功能,但是不知道怎么分页,要是留言过多就会使页面变的很长,能过查看众多网友的经验,知道用PagedData ...

  2. HTLM5第一天的内容

    网页的本质就是超级文本标记语言:Hyper Text Markup Language 网页的组成:结构(HTML)  样式(CSS)  行为(JavaScript) 万维网联盟:W3C:World W ...

  3. 微信小程序-地图组件

    map 地图. 标记点 标记点用于在地图上显示标记的位置,不能自定义图标和样式 覆盖物 覆盖物用于在地图上显示自定义图标,可自定义图标和样式 地图组件的经纬度必填, 如果不填经纬度则默认值是北京的经纬 ...

  4. myeclipse10 优化设置

    http://it.oyksoft.com/post/5898/ 一.Myeclipse10修改字体MyEclipse10是基于Eclipse3.7内核,但在Eclipse的Preferences-〉 ...

  5. 韩国手机游戏Elf Defense角色场景

    ! [复制链接] CG窝微博 签到天数: 36 天 连续签到: 1 天 [LV.5]常住居民I 22 主题 0 精华 2729 窝币 超级版主 积分 2546 收听TA 发消息 电梯直达 楼主     ...

  6. Mifare系列2-非接触卡标准(转)

    本文转自 文/闫鑫原创转载请注明出处http://blog.csdn.net/yxstars/article/details/380799 根据信号发送和接收方式的不同,ISO/IEC14443-3定 ...

  7. Sprint(第十天11.23)

  8. 团队作业week14

    0 如果你的团队来了一个新队员,有一台全新的机器,你们是否有一个文档,只要设置了相应的权限,她就可以根据文档,从头开始搭建环境, 并成功地把最新.最稳定版本的软件编译出来,并运行必要的单元测试?(在这 ...

  9. font-weight -- 定义字体的粗细

    font-weight -- 定义字体的粗细 取值: normal | bold | bolder | lighter | 100 | 200 | 300 | 400 | 500 | 600 | 70 ...

  10. main函数的详解

    public : 公共的. 权限是最大,在任何情况下都可以访问. 原因: 为了保证让jvm在任何情况下都可以访问到main方法. static: 静态.静态可以让jvm调用main函数的时候更加的方便 ...