HDU 3047 Zjnu Stadium 带权并查集[通俗易懂]

HDU 3047 Zjnu Stadium 带权并查集

大家好,又见面了,我是全栈君。

题目来源:#include <cstdio> #include <cstring> using namespace std; const int maxn = 50010; int f[maxn], flag[maxn], sum[maxn]; int cnt; void init(int n) { for(int i = 1; i <= n; i++) f[i] = i; //memset(rank, 0, sizeof(rank)); memset(sum, 0, sizeof(sum)); } int find(int x) { if(x != f[x]) { int rt = find(f[x]); sum[x] += sum[f[x]]; f[x] = rt; return rt; } return f[x]; } void merge(int i, int j) { int x = find(i); int y = find(j); if(x != y) { f[y] = x; } } int main() { int n, m; while(scanf("%d %d", &n, &m) != EOF) { init(n); int ans = 0; while(m--) { int w, u, v; scanf("%d %d %d", &u, &v, &w); int x = find(u); int y = find(v); if(x != y) { f[y] = x; sum[y] = sum[u] - sum[v] + w; } else { int sum1 = sum[u]; int sum2 = sum[v]; if(sum2-sum1 != w) ans++; } } printf("%d\n", ans); } return 0; }

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115472.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 微信小程序轮播图实现详解

    微信小程序轮播图实现详解微信小程序轮播图实现详解先上效果:wxml代码:<swiperautoplayinterval=”2000″circularindicator-color=”#0094ff”indicator-dotsindicator-active-color=”#ff0094″><!–swiper默认高度已经写死,无法改变、无法由内容撑开–><swiper-item><imagemode=”widthFix”src=”../../16151702

    2022年5月21日
    33
  • stemwin实战篇_赖世雄入门篇

    stemwin实战篇_赖世雄入门篇特别说明:原创教程,未经许可禁止转载,教程采用回复可见的形式,谢谢大家的支持。armfly-x2,x3,v2,v3,v5开发板裸机和带系统的emWin工程已经全部建立,链接如下:http://bbs.

    2022年8月4日
    5
  • datax(16):源码解读AbstractReporter

    datax(16):源码解读AbstractReporter之前介绍了通信类communication,容器类container,communication的收集类collector,今天再看一个报告类Reporter;一、概述AbstractReporter主要作用:Reporter的主要功能是将收集到的信息上报给上级主要方法:reportTGCommunication(汇报TG的communication信息给上级),reportJobCommunication(汇报job的communication信息给上级)二、族谱族谱里面目前只有父子.

    2022年5月16日
    43
  • AVAudioEngine录音崩溃, reason: ‘format.sampleRate == hwFormat.sampleRate

    AVAudioEngine录音崩溃, reason: ‘format.sampleRate == hwFormat.sampleRateAVAudioEngine录音频时偶发崩溃报错信息大致如下:2021-12-1520:12:38.429028+0800*[1659:708511]NSURLConnectionfinishedwitherror-code-1002″AudioRecorder创建Audio缓存文件夹成功/var/mobile/Containers/Data/Application//Library/Caches/Audio”2021-12-1520:13:30.762736+0800***

    2022年10月17日
    3
  • AVI视频文件编码格式不受支持0xc00d5212怎么解决?「建议收藏」

    AVI视频文件编码格式不受支持0xc00d5212怎么解决?「建议收藏」AVI视频格式的优点是图像质量好,但最普遍的现象就是高版本Windows媒体播放器播放不了采用早期编码编辑的AVI格式视频,而低版本Windows媒体播放器又播放不了采用最新编码编辑的AVI格式视频。所以我们在进行一些AVI格式的视频播放时,常会出现由于视频编码问题而造成的视频不能播放或即使能够播放,但存在不能调节播放进度和播放时只有声音没有图像等一些莫名其妙的问题。今天来讲讲编码格式不受…

    2022年9月30日
    4
  • avalondock 翻译网站文章(一)「建议收藏」

    avalondock 翻译网站文章(一)「建议收藏」AvalonDock2.0gettingstartedguidePART1AvalonDock2.0allowstocreateaninterfaceforWPFverysimilartoVisualStudioGUI.It’simportanttounderstandthathasbeendevelopedwiththisinmind

    2022年7月20日
    19

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号