very poor description
Please someone explain what the problem actually said ....
what does it mean ?at least one member of each Si is assigned a different color from the other members
Moderator: Board moderators
what does it mean ?at least one member of each Si is assigned a different color from the other members
Code: Select all
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
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())
s.x.add(Integer.parseInt(st.nextToken()) - 1);
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;
BufferedReader br;
public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));}
public String next() throws IOException
{
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
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()); }
public boolean ready() throws IOException {return br.ready();}
}
}