Uvalive 6187 never Wait for Weights take right and check set

Source: Internet
Author: User

Test Instructions : The difference in size between each of the two numbers is given at a time. In the process of giving the relationship, insert the query: The difference between number A and B, if not, the output unknown
solution : The relative size of the relationship between the processing: and check Set
1. Given the relative size relationship of two points, find the root node of the two points and use path compression to point the two point father to the root node. The root node is then merged and the relative size relationship between the root nodes is given
2. When asked, at the same time find the point to the root node distance, subtract to get the relative size.

//Meek#include <iostream>#include<cstdio>#include<cmath>#include<string>#include<cstring>#include<algorithm>#include<queue>#include<map>#include<Set>#include<stack>#include<sstream>#include<vector>using namespacestd; typedefLong Longll;#defineMem (a) memset (A,0,sizeof (a))#definePB Push_back#defineFi first#defineSe Second#defineMP Make_pairinline ll read () {ll x=0, f=1; CharCh=GetChar ();  while(ch<'0'|| Ch>'9')    {        if(ch=='-') f=-1; CH=GetChar (); }     while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; CH=GetChar (); }    returnx*F;}//****************************************Const intn=500000+ -;Constll inf = 1ll<< A;Const intMod=1000000007;Charch[ -];intd[n],parent[n],flag,n;ll ans;voidinit () { for(intI=0; i<=n;i++) parent[i]=i; Mem (d);}intFindsintx) {   if(X==parent[x])returnx; intfy=finds (parent[x]); D[X]= D[parent[x]] +D[x]; returnparent[x]=finds (Parent[x]);}intMain () {inta,b,m,w;  while(SCANF ("%d%d", &n,&m)! =EOF) {        if(n==0&&m==0) Break;        Init ();  for(intI=1; i<=m;i++) {scanf ("%s", CH); if(ch[0]=='!') {scanf ("%d%d%d",&a,&b,&W); intfx=finds (a); intfy=finds (b); if(fx!=fy) parent[fy]=fx,d[fy]=d[a]-w-D[b]; }            Else{scanf ("%d%d",&a,&b); intfx=finds (a); intfy=finds (b); if(fx==FY) {cout<<d[a]-d[b]<<Endl; }                Else{printf ("unknown\n"); }            }        }    }    return 0;}
Code

Uvalive 6187 never Wait for Weights 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.