[BZOJ1202] [HNOI2005] cunning businessman (and check set + prefix and)

Source: Internet
Author: User

Title Description

Transmission Door

Exercises

Just beginning to feel that only a few intervals and a few other intervals to spell the same interval can be sentenced to false, and then Xjblg wrote a No to the beat of the code and then handed in a = = Visible data is weak.
The positive solution on the net is and check the set, yy for quite a long time.
Read-in Interval (l,r,w), if L and R are not in a set, merge them, otherwise determine whether Dis_r-dis_x equals W (where dis_i represents I to the distance of its ancestors)

Code

Xjblg

#include <algorithm>#include <iostream>#include <cstring>#include <cstdio>using namespace STD;Const intmax_n= the;Const intmax_m=1005;intT,n,m;BOOLFlag,vis[max_n][max_n];intF[max_n][max_n];structhp{intX,y,w;} EDGE[MAX_M];inline voidClear () {memset(Vis,0,sizeof(VIS));memset(F,0,sizeof(f));inline intCMP (HP a,hp b) {returna.x<b.x| | (A.X==B.X&AMP;&AMP;A.Y&LT;B.Y);}intMain () {scanf("%d", &t); while(t--) {Clear ();scanf("%d%d", &n,&m); for(intI=1; i<=m;++i)scanf("%d%d%d", &AMP;EDGE[I].X,&AMP;EDGE[I].Y,&AMP;EDGE[I].W); Sort (edge+1, edge+m+1, CMP); flag=true; for(intI=1; i<=m;++i) {if(!vis[edge[i].x][edge[i].y]) {vis[edge[i].x][edge[i].y]=true;            F[EDGE[I].X][EDGE[I].Y]=EDGE[I].W; }Else                if(F[EDGE[I].X][EDGE[I].Y]!=EDGE[I].W) {flag=false; Break; } for(intj=edge[i].x-1; j>=1;--j) {if(vis[j][edge[i].x-1])                {if(!vis[j][edge[i].y]) {vis[j][edge[i].y]=true; f[j][edge[i].y]=f[j][edge[i].x-1]+F[EDGE[I].X][EDGE[I].Y]; }Else                        if(f[j][edge[i].y]!=f[j][edge[i].x-1]+F[EDGE[I].X][EDGE[I].Y]) {flag=false; Break; }                }            }if(!flag) Break; }if(flag)printf("true\n");Else printf("false\n"); }}

and check the set + prefix and

#include <iostream>#include <cstring>#include <cstdio>using namespace STD;Const intmax_n= the;intT,N,M,L,R,W,FL,FR;BOOLFlagintF[max_n],dis[max_n];inline intFindintx) {if(X!=f[x]) {intT=find (F[x]);        DIS[X]+=DIS[F[X]];    f[x]=t; }returnF[X];}intMain () {scanf("%d", &t); while(t--) {scanf("%d%d", &n,&m); for(intI=0; i<=n;++i) f[i]=i,dis[i]=0; flag=true; for(intI=1; i<=m;++i) {scanf("%d%d%d", &l,&r,&w);            l--; Fl=find (l); Fr=find (R);if(FL!=FR)                {dis[fr]=dis[l]-dis[r]+w;            F[FR]=FL; }Else                if(dis[r]-dis[l]!=w) flag=false; }if(flag)printf("true\n");Else printf("false\n"); }}

[BZOJ1202] [HNOI2005] cunning businessman (and check set + prefix and)

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.