【解题思路】

  Kruskal的拓展。

  可以先对边排序,进行一次Kruskal,判断是否可行,并计算出每种权值的边需要多少条。

  然后暴力统计每种权值可行的方案数,根据乘法原理乘起来即可。复杂度o(210mlog2(m+α(n)))。

【参考代码】

 #pragma GCC optimize(2)
#include <algorithm>
#define REP(i,low,high) for(register int i=(low);i<=(high);++i)
using namespace std; //quick_io {
#include <cctype>
#include <cstdio>
inline long long getint()
{
char ch=getchar(); for(;!isdigit(ch)&&ch!='+'&&ch!='-';ch=getchar());
short sig=; for(;ch=='+'||ch=='-';ch=getchar()) if(ch=='-') sig*=-;
long long ret=; for(;isdigit(ch);ch=getchar()) ret=(ret<<)+(ret<<)+ch-'';
return sig*ret;
}
//} quick_io //find_union_set {
#include <cstring>
class find_union_set
{
private:int fat[],stk[];
int find(const int&element)
{
int ancestor=element,top=;
for(;ancestor!=fat[ancestor];ancestor=fat[ancestor]) stk[top++]=ancestor;
for(;top--;fat[stk[top]]=ancestor); return ancestor;
}
public:
find_union_set() {REP(i,,) fat[i]=i;} void clear() {REP(i,,) fat[i]=i;}
find_union_set&operator=(const find_union_set&thr)
{
return memcpy(fat,thr.fat,sizeof thr.fat),*this;
}
bool same(const int&one,const int&thr) {return find(one)==find(thr);}
bool unite(const int&one,const int&thr)
{
return same(one,thr)?:(fat[fat[one]]=fat[thr],);
}
};
//} find_union_set struct edge
{
int fr,to,vl;
void input() {fr=getint(),to=getint(),vl=getint();}
bool operator<(const edge&thr)const{return vl<thr.vl;}
}edg[]; static const int AwD=; int cnt[]={}; find_union_set now,tmp,can;
int main()
{
int n=getint(),m=getint(); REP(i,,m) edg[i].input(); sort(edg+,edg+m+);
int tot=,ans=; REP(i,,m)
{
tot+=edg[i].vl!=edg[i-].vl;
if(!now.same(edg[i].fr,edg[i].to)) now.unite(edg[i].fr,edg[i].to),++cnt[tot];
}
REP(i,,n) if(!now.same(,i)) return puts(""),; now.clear();
for(register int i=,j=,tot=;i<=m;i=++j,++tot,now=can)
{
for(;j<=m&&edg[j].vl==edg[i].vl;++j); --j; int count=;
for(register int status=<<j-i+;status--;)
if(__builtin_popcount(status)==cnt[tot])
{
tmp=now; bool flag=; REP(k,i,j) if(status&(<<k-i))
{
if(flag=!tmp.unite(edg[k].fr,edg[k].to)) break;
}
if(!flag) {can=tmp; if(++count==AwD) count=;}
}
(ans*=count)%=AwD;
}
return printf("%d\n",ans),;
}

bzoj1016题解的更多相关文章

  1. BZOJ1016:[JSOI2008]最小生成树计数——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=1016 现在给出了一个简单无向加权图.你不满足于求出这个图的最小生成树,而希望知道这个图中有多少个不 ...

  2. 【bzoj1016】 JSOI2008—最小生成树计数

    http://www.lydsy.com/JudgeOnline/problem.php?id=1016 (题目链接) 题意 求图的最小生成树计数. Solution %了下题解,发现要写矩阵树,15 ...

  3. 2016 华南师大ACM校赛 SCNUCPC 非官方题解

    我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...

  4. noip2016十连测题解

    以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...

  5. BZOJ-2561-最小生成树 题解(最小割)

    2561: 最小生成树(题解) Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1628  Solved: 786 传送门:http://www.lyd ...

  6. Codeforces Round #353 (Div. 2) ABCDE 题解 python

    Problems     # Name     A Infinite Sequence standard input/output 1 s, 256 MB    x3509 B Restoring P ...

  7. 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解

    题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...

  8. 2016ACM青岛区域赛题解

    A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

  9. poj1399 hoj1037 Direct Visibility 题解 (宽搜)

    http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...

随机推荐

  1. 【sql】leetcode习题 (共 42 题)

    [175]Combine Two Tables (2018年11月23日,开始集中review基础) Table: Person +-------------+---------+ | Column ...

  2. vue组件参数校验与非props特性

    组件参数校验 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <ti ...

  3. SpringBoot集成Swagger(Swagger的使用),生成接口文档,方便前后端分离开发

    首先上一张成果图.  1.Maven依赖 <dependency> <groupId>io.springfox</groupId> <artifactId&g ...

  4. leetcode-160周赛-5240-串联字符串的最大长度

    题目描述: 自己的提交:O(2**n∗n∗m),m 为字符串长度 class Solution: def maxLength(self, arr: List[str]) -> int: from ...

  5. 管理员技术(六): 硬盘分区及格式化、 新建一个逻辑卷、调整现有磁盘的分区、扩展逻辑卷的大小、添加一个swap分区

    一.硬盘分区及格式化 问题: 本例要求熟悉硬盘分区结构,使用fdisk分区工具在磁盘 /dev/vdb 上按以下要求建立分区: 1> 采用默认的 msdos 分区模式        2> ...

  6. WdatePicker.js的使用方法 帮助文档 (日历控件)

    WdatePicker配置和功能 一.配置 日期范围限制 静态限制 注意:日期格式必须与 realDateFmt 和 realTimeFmt 一致 你可以给通过配置minDate(最小日期),maxD ...

  7. 项目管理模式:外瀑布内敏捷(有人称为“信封法”)--转自知乎大神:CORNERSTONE

    作者:CORNERSTONE 链接:https://www.zhihu.com/question/265968122/answer/878124580 来源:知乎 著作权归作者所有.商业转载请联系作者 ...

  8. 6-23 EDM的报告

    EDM营销(Email Direct Marketing)也即:Email营销. 目的:数据分析.制定一对一的个性化数据.提高用户访问率.EDM是一对一的沟通,让你的用户感觉到尊重, 方式:选择强有力 ...

  9. 使用poi实现生成excel文件

    import java.util.ArrayList; import javax.servlet.ServletOutputStream; import org.apache.poi.hssf.use ...

  10. 编码(RZ NRZ NRZI)

    Frm: https://jingyan.baidu.com/album/39810a23addccbb637fda66a.html?picindex=1 RZ 编码(Return-to-zero C ...