Test Instructions:There is no link
Method:and search set + discretization
parsing:Guo Sai this way to popularize the difficulty of the problem I am embarrassed to write the problem, the test instructions is very clear, one eye problem. In fact, all the requirements of a sort, put xi=xj conditions in front, so that the conditions of XI!=XJ put on the back of the line. And then we have all the i,j discretized for n queries, and here I'm slacking off with a map and just add all the XI=XJ parts to the set. XI!=XJ parts look at the two ancestors are not the same, the same as the next, the same output no;
Code:This problem sticker code is really embarrassed--
#include <map>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define N 100100using namespace STD;intTotintTintNintfa[n<<1]; map<int,int>MstructNode {intX, Y, Z }a[n];intFindintx) {if(fa[x]==x)returnXElseFa[x]=find (Fa[x]);returnFA[X]; }intCMP (node A,node b) {returnA.z>b.z;}intMain () {scanf("%d", &t); while(t--) {tot=0; M.clear ();scanf("%d", &n); for(intI=1; i<=n;i++) {scanf("%d%d%d", &a[i].x,&a[i].y,&a[i].z); } for(intI=1; i<=n;i++) {if(!m[a[i].x]) M[a[i].x]=++tot;if(!m[a[i].y]) M[a[i].y]=++tot; } for(intI=1; i<=tot;i++) fa[i]=i;intflag=0; Sort (A +1, a+n+1, CMP); for(intI=1; i<=n;i++) {intX=M[A[I].X],Y=M[A[I].Y];intZ=a[i].z;if(z==1) {intFx=find (x), Fy=find (y);if(FX!=FY) {fa[fx]=fy; } }Else{intFx=find (x), Fy=find (y);if(FX==FY) {printf("no\n"); flag=1; Break;} } }if(!flag)printf("yes\n"); } }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
NOI DAY1 T1 Program automatic analysis and search set + discretization