练习四 1023
来源:互联网 发布:100m阿里云服务器租用 编辑:程序博客网 时间:2024/05/18 02:21
概述:在一个会场举行文艺演出,安排坐区,编号为B的人必须在编号为A顺时针方向X,给你一些规则,找出不正确的,输出个数。
思路:带权值的并查集问题,在该题中可以转化为List[b] = List[x] + m - List[y],基本的find和merge函数和其他的并查集类问题一样。
感想:没想到简单的题在后面。
#include<cstdio> #include<cmath> #include<iostream> #include<fstream> using namespace std;const int N = 60000;int f[N], List[N], n, m;int find(int x) {if (x == f[x]) return f[x];int t = f[x];f[x] = find(f[x]);List[x] += List[t];return f[x];}bool Merge(int x, int y, int m) {int a = find(x), b = find(y);if (a == b) {if (List[x] + m != List[y])return false;return true;}f[b] = a;List[b] = List[x] + m - List[y];return true;}int main() {//ifstream cin("aaa.txt");int a, b, x;while (cin >> n >> m){for (int i = 0; i <= n; ++i)f[i] = i, List[i] = 0;int cnt = 0;for (int i = 0; i<m; ++i){cin >> a >> b >> x;if (!Merge(a, b, x))++cnt;}printf("%d\n", cnt);}return 0;}
0 0
- 练习四 1023
- 练习四
- 数据库练习四
- 查询练习(四)
- Scala练习(四)
- 听课练习(四)
- 笔试练习四
- 笔试练习四
- 练习四1001
- 练习四 1001
- 练习四 1004
- 练习四 1003
- 练习四 1002
- 练习四 1005
- 练习四 1001
- 练习四 1002
- 练习四 1003
- 练习四 1004
- Interleaving String
- Java源代码分析之Vector数组
- CAP理论
- 存储过程定时任务
- OpenGL::让我们画几条线
- 练习四 1023
- 关于listener的一些理解
- 常用算法---动态规划算法
- NSString
- js笔记--事件
- binbinyang---Android studio设置ide和project编码格式
- 1006-F专题四
- Weka-无监督属性过滤器-AddCopyAddid[6-1]
- Android设计模式-适配器模式