How Many Answers Are Wrong hdu3038
来源:互联网 发布:编程大师访谈录 编辑:程序博客网 时间:2024/05/21 16:23
rank[x] 表示从x结点到根节点的值。
首先我们可以把问题稍微转化一下,就是如果已知[3,6],[7,10]俩个区间内各自所有数的和,那么就可以[3,10]内所有数的和,可是,这俩个区间根本就不衔接,所有要稍微处理一下,将左区间值减1,就变成了[2,6],[6,10],这样就方便处理了。
具体注释见代码。
/*********************************************** * Author: fisty * Created Time: 2015/2/27 12:57:32 * File Name : D.cpp *********************************************** */#include <iostream>#include <cstring>#include <deque>#include <cmath>#include <queue>#include <stack>#include <list>#include <map>#include <set>#include <string>#include <vector>#include <cstdio>#include <bitset>#include <algorithm>using namespace std;#define Debug(x) cout << #x << " " << x <<endl#define Memset(x, a) memset(x, a, sizeof(x))const int INF = 0x3f3f3f3f;typedef long long LL;typedef pair<int, int> P;#define FOR(i, a, b) for(int i = a;i < b; i++)#define MAX_N 200050int n, m;int par[MAX_N], rank[MAX_N];void init(){ for(int i = 0;i < MAX_N; i++){ par[i] = i; rank[i] = 0; }}int find(int x){ if(x == par[x]) return x; int tmp = par[x]; par[x] = find(par[x]); //当递归到某一层时,x还未接到根节点上, //所以rank[x]表示的是x到par[x]的距离,但par[x]已经接到根节点上了,所以rank[par[x]]表示的是父节点到根节点的距离 //所以x到根节点的距离就直接等于rank[x]+r[par[x]] rank[x] += rank[tmp]; return par[x];}bool unio(int x, int y, int c){ int fx = find(x); int fy = find(y); if(fx == fy){ if(rank[x] + c != rank[y]) return false; return true; } par[fy] = fx; //rank[x] 为 x 到 fx的距离 //rank[y] 为 y 到 fy的距离 //c 为 y 到 x的距离 //所以fy到fx的距离为 c + rank[x] - rank[y] rank[fy] = rank[x] + c - rank[y] ; return true;}int main() { //freopen("in.cpp", "r", stdin); cin.tie(0); ios::sync_with_stdio(false); while(cin >> n >> m){ int cnt = 0; init(); FOR(i, 0, m){ int u, v, c; cin >> u >> v >> c; if(!unio(u-1, v, c)) ++cnt; } cout << cnt << endl; } return 0;}
0 0
- hdu3038 How Many Answers Are Wrong
- HDU3038 How Many Answers Are Wrong
- HDU3038 How Many Answers Are Wrong
- hdu3038--How Many Answers Are Wrong
- How Many Answers Are Wrong hdu3038
- hdu3038 How Many Answers Are Wrong
- 并查集好题 hdu3038 how many answers are wrong
- hdu3038 How Many Answers Are Wrong --- 种类并查集
- Hdu3038 - How Many Answers Are Wrong - 并查集
- HDU3038 How Many Answers Are Wrong 【并查集】
- 并查集 hdu3038 How Many Answers Are Wrong
- hdu3038 How Many Answers Are Wrong--种类并查集
- HDU3038 How Many Answers Are Wrong (加权并查集)
- HDU3038 How Many Answers Are Wrong【巧妙并查集】
- How Many Answers Are Wrong
- How Many Answers Are Wrong
- How Many Answers Are Wrong
- How Many Answers Are Wrong
- LBP
- Xcode LLDB Debug教程
- 逃跑的拉尔夫---题目建议BFS
- Scrum与项目管理亲体验
- [算法系列之二十四]后缀树(Suffix Tree)
- How Many Answers Are Wrong hdu3038
- delete运算符
- 浅谈LLDB调试器
- django的表单用法~~
- NSOperations and NSOperationQueues学习笔记
- STL-ZOJ-2829 Beautiful Number:打表的艺术&&map映照容器
- c#基础(一)----数据类型
- BZOJ 3891 Usaco2014 Dec Piggy Back BFS
- ubuntu13.04下运行doris时,run -s6解缠时出错!!