Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fi='uva793.inp';
- fo='uva793.out';
- var test, n, success, failure, u, v: longint;
- pSet: array [1..1000000] of longint;
- c: char;
- firstline: boolean;
- procedure initSet(n: longint);
- var i: longint;
- begin
- for i:=1 to n do pSet[i]:=i;
- success:=0; failure:=0;
- end;
- function findSet(u: longint): longint;
- begin
- if (pSet[u]=u) then exit(u);
- pSet[u]:=findSet(pSet[u]);
- exit(pSet[u]);
- end;
- procedure unionSet(u, v: longint);
- begin
- pSet[findSet(u)]:=findSet(v);
- end;
- function isSameSet(u, v: longint): boolean;
- begin
- exit(findSet(u)=findSet(v));
- end;
- begin
- assign(input, fi); reset(input);
- assign(output, fo); rewrite(output);
- readln(test); readln;
- firstline:=true;
- while (test>0) do
- begin
- readln(n);
- initSet(n);
- while true do
- begin
- read(c);
- if (c<>'c') and (c<>'q') then break;
- readln(u, v);
- if (c='c') then unionSet(u, v) else
- //if (c='p') then
- if (isSameSet(u, v)) then inc(success) else inc(failure);
- end;
- dec(test);
- if firstline then firstline:=false else writeln;
- writeln(success, ',', failure);
- end;
- close(input); close(output);
- end.
Advertisement
Add Comment
Please, Sign In to add comment