Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class E {
- FastScanner in;
- PrintWriter out;
- int k, n;
- final String possible = "possible";
- final String impossible = "impossible";
- long[][] neigh;
- long START;
- int it = 0;
- void go(long used1, long used2, int alr) {
- if (alr >= k) {
- out.println(possible);
- out.close();
- System.exit(0);
- }
- if (alr + n - Long.bitCount(used1) - Long.bitCount(used2) < k)
- return;
- it++;
- if (it % 10000 == 0) {
- if (System.currentTimeMillis() - START > 900) {
- out.println(impossible);
- out.close();
- System.exit(0);
- }
- }
- for (int i = 0; i < n; i++) {
- long n1 = neigh[0][i], n2 = neigh[1][i];
- long nused1 = used1, nused2 = used2;
- if (i < 64) {
- if (((1L << i) & used1) != 0)
- continue;
- nused1 ^= 1L << i;
- } else {
- if (((1L << (i & 63)) & used2) != 0)
- continue;
- nused2 ^= 1L << (i & 63);
- }
- n1 &= ~used1;
- n2 &= ~used2;
- go(nused1 ^ n1, nused2 ^ n2, alr + 1);
- if (Long.bitCount(n1) + Long.bitCount(n2) >= 2) {
- go(nused1, nused2, alr);
- }
- break;
- }
- }
- void solve() {
- k = in.nextInt();
- n = in.nextInt();
- START = System.currentTimeMillis();
- if (n >= k * 5) {
- out.println(possible);
- return;
- }
- neigh = new long[2][n];
- Random rnd = new Random(123);
- int[] perm = new int[n];
- for (int i = 0; i < n; i++) {
- int pos = rnd.nextInt(i + 1);
- perm[i] = perm[pos];
- perm[pos] = i;
- }
- for (int ii = 0; ii < n; ii++) {
- int i = perm[ii];
- int t = in.nextInt();
- for (int j = 0; j < t; j++) {
- int next = perm[in.nextInt() - 1];
- if (next < 64) {
- neigh[0][i] ^= 1L << next;
- } else {
- neigh[1][i] ^= 1L << (next & 63);
- }
- }
- }
- go(0, 0, 0);
- out.println(impossible);
- }
- void run() {
- try {
- in = new FastScanner(new File("E.in"));
- out = new PrintWriter(new File("E.out"));
- solve();
- out.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- void runIO() {
- in = new FastScanner(System.in);
- out = new PrintWriter(System.out);
- solve();
- out.close();
- }
- class FastScanner {
- BufferedReader br;
- StringTokenizer st;
- public FastScanner(File f) {
- try {
- br = new BufferedReader(new FileReader(f));
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- public FastScanner(InputStream f) {
- br = new BufferedReader(new InputStreamReader(f));
- }
- String next() {
- while (st == null || !st.hasMoreTokens()) {
- String s = null;
- try {
- s = br.readLine();
- } catch (IOException e) {
- e.printStackTrace();
- }
- if (s == null)
- return null;
- st = new StringTokenizer(s);
- }
- return st.nextToken();
- }
- boolean hasMoreTokens() {
- while (st == null || !st.hasMoreTokens()) {
- String s = null;
- try {
- s = br.readLine();
- } catch (IOException e) {
- e.printStackTrace();
- }
- if (s == null)
- return false;
- st = new StringTokenizer(s);
- }
- return true;
- }
- int nextInt() {
- return Integer.parseInt(next());
- }
- long nextLong() {
- return Long.parseLong(next());
- }
- double nextDouble() {
- return Double.parseDouble(next());
- }
- }
- public static void main(String[] args) {
- new E().runIO();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment