|
|
back to boardWhy wa #3? program ural1211; const maxn=25000; var next:array[1..maxn]of word; t,i:word; procedure test; var sign:array[1..maxn]of word; n,i,x:word; zeros:byte; begin read(n);zeros:=0; for i:=1 to n do begin read(next[i]); if next[i]=0 then begin inc(zeros); if zeros>1 then break; end; end; if zeros<>1 then begin writeln('NO'); exit; end; fillchar(sign,sizeof(sign),0); for i:=1 to n do begin x:=i; while x>0 do begin if sign[x]=i then begin writeln('NO'); exit; end else if sign[x]>0 then break; sign[x]:=i;x:=next[x]; end; end; writeln('YES'); end; begin read(t); for i:=1 to t do test; end. Re: Why wa #3? I too get WA#3 to this one, here is my C solution: #include<stdio.h> #define true 1 #define false 0 main() { int parent[25001] , T , temp , temp2 , temp3 , test; int N , answer , cur; int confess , resolved; scanf("%d" , &T); for(test = 1 ; test <= T ; test++) { scanf("%d" , &N); confess = resolved = false; for(temp = 1 ; temp <= N ; temp++) parent[temp] = -1; for(temp = 1 ; temp <= N && resolved == false ; temp++) { scanf("%d" , &answer); if(answer == 0) { if(confess) { printf("NO\n"); resolved = true; } else confess = true; } else { cur = temp; while(cur != -1 && cur != answer) cur = parent[cur]; if(cur == answer){ printf("NO\n") ; resolved = true; } else parent[answer] = temp; } } if(resolved == false) printf("YES\n"); for( ; temp <= N ; temp++) scanf("%d" , &answer); } } Edited by author 24.05.2005 19:04 |
|
|