Gym102012A Rikka with Minimum Spanning Trees
题意
\(T\) 组数据,每组数据给定一个 \(n\) 个点,\(m\) 条边,可能含有重边自环的图,求出最小生成树的个数与边权和的乘积,对 \(10^9+7\) 取模。
\(\texttt{Data Range:}T\leq 100,2\leq n\leq 10^5,m=10^5\)
题解
大家好,这题充分展现了我就是个 sb。
一见数据随机,立刻想到相同边权的边很少,立刻想到矩阵大小很小,立刻想到最小生成树计数,立刻想到 Matrix-Tree 定理。某些 Karry5307 的想像惟在这一层能够如此跃进。
直接进入正题,首先不能被题目中给出的最小生成树计数方法给带偏。
注意到边权在 \(0\sim 2^{64}-1\) 范围内随机给定,所以我们有很大的把握认定最小生成树唯一,求出这个生成树的边权和即可。
代码
#include<bits/stdc++.h>
using namespace std;
typedef int ll;
typedef long long int li;
typedef unsigned long long int ull;
const ll MAXN=2e5+51,MOD=1e9+7;
struct EdgeForKruskal{
ll from,to;
ull dist;
inline bool operator <(const EdgeForKruskal &rhs)const
{
return this->dist<rhs.dist;
}
};
EdgeForKruskal ed[MAXN];
ll test,n,m,x,y;
ull z;
ll ffa[MAXN];
inline ll read()
{
register ll num=0,neg=1;
register char ch=getchar();
while(!isdigit(ch)&&ch!='-')
{
ch=getchar();
}
if(ch=='-')
{
neg=-1;
ch=getchar();
}
while(isdigit(ch))
{
num=(num<<3)+(num<<1)+(ch-'0');
ch=getchar();
}
return num*neg;
}
inline ll find(ll x)
{
return x==ffa[x]?x:ffa[x]=find(ffa[x]);
}
inline void merge(ll x,ll y)
{
ll fx=find(x),fy=find(y);
fx!=fy?ffa[fy]=fx:1;
}
inline ll Kruskal()
{
ll tott=0,res=0;
for(register int i=1;i<=m;i++)
{
if(find(ed[i].from)!=find(ed[i].to))
{
merge(ed[i].from,ed[i].to),res=(res+ed[i].dist%MOD)%MOD;
if(++tott==n-1)
{
break;
}
}
}
return tott==n-1?res:0;
}
namespace Maker{
ull k1,k2;
inline ull gen()
{
ull k3=k1,k4=k2;
k1=k4,k3^=k3<<23,k2=k3^k4^(k3>>17)^(k4>>26);
return k2+k4;
}
}
using namespace Maker;
inline void solve()
{
n=read(),m=read(),scanf("%llu%llu",&k1,&k2);
for(register int i=1;i<=n;i++)
{
ffa[i]=i;
}
for(register int i=1;i<=m;i++)
{
x=gen()%n+1,y=gen()%n+1,z=gen(),ed[i]=(EdgeForKruskal){x,y,z};
}
sort(ed+1,ed+m+1),printf("%d\n",Kruskal());
}
int main()
{
test=read();
for(register int i=0;i<test;i++)
{
solve();
}
}
Gym102012A Rikka with Minimum Spanning Trees的更多相关文章
- 【2018 ICPC亚洲区域赛徐州站 A】Rikka with Minimum Spanning Trees(求最小生成树个数与总权值的乘积)
Hello everyone! I am your old friend Rikka. Welcome to Xuzhou. This is the first problem, which is a ...
- Minimum Spanning Trees
Kruskal’s algorithm always union the lightest link if two sets haven't been linked typedef struct { ...
- 【HDU 4408】Minimum Spanning Tree(最小生成树计数)
Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Krusk ...
- HDU 4408 Minimum Spanning Tree 最小生成树计数
Minimum Spanning Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- hdu 4408 Minimum Spanning Tree
Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Krusk ...
- MST(Kruskal’s Minimum Spanning Tree Algorithm)
You may refer to the main idea of MST in graph theory. http://en.wikipedia.org/wiki/Minimum_spanning ...
- [LeetCode] Minimum Height Trees 最小高度树
For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...
- 数据结构与算法分析–Minimum Spanning Tree(最小生成树)
给定一个无向图,如果他的某个子图中,任意两个顶点都能互相连通并且是一棵树,那么这棵树就叫做生成树(spanning tree). 如果边上有权值,那么使得边权和最小的生成树叫做最小生成树(MST,Mi ...
- Minimum Height Trees
For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...
随机推荐
- 微服务实战系列(五)-注册中心Eureka与nacos区别
1. 场景描述 nacos最近用的比较多,介绍下nacos及部署吧,刚看了下以前写过类似的,不过没写如何部署及与eureka区别,只展示了效果,补补吧. 2.解决方案 2.1 nacos与eureka ...
- 探讨JVM运行机制和执行流程
JVM是什么 概述 JVM是Java Virtual Machine的缩写.它是一种基于计算设备的规范,是一台虚拟机,即虚构的计算机. JVM屏蔽了具体操作系统平台的信息(显然,就像是我们在电脑上开了 ...
- (leetcode每日打卡)秋叶收藏集【动态规划】
LCP 19.秋叶收藏集 题目链接 算法 动态规划 时间复杂度O(n) 1.题目要求最终形成[红.黄.红]三部分,每部分数量可以不相等,问最终调整操作数量最小是多少.这道题一开始考虑暴力去做,枚举两个 ...
- P1000 超级玛丽游戏
P1000 超级玛丽游戏 https://www.luogu.com.cn/problem/P1000 这就很简单了代码: #include <iostream> #include < ...
- C 面向对象编程 --- 一模块的串口协议解析
// 任务目的// 解析串口收到的54个字节.这54个字节包含了8个车道的5大信息以及校验信息.// 实现了查询每条车道包含了哪些信息. #include <stdio.h>#includ ...
- Python字符编码和二进制不得不说的故事
二进制 核心思想: 冯诺依曼 + 图灵机 电如何表示状态,才能稳定? 计算机开始设计的时候并不是考虑简单,而是考虑能自动完成任务与结果的可靠性, 简单始终是建立再稳定.可靠基础上 经过尝试10进制,但 ...
- 084 01 Android 零基础入门 02 Java面向对象 01 Java面向对象基础 02 构造方法介绍 03 构造方法-this关键字
084 01 Android 零基础入门 02 Java面向对象 01 Java面向对象基础 02 构造方法介绍 03 构造方法-this关键字 本文知识点:构造方法-this关键字 说明:因为时间紧 ...
- java多线程:线程池原理、阻塞队列
一.线程池定义和使用 jdk 1.5 之后就引入了线程池. 1.1 定义 从上面的空间切换看得出来,线程是稀缺资源,它的创建与销毁是一个相对偏重且耗资源的操作,而Java线程依赖于内核线程,创建线程需 ...
- 3.Android网络编程-http介绍
1.HTTP请求方法 根据HTTP标准,HTTP请求可以使用多种请求方法. HTTP1.0定义了三种请求方法: GET(查), POST(改)和 HEAD(获取报头,一般用来测试链接是否正常)方法. ...
- 【原创】xenomai内核解析--xenomai与普通linux进程之间通讯XDDP(二)--实时与非实时关联(bind流程)
版权声明:本文为本文为博主原创文章,转载请注明出处.如有问题,欢迎指正.博客地址:https://www.cnblogs.com/wsg1100/ 1.概述 上篇文章介绍了实时端socket创建和配置 ...