Analysis: This problem and HDU3047, all are with the right and check set, after the input and the first input of the number of conflicts
And then actually use and check the set to maintain a tree, small as a large ancestor, and then this tree each node to the root of the path weights are relative to the root node distance
This allows you to maintain distance limits and judge conflicts
#include <cstdio>#include<cstring>#include<queue>#include<Set>#include<map>#include<stack>#include<cstdlib>#include<algorithm>#include<vector>#include<cmath>using namespacestd;Const intn=2e5+5;intN,m,fa[n],sum[n];intFindintx) { if(X==fa[x])returnFa[x]; intt=Fa[x]; FA[X]=find (fa[x]); SUM[X]+=Sum[t]; returnfa[x];}intMain () { while(~SCANF ("%d%d",&n,&m)) { for(intI=0; i<=n;++i) fa[i]=i,sum[i]=0; intans=0; while(m--){ intu,v,w; scanf ("%d%d%d",&u,&v,&W); --u; intX=find (u), y=Find (v); if(x==y) { if(sum[v]-sum[u]!=w) + +ans; } Else if(x>y) {Sum[x]=sum[v]-sum[u]-W; FA[X]=y; }} printf ("%d\n", ans);} return 0;}View Code
HDU 3038 How many Answers is wrong take right and check set