ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1211. Collective Guarantee

HELPPPPPPPP. TL PLEASE
Posted by I am david. Tabo. 28 Oct 2002 14:43
var ni,k,i,j,max,m,n:longint;
    a,b:array [1..25001] of integer;
    q:array [1..1001] of byte;
    s:boolean;

begin
  readln (k);
  for j:=1 to k do
    begin
      readln (n);
      for i:=1 to n do
        begin
          if i<>n then
            read (a[i])
          else
            readln (a[i]);
          if a[i]<>0 then
            inc (b[a[i]]);
        end;
      max:=b[1];
      for i:=2 to n do
        if max<b[i] then
          max:=b[i];
      for i:=1 to n do
        if (b[i]=max) then
          inc (m);
      ni:=0;
      if m=1 then
        begin
          q[j]:=1;
          s:=true;
        end
      else
        for i:=1 to n do
          if (a[i]=0)and(b[i]<>0) then
            begin
              q[j]:=1;
              if ni=1 then
                begin
                  q[j]:=0;
                  break;
                end;
              inc (ni);
              s:=true;
            end;
      if not s then
        q[j]:=0;
      s:=false;
      fillchar(a,sizeof(a),0);
      fillchar(b,sizeof(b),0);
    end;
  for i:=1 to k do
    if q[i]=1 then
      writeln ('YES')
    else
      writeln ('NO');
end.
Re: HELPPPPPPPP. TL PLEASE
Posted by Илья Гофман (Ilya Gofman) 29 Oct 2002 22:19
You need to use the O(n) algo. Here is my AC solution.
It checks if the graph is the tree. If is then YES.


program Collective_Guarantee;
const MaxN=25000;
label fin;
type List=array[1..MaxN] of Integer;
var  Tree,Sign:List;
     N,i,j,z,K,M,m1:integer;
     res:boolean;
begin
     readln(M);
for m1:=1 to M do
begin readln(N);
     z:=0;
     res:=true;
     for i:=1 to N do
     begin
          read(Tree[i]);
          if Tree[i]=0 then
            inc(z);
     end;
     if z<>1 then
     begin
          res:=false;
          goto fin;
     end else
     begin
           FillChar(Sign,SizeOf(Sign),0);
          K:=0;
          while true do
          begin
               i:=1;
               inc(K);
                 while (Sign[i]>0) and (i<=N) do
                  inc(i);
               if i<>N+1 then
               begin
                       while i<>0 do
                       begin
                         if Sign[i]=0 then
                            Sign[i]:=K else
                         if Sign[i]=K then
                         begin
                              res:=false;
                              goto fin;
                         end else
                         if Sign[i]<K then
                           i:=0;
                         if i<>0 then
                           i:=Tree[i];
                    end;
               end else
                goto fin;
           end;
     end;
fin: if res=false then
        writeln('NO') else
        writeln('YES');
end;
end.