bzoj1016
这道题主要利用了最小生成树的两个性质
最小生成树每种边权的数目固定不变
最小生成树每种边权带来的连通状况一定唯一
由于每种边权的只有不到10种,所以直接穷举然后乘法原理即可
const mo=;
type node=record
x,y,w:longint;
end; var a:array[..] of node;
fa,rank,v:array[..] of longint;
sum,ans,k1,k2,s,i,j,n,m,p,k:longint; function getf(x:longint):longint;
begin
if fa[x]<>x then fa[x]:=getf(fa[x]);
exit(fa[x]);
end; procedure swap(var a,b:node);
var c:node;
begin
c:=a;
a:=b;
b:=c;
end; procedure sort(l,r:longint);
var i,j,x,y:longint;
begin
i:=l;
j:=r;
x:=a[(l+r) shr ].w;
repeat
while a[i].w<x do inc(i);
while x<a[j].w do dec(j);
if not(i>j) then
begin
swap(a[i],a[j]);
inc(i);
j:=j-;
end;
until i>j;
if l<j then sort(l,j);
if i<r then sort(i,r);
end; function calc(x:longint):longint;
begin
calc:=;
while x> do
begin
calc:=calc+x mod ;
x:=x shr ;
end;
end; function check(p,cur:longint):longint;
var i,res,tot:longint;
begin
for i:= to n do
fa[i]:=i;
tot:=;
res:=;
for i:=p to j- do
begin
if cur and = then
begin
k1:=getf(a[i].x);
k2:=getf(a[i].y);
if k1<>k2 then
begin
fa[k1]:=k2;
inc(tot);
res:=res+a[i].w;
end;
end;
cur:=cur shr ;
end;
for i:= to m do
begin
if a[i].w=a[j-].w then continue;
k1:=getf(a[i].x);
k2:=getf(a[i].y);
if k1<>k2 then
begin
fa[k1]:=k2;
inc(tot);
res:=res+a[i].w;
end;
end;
if (res=sum) and (tot=n-) then exit() else exit();
end; begin
readln(n,m);
for i:= to m do
readln(a[i].x,a[i].y,a[i].w);
sort(,m);
for i:= to n do
fa[i]:=i;
rank[]:=;
p:=;
for i:= to m do
begin
if a[i].w<>a[i-].w then inc(p);
rank[i]:=p;
end;
i:=;
j:=;
while i<n- do
begin
inc(j);
k1:=getf(a[j].x);
k2:=getf(a[j].y);
if k1<>k2 then
begin
fa[k1]:=k2;
inc(i);
inc(v[rank[j]]);
sum:=sum+a[j].w;
end;
end;
if i<n- then
begin
writeln();
halt;
end;
ans:=;
i:=;
while i<=m do
begin
j:=i+;
while a[i].w=a[j].w do inc(j);
if v[rank[i]]> then
begin
s:=;
for k:= to shl (j-i)- do
if calc(k)=v[rank[i]] then s:=s+check(i,k);
ans:=ans*s mod mo;
end;
i:=j;
end;
writeln(ans);
end.
bzoj1016的更多相关文章
- 【bzoj1016】 JSOI2008—最小生成树计数
http://www.lydsy.com/JudgeOnline/problem.php?id=1016 (题目链接) 题意 求图的最小生成树计数. Solution %了下题解,发现要写矩阵树,15 ...
- 【Matrix-tree定理】【并查集】【kruscal算法】bzoj1016 [JSOI2008]最小生成树计数
题意:求一个图的最小生成树个数. 矩阵树定理:一张无向图的生成树个数 = (度数矩阵 - 邻接矩阵)的任意一个n-1主子式的值. 度数矩阵除了对角线上D[i][i]为i的度数(不计自环)外,其他位置是 ...
- [BZOJ1016][JSOI2008]最小生成树计数(结论题)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1016 分析: 首先有个性质:如果边集E.E'都可以表示一个图G的最小生成树(当然E和E ...
- BZOJ1016 最小生成树计数
Description 现在给出了一个简单无向加权图.你不满足于求出这个图的最小生成树,而希望知道这个图中有多少个不同的最小生成树.(如果两颗最小生成树中至少有一条边不同,则这两个最小生成树就是不同的 ...
- bzoj1016 [JSOI2008]最小生成树计数
1016: [JSOI2008]最小生成树计数 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 3517 Solved: 1396[Submit][St ...
- bzoj1016:[JSOI2008]最小生成树计数
思路:模拟kruskal的过程,可以发现对于所有权值相同的边,有很多种选择的方案,而且权值不同的边并不会相互影响,因为先考虑权值较小的边,权值比当前权值大的边显然不在考虑范围之内,而权值比当前权值小的 ...
- [BZOJ1016] [JSOI2008] 最小生成树计数 (Kruskal)
Description 现在给出了一个简单无向加权图.你不满足于求出这个图的最小生成树,而希望知道这个图中有多少个不同的最小生成树.(如果两颗最小生成树中至少有一条边不同,则这两个最小生成树就是不同的 ...
- 【BZOJ1016】【JSOI2008】最小生成树计数
Description 现在给出了一个简单无向加权图.你不满足于求出这个图的最小生成树,而希望知道这个图中有多少个不同的最小生成树.(如果两颗最小生成树中至少有一条边不同,则这两个最小生成树就是不同的 ...
- 【最小生成树】BZOJ1016: [JSOI2008]最小生成树计数
Description 现在给出了一个简单无向加权图.你不满足于求出这个图的最小生成树,而希望知道这个图中有多少个不同的最小生成树.(如果两颗最小生成树中至少有一条边不同,则这两个最小生成树就是不同的 ...
随机推荐
- Java——类比较器
1.Product类 public class Product { private int pid; private String name; private double price; public ...
- 关于iOS上的静态库
最近再进行项目的真机调试,然后发现了一个天坑.就此研究了一些iOS上的静态库的使用: 首先我们是直接拿一个可以运行的项目来制作静态库的,网上大部分都是先创建静态库然后再写内容,看看我的方法. 1.把子 ...
- js控制文本框输入数字和小数点等
1.文本框只能输入数字代码(小数点也不能输入) <input onkeyup="this.value=this.value.replace(/\D/g,'')" onafte ...
- SpringMVC的文件上传
首先导入jar包 在springMVC里面配置文件上传,以及限定上传文件的大小 <bean id="multipartResolver" class="org.s ...
- Codevs 3729 飞扬的小鸟
飞扬的小鸟 标签 动态规划 NOIp提高组 2014 难度 提高+/省选- 题目描述 Flappy Bird 是一款风靡一时的休闲手机游戏.玩家需要不断控制点击手机屏幕的频率来调节小鸟的飞行高度,让小 ...
- bzoj1004:[HNOI2008]Cards
思路:由于题目给出了置换,又要求本质不同的方案数,考虑使用Burnside引理,Burnside引理即通过所有置换和原来相同的方案数之和除以方案数总数,而对于某一个置换要使置换后得到的与原来的相同,就 ...
- IF EXIST: The syntax of the command is incorrect.
Batch 脚本中使用 IF EXIST 语句时,总是提示 The syntax of the command is incorrect. 原始 bat 脚本如下: ECHO OFF SET proj ...
- freemaker小练习
public class TestFreemaker extends HttpServlet{ // 负责管理FreeMarker模板的Configuration实例 private ...
- Mysql DOC阅读笔记
Mysql DOC阅读笔记 转自我的Github Speed of SELECT Statements 合理利用索引 隔离调试查询中花费高的部分,例如函数调用是在结果集中的行执行还是全表中的行执行 最 ...
- xp 下卸载 硬盘安装的 ubuntu (本人的悲伤史)
正常启动XP系统,到http://www.sysint.no 下载 MBRFIX.zip,解压,把文件放在C盘, 点击“开始”==“运行”==“cmd”,出现下面图 输入cd\,如下图, 再按回车键, ...