## 12348 - Fun Coloring

Moderator: Board moderators

@li_kuet
New poster
Posts: 44
Joined: Fri May 25, 2012 6:22 pm

### 12348 - Fun Coloring

Can't understand the problem ...
very poor description
Please someone explain what the problem actually said ....
at least one member of each Si is assigned a different color from the other members
what does it mean ?

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 12348 - Fun Coloring

You need to see if you can assign a color (R or B) to each integer 1 through n such that none of the m sets contain only one color. For the first sample input you could assign:
1=R, 2=R, 3=B, 4=R, 5=R

1R 2R 3B
2R 3B 4R
1R 3B 5R

For the second sample input if you assign 1=R, then 2 and 3 must be B, but 2 and 3 are in a set together and that set is all B so it isn't possible to fun color.
1R 2B
1R 3B
2B 3B
Check input and AC output for thousands of problems on uDebug!

@li_kuet
New poster
Posts: 44
Joined: Fri May 25, 2012 6:22 pm

### Re: 12348 - Fun Coloring

Now i get the problem
This problem made me crazy .....

@li_kuet
New poster
Posts: 44
Joined: Fri May 25, 2012 6:22 pm

### Re: 12348 - Fun Coloring

Careful !!! There shouldn't be any newline in the output .. it will cost WA

New poster
Posts: 3
Joined: Thu Jul 09, 2015 5:20 am

### Re: 12348 - Fun Coloring

Can someone help me please? I have got a lot of WA and I don't know why. I have tried various solutions and no one worked. This is the most straightforward one (though not optimal) but it is WA. I will appreciate any help. Thanks!

Code: Select all

``````import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class B {

static int N, M;
static Set[] sets;

public static void main(String[] args) throws IOException
{
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);

int tc = Integer.parseInt(sc.nextLine());
while(tc-->0)
{
StringTokenizer st = new StringTokenizer(sc.nextLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
sets = new Set[M];

for(int i = 0; i < M; ++i)
{
st = new StringTokenizer(sc.nextLine());
Set s = new Set();
while(st.hasMoreTokens())
sets[i] = s;
}

boolean possible = false;
for(long i = 0; !possible && i < 1l<<N; ++i)
{
boolean can = true;
for(Set s: sets)
{
if(s.x.size() == 3)
{
long sum = ((i>>s.x.get(0))&1) + ((i>>s.x.get(1))&1) + ((i>>s.x.get(2))&1);
if(sum <= 0 || sum >= 3)
can = false;
}
else
if(s.x.size() == 2)
{
long sum = ((i>>s.x.get(0))&1) + ((i>>s.x.get(1))&1);
if(sum != 1)
can = false;
}
else
can = false;
if(!can)
break;
}
possible |= can;
}
System.out.print(possible?'Y':'N');
if(tc != 0)
sc.nextLine();
}

out.flush();
out.close();
}

static class Set { ArrayList<Integer> x = new ArrayList<Integer>(); }

static class Scanner
{
StringTokenizer st;

public String next() throws IOException
{
while (st == null || !st.hasMoreTokens())
return st.nextToken();
}

public int nextInt() throws IOException {return Integer.parseInt(next());}

public long nextLong() throws IOException {return Long.parseLong(next());}

public String nextLine() throws IOException {return br.readLine();}

public double nextDouble() throws IOException { return Double.parseDouble(next()); }