Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Difficulty8;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.*;
- import java.util.function.Function;
- public class programmingteamselection {
- public static void main(String[] args) {
- StringBuilder sb = new StringBuilder(1000);
- InputReader in = new InputReader(System.in);
- while (true) {
- int pairs = in.nextInt();
- if (pairs == 0) break;
- boolean[][] graph = new boolean[15][15];
- Map<String, Integer> mapping = new HashMap<>();
- for (int i = 0; i < pairs; i++) {
- Function<String, Integer> f = new Function<>() {
- @Override
- public Integer apply(String s) {
- return mapping.size();
- }
- };
- int a = mapping.computeIfAbsent(in.readString(), f), b = mapping.computeIfAbsent(in.readString(), f);
- graph[a][b] = true;
- graph[b][a] = true;
- }
- boolean[] used = new boolean[mapping.size()];
- if (used.length % 3 != 0) {
- sb.append("impossible\n");
- continue;
- }
- sb.append(canDo(graph, used) ? "possible\n" : "impossible\n");
- }
- System.out.print(sb.toString());
- }
- private static boolean canDo(boolean[][] graph, boolean[] used) {
- int start = 0;
- while (start < used.length && used[start]) start++;
- if (start == used.length) return true;
- used[start] = true;
- for (int i = start+1; i < used.length; i++) {
- if (used[i] || !graph[start][i]) continue;
- used[i] = true;
- for (int j = i+1; j < used.length; j++) {
- if (used[j] || !graph[start][j] || !graph[i][j]) continue;
- used[j] = true;
- if (canDo(graph, used)) return true;
- used[j] = false;
- }
- used[i] = false;
- }
- return false;
- }
- static class InputReader {
- private final InputStream stream;
- private final byte[] buf = new byte[8192];
- private int curChar, snumChars;
- private SpaceCharFilter filter;
- public InputReader(InputStream stream) {
- this.stream = stream;
- }
- public int snext() {
- if (snumChars == -1)
- throw new InputMismatchException();
- if (curChar >= snumChars) {
- curChar = 0;
- try {
- snumChars = stream.read(buf);
- } catch (IOException e) {
- throw new InputMismatchException();
- }
- if (snumChars <= 0)
- return -1;
- }
- return buf[curChar++];
- }
- public int nextInt() {
- int c = snext();
- while (isSpaceChar(c)) {
- c = snext();
- }
- int sgn = 1;
- if (c == '-') {
- sgn = -1;
- c = snext();
- }
- int res = 0;
- do {
- if (c < '0' || c > '9')
- throw new InputMismatchException();
- res *= 10;
- res += c - '0';
- c = snext();
- } while (!isSpaceChar(c));
- return res * sgn;
- }
- public long nextLong() {
- int c = snext();
- while (isSpaceChar(c)) {
- c = snext();
- }
- int sgn = 1;
- if (c == '-') {
- sgn = -1;
- c = snext();
- }
- long res = 0;
- do {
- if (c < '0' || c > '9')
- throw new InputMismatchException();
- res *= 10;
- res += c - '0';
- c = snext();
- } while (!isSpaceChar(c));
- return res * sgn;
- }
- public int[] nextIntArray(int n) {
- int[] a = new int[n];
- for (int i = 0; i < n; i++) {
- a[i] = nextInt();
- }
- return a;
- }
- public double nextDouble() {
- int c = snext();
- while (isSpaceChar(c)) {
- c = snext();
- }
- int sgn = 1;
- if (c == '-') {
- sgn = -1;
- c = snext();
- }
- double res = 0;
- while (!isSpaceChar(c) && c != '.') {
- if (c == 'e' || c == 'E') {
- return res * Math.pow(10, nextInt());
- }
- if (c < '0' || c > '9') {
- throw new InputMismatchException();
- }
- res *= 10;
- res += c - '0';
- c = snext();
- }
- if (c == '.') {
- c = snext();
- double m = 1;
- while (!isSpaceChar(c)) {
- if (c == 'e' || c == 'E') {
- return res * Math.pow(10, nextInt());
- }
- if (c < '0' || c > '9') {
- throw new InputMismatchException();
- }
- m /= 10;
- res += (c - '0') * m;
- c = snext();
- }
- }
- return res * sgn;
- }
- public String readString() {
- int c = snext();
- while (isSpaceChar(c)) {
- c = snext();
- }
- StringBuilder res = new StringBuilder();
- do {
- res.appendCodePoint(c);
- c = snext();
- } while (!isSpaceChar(c));
- return res.toString();
- }
- public String nextLine() {
- int c = snext();
- while (isSpaceChar(c))
- c = snext();
- StringBuilder res = new StringBuilder();
- do {
- res.appendCodePoint(c);
- c = snext();
- } while (!isEndOfLine(c));
- return res.toString();
- }
- public boolean isSpaceChar(int c) {
- if (filter != null)
- return filter.isSpaceChar(c);
- return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
- }
- private boolean isEndOfLine(int c) {
- return c == '\n' || c == '\r' || c == -1;
- }
- public interface SpaceCharFilter {
- boolean isSpaceChar(int ch);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement