Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class game_as {
- int n1;
- int n2;
- ArrayList<Integer>[] es;
- boolean[] u;
- int[] px;
- int[] py;
- boolean[] killed1;
- boolean[] killed2;
- boolean dfs(int i) {
- u[i] = true;
- for (int j : es[i]) {
- if (!killed2[j] && (py[j] == -1 || (!u[py[j]] && !killed1[py[j]]))) {
- if (py[j] == -1 || dfs(py[j])) {
- px[i] = j;
- py[j] = i;
- return true;
- }
- }
- }
- return false;
- }
- public void run() throws IOException {
- Scanner in = new Scanner(new File("game.in"));
- PrintWriter out = new PrintWriter(new File("game.out"));
- n1 = in.nextInt();
- n2 = in.nextInt();
- es = new ArrayList[n1];
- for (int i = 0; i < n1; i++) {
- es[i] = new ArrayList<Integer>();
- }
- int m = in.nextInt();
- for (int i = 0; i < m; i++) {
- int a = in.nextInt() - 1;
- int b = in.nextInt() - 1;
- es[a].add(b);
- }
- u = new boolean[n1];
- px = new int[n1];
- Arrays.fill(px, -1);
- py = new int[n2];
- Arrays.fill(py, -1);
- killed1 = new boolean[n1];
- killed2 = new boolean[n2];
- for (int i = 0; i < n1; i++) {
- Arrays.fill(u, false);
- dfs(i);
- }
- char[] w1 = new char[n1];
- char[] w2 = new char[n2];
- for (int i = 0; i < n1; i++) {
- if (px[i] == -1) {
- w1[i] = 'P';
- } else {
- py[px[i]] = -1;
- killed1[i] = true;
- Arrays.fill(u, false);
- boolean found = false;
- for (int j = 0; j < n1; j++) {
- if (j != i && px[j] == -1 && !u[j] && dfs(j)) {
- found = true;
- break;
- }
- }
- if (found) {
- w1[i] = 'P';
- px[i] = -1;
- killed1[i] = false;
- } else {
- w1[i] = 'N';
- py[px[i]] = i;
- killed1[i] = false;
- }
- }
- }
- for (int i = 0; i < n2; i++) {
- if (py[i] == -1) {
- w2[i] = 'P';
- } else {
- px[py[i]] = -1;
- killed2[i] = true;
- Arrays.fill(u, false);
- boolean found = false;
- for (int j = 0; j < n1; j++) {
- if (px[j] == -1 && !u[j] && dfs(j)) {
- found = true;
- break;
- }
- }
- if (found) {
- w2[i] = 'P';
- py[i] = -1;
- killed2[i] = false;
- } else {
- w2[i] = 'N';
- px[py[i]] = i;
- killed2[i] = false;
- }
- }
- }
- for (char c : w1) {
- out.print(c);
- }
- out.println();
- for (char c : w2) {
- out.print(c);
- }
- out.println();
- in.close();
- out.close();
- }
- public static void main(String[] arg) throws IOException {
- new game_as().run();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment