giabinh

793_NetworkConnections

Aug 14th, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.24 KB | None | 0 0
  1. const fi='uva793.inp';
  2.       fo='uva793.out';
  3.  
  4. var test, n, success, failure, u, v: longint;
  5.     pSet: array [1..1000000] of longint;
  6.     c: char;
  7.     firstline: boolean;
  8.  
  9. procedure initSet(n: longint);
  10. var i: longint;
  11. begin
  12.   for i:=1 to n do pSet[i]:=i;
  13.   success:=0; failure:=0;
  14. end;
  15.  
  16. function findSet(u: longint): longint;
  17. begin
  18.   if (pSet[u]=u) then exit(u);
  19.   pSet[u]:=findSet(pSet[u]);
  20.   exit(pSet[u]);
  21. end;
  22.  
  23. procedure unionSet(u, v: longint);
  24. begin
  25.   pSet[findSet(u)]:=findSet(v);
  26. end;
  27.  
  28. function isSameSet(u, v: longint): boolean;
  29. begin
  30.   exit(findSet(u)=findSet(v));
  31. end;
  32.  
  33. begin
  34.   assign(input, fi); reset(input);
  35.   assign(output, fo); rewrite(output);
  36.  
  37.   readln(test); readln;
  38.   firstline:=true;
  39.   while (test>0) do
  40.     begin
  41.       readln(n);
  42.       initSet(n);
  43.  
  44.       while true do
  45.         begin
  46.           read(c);
  47.           if (c<>'c') and (c<>'q') then break;
  48.  
  49.           readln(u, v);
  50.           if (c='c') then unionSet(u, v) else
  51.           //if (c='p') then
  52.             if (isSameSet(u, v)) then inc(success) else inc(failure);
  53.         end;
  54.  
  55.       dec(test);
  56.       if firstline then firstline:=false else writeln;
  57.       writeln(success, ',', failure);
  58.     end;
  59.  
  60.   close(input); close(output);
  61. end.
Advertisement
Add Comment
Please, Sign In to add comment