qwerty787788

ncpc_b

Oct 6th, 2014
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.32 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class E {
  5.     FastScanner in;
  6.     PrintWriter out;
  7.  
  8.     int k, n;
  9.     final String possible = "possible";
  10.     final String impossible = "impossible";
  11.     long[][] neigh;
  12.     long START;
  13.     int it = 0;
  14.  
  15.     void go(long used1, long used2, int alr) {
  16.         if (alr >= k) {
  17.             out.println(possible);
  18.             out.close();
  19.             System.exit(0);
  20.         }
  21.         if (alr + n - Long.bitCount(used1) - Long.bitCount(used2) < k)
  22.             return;
  23.         it++;
  24.         if (it % 10000 == 0) {
  25.             if (System.currentTimeMillis() - START > 900) {
  26.                 out.println(impossible);
  27.                 out.close();
  28.                 System.exit(0);
  29.             }
  30.         }
  31.         for (int i = 0; i < n; i++) {
  32.             long n1 = neigh[0][i], n2 = neigh[1][i];
  33.             long nused1 = used1, nused2 = used2;
  34.             if (i < 64) {
  35.                 if (((1L << i) & used1) != 0)
  36.                     continue;
  37.                 nused1 ^= 1L << i;
  38.             } else {
  39.                 if (((1L << (i & 63)) & used2) != 0)
  40.                     continue;
  41.                 nused2 ^= 1L << (i & 63);
  42.             }
  43.             n1 &= ~used1;
  44.             n2 &= ~used2;
  45.             go(nused1 ^ n1, nused2 ^ n2, alr + 1);
  46.             if (Long.bitCount(n1) + Long.bitCount(n2) >= 2) {
  47.                 go(nused1, nused2, alr);
  48.             }
  49.             break;
  50.         }
  51.     }
  52.  
  53.     void solve() {
  54.         k = in.nextInt();
  55.         n = in.nextInt();
  56.         START = System.currentTimeMillis();
  57.         if (n >= k * 5) {
  58.             out.println(possible);
  59.             return;
  60.         }
  61.         neigh = new long[2][n];
  62.         Random rnd = new Random(123);
  63.         int[] perm = new int[n];
  64.         for (int i = 0; i < n; i++) {
  65.             int pos = rnd.nextInt(i + 1);
  66.             perm[i] = perm[pos];
  67.             perm[pos] = i;
  68.         }
  69.         for (int ii = 0; ii < n; ii++) {
  70.             int i = perm[ii];
  71.             int t = in.nextInt();
  72.             for (int j = 0; j < t; j++) {
  73.                 int next = perm[in.nextInt() - 1];
  74.                 if (next < 64) {
  75.                     neigh[0][i] ^= 1L << next;
  76.                 } else {
  77.                     neigh[1][i] ^= 1L << (next & 63);
  78.                 }
  79.             }
  80.         }
  81.         go(0, 0, 0);
  82.         out.println(impossible);
  83.     }
  84.  
  85.     void run() {
  86.         try {
  87.             in = new FastScanner(new File("E.in"));
  88.             out = new PrintWriter(new File("E.out"));
  89.  
  90.             solve();
  91.  
  92.             out.close();
  93.         } catch (FileNotFoundException e) {
  94.             e.printStackTrace();
  95.         }
  96.     }
  97.  
  98.     void runIO() {
  99.  
  100.         in = new FastScanner(System.in);
  101.         out = new PrintWriter(System.out);
  102.  
  103.         solve();
  104.  
  105.         out.close();
  106.     }
  107.  
  108.     class FastScanner {
  109.         BufferedReader br;
  110.         StringTokenizer st;
  111.  
  112.         public FastScanner(File f) {
  113.             try {
  114.                 br = new BufferedReader(new FileReader(f));
  115.             } catch (FileNotFoundException e) {
  116.                 e.printStackTrace();
  117.             }
  118.         }
  119.  
  120.         public FastScanner(InputStream f) {
  121.             br = new BufferedReader(new InputStreamReader(f));
  122.         }
  123.  
  124.         String next() {
  125.             while (st == null || !st.hasMoreTokens()) {
  126.                 String s = null;
  127.                 try {
  128.                     s = br.readLine();
  129.                 } catch (IOException e) {
  130.                     e.printStackTrace();
  131.                 }
  132.                 if (s == null)
  133.                     return null;
  134.                 st = new StringTokenizer(s);
  135.             }
  136.             return st.nextToken();
  137.         }
  138.  
  139.         boolean hasMoreTokens() {
  140.             while (st == null || !st.hasMoreTokens()) {
  141.                 String s = null;
  142.                 try {
  143.                     s = br.readLine();
  144.                 } catch (IOException e) {
  145.                     e.printStackTrace();
  146.                 }
  147.                 if (s == null)
  148.                     return false;
  149.                 st = new StringTokenizer(s);
  150.             }
  151.             return true;
  152.         }
  153.  
  154.         int nextInt() {
  155.             return Integer.parseInt(next());
  156.         }
  157.  
  158.         long nextLong() {
  159.             return Long.parseLong(next());
  160.         }
  161.  
  162.         double nextDouble() {
  163.             return Double.parseDouble(next());
  164.         }
  165.     }
  166.  
  167.     public static void main(String[] args) {
  168.         new E().runIO();
  169.     }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment