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 1128. Partition into Groups

Why it fails? (+)
Posted by Andrey Popyk (popyk@ief.tup.km.ua) 13 Jun 2002 17:23
I use DFS, but get WA.
Give me a test please.

Andrey Popyk.
popyk@ief.tup.km.ua
ICQ# 88914410

CONST Dim = 7200;

VAR A:Array[1..Dim,0..3] of integer;
    Col:Array[1..Dim] of byte;
    N:integer;

PROCEDURE ReadData;
var i,j:integer;
begin
  readln(N);
  for i:=1 to N do
  begin
    read(A[i,0]);
    for j:=1 to A[i,0] do read(A[i,j]);
    readln;
  end;
end;

PROCEDURE DFS(v:integer; c:byte);
var i:integer;
begin
  Col[v]:=c;
  for i:=1 to A[v,0] do
    if Col[A[v,i]]=0 then DFS(A[v,i],3-c);
end;

PROCEDURE Solve;
var i:integer;
begin
  for i:=1 to N do
    if Col[i]=0 then DFS(i,1);
end;

PROCEDURE WriteData;
var S,C,i:integer;
begin
  S:=0;
  for i:=1 to N do
    if Col[i]=1 then inc(S);
  if S>N-S then begin S:=N-S; C:=2 end
           else C:=1;
  writeln(S);
  for i:=1 to N do
    if Col[i]=C then write(i,' ');
  writeln;
end;

BEGIN
  ReadData;
  Solve;
  WriteData;
END.
Re: Why it fails? (+)
Posted by Christopher Moh 14 Jun 2002 16:23
How about this?

4
3 2 3 4
3 1 3 4
2 1 2
2 1 2

DFS does this:

Go to node 1, mark it as colour 1.
Go to node 2, mark it as colour 2.
Go to node 3, mark it as colour 1.
Return to node 2.
Go to node 4, mark it as colour 1.

Then node 1 will be the same colour as node 3 and 4.

Correct solution is to put nodes 3 and 4 together and node 1 and 2
together.

Did I make any error here?
Thanks, I will try to correct it
Posted by Andrey Popyk (popyk@ief.tup.km.ua) 14 Jun 2002 19:46