Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.61 KB | None | 0 0
  1. package Difficulty8;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.util.*;
  6. import java.util.function.Function;
  7.  
  8. public class programmingteamselection {
  9.     public static void main(String[] args) {
  10.         StringBuilder sb = new StringBuilder(1000);
  11.         InputReader in = new InputReader(System.in);
  12.         while (true) {
  13.             int pairs = in.nextInt();
  14.             if (pairs == 0) break;
  15.             boolean[][] graph = new boolean[15][15];
  16.             Map<String, Integer> mapping = new HashMap<>();
  17.             for (int i = 0; i < pairs; i++) {
  18.                 Function<String, Integer> f = new Function<>() {
  19.                     @Override
  20.                     public Integer apply(String s) {
  21.                         return mapping.size();
  22.                     }
  23.                 };
  24.                 int a = mapping.computeIfAbsent(in.readString(), f), b = mapping.computeIfAbsent(in.readString(), f);
  25.                 graph[a][b] = true;
  26.                 graph[b][a] = true;
  27.             }
  28.             boolean[] used = new boolean[mapping.size()];
  29.             if (used.length % 3 != 0) {
  30.                 sb.append("impossible\n");
  31.                 continue;
  32.             }
  33.             sb.append(canDo(graph, used) ? "possible\n" : "impossible\n");
  34.         }
  35.         System.out.print(sb.toString());
  36.     }
  37.  
  38.     private static boolean canDo(boolean[][] graph, boolean[] used) {
  39.         int start = 0;
  40.         while (start < used.length && used[start]) start++;
  41.         if (start == used.length) return true;
  42.         used[start] = true;
  43.         for (int i = start+1; i < used.length; i++) {
  44.             if (used[i] || !graph[start][i]) continue;
  45.             used[i] = true;
  46.             for (int j = i+1; j < used.length; j++) {
  47.                 if (used[j] || !graph[start][j] || !graph[i][j]) continue;
  48.                 used[j] = true;
  49.                 if (canDo(graph, used)) return true;
  50.                 used[j] = false;
  51.             }
  52.             used[i] = false;
  53.         }
  54.         return false;
  55.     }
  56.  
  57.     static class InputReader {
  58.         private final InputStream stream;
  59.         private final byte[] buf = new byte[8192];
  60.         private int curChar, snumChars;
  61.         private SpaceCharFilter filter;
  62.  
  63.         public InputReader(InputStream stream) {
  64.             this.stream = stream;
  65.         }
  66.  
  67.         public int snext() {
  68.             if (snumChars == -1)
  69.                 throw new InputMismatchException();
  70.             if (curChar >= snumChars) {
  71.                 curChar = 0;
  72.                 try {
  73.                     snumChars = stream.read(buf);
  74.                 } catch (IOException e) {
  75.                     throw new InputMismatchException();
  76.                 }
  77.                 if (snumChars <= 0)
  78.                     return -1;
  79.             }
  80.             return buf[curChar++];
  81.         }
  82.  
  83.         public int nextInt() {
  84.             int c = snext();
  85.             while (isSpaceChar(c)) {
  86.                 c = snext();
  87.             }
  88.             int sgn = 1;
  89.             if (c == '-') {
  90.                 sgn = -1;
  91.                 c = snext();
  92.             }
  93.             int res = 0;
  94.             do {
  95.                 if (c < '0' || c > '9')
  96.                     throw new InputMismatchException();
  97.                 res *= 10;
  98.                 res += c - '0';
  99.                 c = snext();
  100.             } while (!isSpaceChar(c));
  101.             return res * sgn;
  102.         }
  103.  
  104.         public long nextLong() {
  105.             int c = snext();
  106.             while (isSpaceChar(c)) {
  107.                 c = snext();
  108.             }
  109.             int sgn = 1;
  110.             if (c == '-') {
  111.                 sgn = -1;
  112.                 c = snext();
  113.             }
  114.             long res = 0;
  115.             do {
  116.                 if (c < '0' || c > '9')
  117.                     throw new InputMismatchException();
  118.                 res *= 10;
  119.                 res += c - '0';
  120.                 c = snext();
  121.             } while (!isSpaceChar(c));
  122.             return res * sgn;
  123.         }
  124.  
  125.         public int[] nextIntArray(int n) {
  126.             int[] a = new int[n];
  127.             for (int i = 0; i < n; i++) {
  128.                 a[i] = nextInt();
  129.             }
  130.             return a;
  131.         }
  132.  
  133.         public double nextDouble() {
  134.             int c = snext();
  135.             while (isSpaceChar(c)) {
  136.                 c = snext();
  137.             }
  138.             int sgn = 1;
  139.             if (c == '-') {
  140.                 sgn = -1;
  141.                 c = snext();
  142.             }
  143.             double res = 0;
  144.             while (!isSpaceChar(c) && c != '.') {
  145.                 if (c == 'e' || c == 'E') {
  146.                     return res * Math.pow(10, nextInt());
  147.                 }
  148.                 if (c < '0' || c > '9') {
  149.                     throw new InputMismatchException();
  150.                 }
  151.                 res *= 10;
  152.                 res += c - '0';
  153.                 c = snext();
  154.             }
  155.             if (c == '.') {
  156.                 c = snext();
  157.                 double m = 1;
  158.                 while (!isSpaceChar(c)) {
  159.                     if (c == 'e' || c == 'E') {
  160.                         return res * Math.pow(10, nextInt());
  161.                     }
  162.                     if (c < '0' || c > '9') {
  163.                         throw new InputMismatchException();
  164.                     }
  165.                     m /= 10;
  166.                     res += (c - '0') * m;
  167.                     c = snext();
  168.                 }
  169.             }
  170.             return res * sgn;
  171.         }
  172.  
  173.         public String readString() {
  174.             int c = snext();
  175.             while (isSpaceChar(c)) {
  176.                 c = snext();
  177.             }
  178.             StringBuilder res = new StringBuilder();
  179.             do {
  180.                 res.appendCodePoint(c);
  181.                 c = snext();
  182.             } while (!isSpaceChar(c));
  183.             return res.toString();
  184.         }
  185.  
  186.         public String nextLine() {
  187.             int c = snext();
  188.             while (isSpaceChar(c))
  189.                 c = snext();
  190.             StringBuilder res = new StringBuilder();
  191.             do {
  192.                 res.appendCodePoint(c);
  193.                 c = snext();
  194.             } while (!isEndOfLine(c));
  195.             return res.toString();
  196.         }
  197.  
  198.         public boolean isSpaceChar(int c) {
  199.             if (filter != null)
  200.                 return filter.isSpaceChar(c);
  201.             return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
  202.         }
  203.  
  204.         private boolean isEndOfLine(int c) {
  205.             return c == '\n' || c == '\r' || c == -1;
  206.         }
  207.  
  208.         public interface SpaceCharFilter {
  209.             boolean isSpaceChar(int ch);
  210.         }
  211.     }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement