题意

\(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的更多相关文章

  1. 【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 ...

  2. Minimum Spanning Trees

    Kruskal’s algorithm always union the lightest link if two sets haven't been linked typedef struct { ...

  3. 【HDU 4408】Minimum Spanning Tree(最小生成树计数)

    Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Krusk ...

  4. HDU 4408 Minimum Spanning Tree 最小生成树计数

    Minimum Spanning Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  5. hdu 4408 Minimum Spanning Tree

    Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Krusk ...

  6. 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 ...

  7. [LeetCode] Minimum Height Trees 最小高度树

    For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...

  8. 数据结构与算法分析–Minimum Spanning Tree(最小生成树)

    给定一个无向图,如果他的某个子图中,任意两个顶点都能互相连通并且是一棵树,那么这棵树就叫做生成树(spanning tree). 如果边上有权值,那么使得边权和最小的生成树叫做最小生成树(MST,Mi ...

  9. Minimum Height Trees

    For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...

随机推荐

  1. 使用MATLAB 2019 App Design 工具设计一个 电子日记App

    使用MATLAB 2019 App Design 工具设计一个 电子日记App1.1 前言:由于信号与系统课程需要,因此下载了MATLAB软件,加之对新款的执着追求,通过一些渠道,下载了MATLAB ...

  2. 记一次uwsgi django nginx 调优

    [uwsgi] project = fortune_cat uid = ubuntu gid = ubuntu path = fortune_cat base = /home/%(uid) chdir ...

  3. 日志分析平台ELK之前端展示kibana

    之前的博客一直在聊ELK集群中的存储.日志收集相关的组件的配置,但通常我们给用户使用不应该是一个黑黑的shell界面,通过接口去查询搜索:今天我们来了ELK中的前端可视化组件kibana:kibana ...

  4. C# Dropdownlist设置选择项

    (1)   dropdowslist.selectedIndex=索引值(数字); (2) dropdownlist.Items.findbyvalue(你的值).selected=true   (3 ...

  5. 【小白学PyTorch】21 Keras的API详解(上)卷积、激活、初始化、正则

    [新闻]:机器学习炼丹术的粉丝的人工智能交流群已经建立,目前有目标检测.医学图像.时间序列等多个目标为技术学习的分群和水群唠嗑答疑解惑的总群,欢迎大家加炼丹兄为好友,加入炼丹协会.微信:cyx6450 ...

  6. IDEA推送docker镜像到私服/利用dockerfile-maven-plugin插件在springboot中上传镜像到远程的docker服务器、远程仓库

    利用dockerfile-maven-plugin插件在springboot中上传镜像到远程仓库      这篇文章讲解在开发工具中把打包好的jar编译成docker镜像,上传到远程的docker服务 ...

  7. VUE 安装项目

    注意:在cmd中执行的命令 1,前提是安装了node.js 查看 npm 版本号 2,创建项目路径 mkdir vue cd vue 3,安装vue-cli (脚手架) npm install -个v ...

  8. 多测师讲解selenium_alert弹框定位_高级讲师肖sir

    from selenium import webdriverfrom time import sleepdrvier=webdriver.Chrome()url=r'F:\dcs\DCS课程安排\se ...

  9. vs code 编译python 输出到调试控制台

    如图所示,在debug菜单中点击齿轮按钮,进入launch.json,更改console选项的值(有三种) "console": "internalConsole&quo ...

  10. day11 Pyhton学习

    一.昨日内容回顾 函数的进阶 动态传参 *args : 位置参数的动态传参 **kwargs: 关键字参数的动态传参 形参:聚合 实参:打散 顺序:位置参数>*args >默认值 > ...