标签:dict ssi rom this eof eve note += however
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3038
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10164 Accepted Submission(s): 3699
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <vector> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 #define ms(a,b) memset((a),(b),sizeof((a))) 13 using namespace std; 14 typedef long long LL; 15 const double EPS = 1e-8; 16 const int INF = 2e9; 17 const LL LNF = 2e18; 18 const int MAXN = 2e5+10; 19 20 int n, m; 21 int fa[MAXN], r[MAXN]; 22 23 int find(int x) 24 { 25 if(fa[x]==-1) return x; 26 int pre = find(fa[x]); 27 r[x] += r[fa[x]]; 28 return fa[x] = pre; 29 } 30 31 bool Union(int u, int v, int w) 32 { 33 int fu = find(u); 34 int fv = find(v); 35 if(fu==fv) 36 return (r[v]-r[u]!=w); 37 38 fa[fv] = fu; 39 r[fv] = -r[v]+w+r[u]; 40 return false; 41 } 42 43 int main() 44 { 45 while(scanf("%d%d", &n, &m)!=EOF) 46 { 47 memset(r, 0, sizeof(r)); 48 memset(fa, -1, sizeof(fa)); 49 50 int ans = 0; 51 for(int i = 1; i<=m; i++) 52 { 53 int u, v, w; 54 scanf("%d%d%d", &u, &v, &w); 55 if(Union(u-1, v, w)) 56 ans++; 57 } 58 printf("%d\n", ans); 59 } 60 }
HDU3038 How Many Answers Are Wrong —— 带权并查集
标签:dict ssi rom this eof eve note += however
原文地址:http://www.cnblogs.com/DOLFAMINGO/p/7652440.html