HDU 3038 How many Answers is wrong take right and check set

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.