bzoj1016题解
【解题思路】
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题解的更多相关文章
- BZOJ1016:[JSOI2008]最小生成树计数——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=1016 现在给出了一个简单无向加权图.你不满足于求出这个图的最小生成树,而希望知道这个图中有多少个不 ...
- 【bzoj1016】 JSOI2008—最小生成树计数
http://www.lydsy.com/JudgeOnline/problem.php?id=1016 (题目链接) 题意 求图的最小生成树计数. Solution %了下题解,发现要写矩阵树,15 ...
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- noip2016十连测题解
以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...
- BZOJ-2561-最小生成树 题解(最小割)
2561: 最小生成树(题解) Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1628 Solved: 786 传送门:http://www.lyd ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解
题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...
- 2016ACM青岛区域赛题解
A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- poj1399 hoj1037 Direct Visibility 题解 (宽搜)
http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...
随机推荐
- 写MySQL存储过程实现动态执行SQL
转自:http://www.cnblogs.com/william-lin/archive/2013/03/19/2968727.html--存储过程名和参数,参数中in表示传入参数,out标示传出参 ...
- java笔试手写算法面试题大全含答案
1.统计一篇英文文章单词个数.public class WordCounting {public static void main(String[] args) {try(FileReader fr ...
- Eclipse快速生成一个JavaBean类的方法
原文: https://jingyan.baidu.com/article/948f5924156866d80ff5f921.html Eclipse快速生成一个JavaBean类的方法 听语音 | ...
- MySQL - 两种存储引擎 (MyISAM PK InnoDB) + MVCC
总结 1.两种存储引擎各有各的有点,MyISAM专注性能,InnoDB专注事务.两者最大的区别就是InnoDB支持事务,和行锁. 2.InnoDB采用MVCC(Multi-Version Concur ...
- synchronized(this) 与 synchronized(class) 理解
1.概念 synchronized 是 Java 中的关键字,是利用锁的机制来实现同步的. 锁机制有如下两种特性: 互斥性:即在同一时间只允许一个线程持有某个对象锁,通过这种特性来实现多线程中的协调机 ...
- 埃氏筛+线段树——cf731F
从2e5-1依次枚举每个数作为主显卡,然后分段求比它大的数的个数,这里的复杂度是调和级数ln2e5,即埃氏筛的复杂度.. #include<bits/stdc++.h> using nam ...
- js类数组转数组的方法(ArrayLike)
1. 什么是类数组ArrayLike(类数组 就是一个普通的 js对象) 类数组对象必须含有 length 属性,且元素属性名必须是数值或者可转换为数值的字符. 类数组对象不是数组对象,所以没有数 ...
- ORM-Dapper:Dapper列表
ylbtech-ORM-Dapper:Dapper列表 1.返回顶部 1.1. https://dapper-tutorial.net/ 1.2. 2.1. https://www.nuget.org ...
- 19、Page Object 实例
项目目录介绍: CalcuatorPage.java文件代码: package example; import io.appium.java_client.android.AndroidDriver; ...
- POJ 3130 How I Mathematician Wonder What You Are! (半平面交)
题目链接:POJ 3130 Problem Description After counting so many stars in the sky in his childhood, Isaac, n ...