bzoj 1016 深搜
首先我们知道MST的一些性质,对于这道题来说就是,假设我们先求出一颗MST设为G,由已知边权相同的边最多会有10条,那么假设我们在这10条边中选取size条边∈G,那么我们在这边权相同的边集E中任意选取size条有意义的边,这里的有意义的边的定义为每条边都会造成新的连通性的增加,那么边集E中所有的size条有意义的边的方案我们可以通过dfs求出,然后我们将不同边权的边的方案求连乘,就是MST的方案数。
ps:我们没有必要求一遍MST,我们可以一边做kruskal,一边维护图的连通性,然后每找到一个权值不同的边集E时深搜。
反思:做dfs的时候使用并查集维护图的连通性,但是加了路径压缩,这样就会在找块的祖先的时候改变不同节点的父亲,这样就没有办法在dfs时恢复原图,所以我们应该只用并查集维护节点的父亲而不是祖先,找了半天才找到这里的错误。
/**************************************************************
Problem: 1016
User: BLADEVIL
Language: C++
Result: Accepted
Time:8 ms
Memory:820 kb
****************************************************************/ //By BLADEVIL
#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 110
#define maxm 1010
#define d39 31011 using namespace std; int n,m;
int father[maxn],curfather[maxn];
struct rec
{
int a,b,len;
} c[maxm]; bool cmp(rec a,rec b)
{return (a.len<b.len);} int getfather(int x)
{
if (father[x]==x) return x;
return father[x]=getfather(father[x]);
} int getcurfather(int x)
{
if (curfather[x]==x) return x;
return getcurfather(curfather[x]);
} int dfs(int l,int r,int size)
{
//printf("%d %d %d\n",l,r,size);
if (!size) return ;
if (l>r) return ;
int fa,fb,cur=;
cur=dfs(l+,r,size);
fa=getcurfather(c[l].a); fb=getcurfather(c[l].b);
if (fa!=fb)
{
curfather[fa]=fb;
cur+=dfs(l+,r,size-);
curfather[fa]=fa;
}
return cur;
} int main()
{
int ans=,cnt=;
scanf("%d%d",&n,&m);
for (int i=;i<=m;i++) scanf("%d%d%d",&c[i].a,&c[i].b,&c[i].len);
sort(c+,c+m+,cmp);
for (int i=;i<=n;i++) father[i]=curfather[i]=i;
int cur=;
int l,r,size=;
for (int i=;i<=m+;i++)
{
if (cur!=c[i].len)
{
r=i-;
//if (i!=1) printf(" %d %d %d\n",l,r,size);
if (i!=) ans=(ans*=dfs(l,r,size))%d39;
l=i;
size=;
cur=c[i].len;
memcpy(curfather,father,sizeof curfather);
}
int fa,fb;
fa=getfather(c[i].a); fb=getfather(c[i].b);
if (fa!=fb)
{
size++;
cnt++;
father[fa]=fb;
}
}
if (cnt!=n-) printf("0\n"); else printf("%d\n",ans);
return ;
}
bzoj 1016 深搜的更多相关文章
- 【BZOJ】1016: [JSOI2008]最小生成树计数 深搜+并查集
最小生成树计数 Description 现在给出了一个简单无向加权图.你不满足于求出这个图的最小生成树,而希望知道这个图中有多少个不同的最小生成树.(如果两颗最小生成树中至少有一条边不同,则这两个最小 ...
- Bzoj 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 深搜,bitset
1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 554 Solved: 346[ ...
- bzoj 1085骑士精神 迭代深搜
题目传送门 题目大意:给出一幅棋盘,问能否复原,中文题面,不做解释. 思路:第一次写迭代深搜的题目,这道题还是挺经典的.这道题的状态很明显的每多搜一层就是多八倍,非常的多,而且又是t组输入,所以必定有 ...
- 小结:A* & IDA* & 迭代深搜
概要: 在dfs中,如果答案的深度很小但是却很宽,而且bfs还不一定好做的情况下,我们就综合bfs的优点,结合dfs的思想,进行有限制的dfs.在这里A*.IDA*和迭代深搜都是对dfs的优化,因此放 ...
- HDU--杭电--1195--Open the Lock--深搜--都用双向广搜,弱爆了,看题了没?语文没过关吧?暴力深搜难道我会害羞?
这个题我看了,都是推荐的神马双向广搜,难道这个深搜你们都木有发现?还是特意留个机会给我装逼? Open the Lock Time Limit: 2000/1000 MS (Java/Others) ...
- 利用深搜和宽搜两种算法解决TreeView控件加载文件的问题。
利用TreeView控件加载文件,必须遍历处所有的文件和文件夹. 深搜算法用到了递归. using System; using System.Collections.Generic; using Sy ...
- 2016弱校联盟十一专场10.3---Similarity of Subtrees(深搜+hash、映射)
题目链接 https://acm.bnu.edu.cn/v3/problem_show.php?pid=52310 problem description Define the depth of a ...
- 2016弱校联盟十一专场10.2---Around the World(深搜+组合数、逆元)
题目链接 https://acm.bnu.edu.cn/v3/problem_show.php?pid=52305 problem description In ICPCCamp, there ar ...
- 2015暑假多校联合---Cake(深搜)
题目链接:HDU 5355 http://acm.split.hdu.edu.cn/showproblem.php?pid=5355 Problem Description There are m s ...
随机推荐
- What to do when Enterprise Manager is not able to connect to the database instance (ORA-28001)
摘自:http://dbtricks.com/?p=34 If you are trying to connect to the Oracle enterprise Manger and you ge ...
- android中的AIDL学习笔记
一.定义 AIDL是用来解决进程间通信的(一般有四种方式:Activity.Service.ContentProvider.Broadcast Receiver),两个进程间无法直接通信,所以要用AI ...
- 【IOI 2002/FJOI2019】任务安排(超级计算机)
题目 \(N\) 个任务排成一个序列在一台机器上等待完成(顺序不得改变),这 \(N\) 个任务被分成若干批,每批包含相邻的若干任务.从时刻 \(0\) 开始,这些任务被分批加工,第 \(i\) 个任 ...
- ES6 语法糖
重新认识ES6中的语法糖:https://segmentfault.com/a/1190000010159725
- Qt 汽车仪表再次编写,Widget,仪表显示,绘制界面
感谢某网友提供UI让我练练手,上目前的效果 还在晚上,代码等后面在贴出来,就是出来显摆一下
- 再见NullPointerException。在Kotlin里null的处理(KAD 19)
作者:Antonio Leiva 时间:Apr 4, 2017 原文链接:https://antonioleiva.com/nullity-kotlin/ 关于Kotlin最重要的部分之一:无效处理, ...
- jmeter之HTTP请求
1.添加一个线程组:Test plan_添加_Threads(users)_线程组(右键操作),如下图: 2.添加一个HTTP请求:线程组_添加_sample_HTTP请求(右键操作),如下图: 3. ...
- devstack环境搭建
1. devstack部署 参考Quick Start,推荐使用ubuntu16.04进行安装 1.1 配置ubuntu国内源 修改/etc/apt/sources.list内容为 deb http: ...
- [leetcode-652-Find Duplicate Subtrees]
Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only ne ...
- [leetcode-640-Solve the Equation]
Solve a given equation and return the value of x in the form of string "x=#value". The equ ...