1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class reo {

  private static int[] parent;

  public static void main(String[] args) {
    try (Scanner scanner = new Scanner(System.in)) {
      int n = scanner.nextInt();
      int m = scanner.nextInt();

      Preference[][] preferences = new Preference[n][n];
      boolean[][] graph = new boolean[n][n];

      for (int i = 0; i < m; i++) {
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        char c = scanner.next().charAt(0);
        if (c == 'T') {
          preferences[b - 1][a - 1] = Preference.YES;
          graph[b - 1][a - 1] = true;
        } else {
          preferences[b - 1][a - 1] = Preference.NO;
        }
      }

      int root = findRoot(preferences);
      if (root == -1) {
        System.out.println("NIE");
        return;
      }

      for (int i = 0; i < graph.length; i++) {
        for (int j = 0; j < graph.length; j++) {
          if (preferences[i][j] != Preference.NO)
            graph[i][j] = true;
        }
      }


      do {
        boolean missionImpossible = createTreeFromGraph(n, preferences, graph, root);
        if (missionImpossible) {
          System.out.println("NIE");
          return;
        }
      } while (!correct(preferences, graph));

      Arrays.stream(parent).forEach(index -> System.out.println(index + 1));
    }
  }

  private static boolean createTreeFromGraph(int n, Preference[][] preferences, boolean[][] graph, int root) {
    parent = new int[n];
    for (int i = 0; i < parent.length; i++) {
      parent[i] = Integer.MIN_VALUE;
    }
    parent[root] = -1;

    boolean[] visitedNodes = bfs(graph, root);
    for (boolean val : visitedNodes) {
      if (!val) {
        return true;
      }
    }

    return false;
  }

  private static boolean correct(Preference[][] preferences, boolean[][] graph) {
    boolean correct = true;
    for (int i = 0; i < preferences.length; i++) {
      for (int j = 0; j < preferences.length; j++) {
        if (preferences[i][j] == Preference.YES) {
          if (!isPreferenceCorrect(i, j)) {
            graph[parent[j]][j] = false;
            correct = false;
          }
        }
      }
    }
    return correct;
  }

  private static boolean isPreferenceCorrect(int superior, int child) {
    int currentChild = child;
    while (true) {
      if (parent[currentChild] == superior) {
        return true;
      }
      if (parent[currentChild] == -1) {
        return false;
      }
      currentChild = parent[currentChild];
    }
  }

  private static int findRoot(Preference[][] preferences) {
    int length = preferences.length;

    for (int i = 0; i < length; i++) {
      boolean found = true;
      for (int j = 0; j < length; j++) {
        if (preferences[j][i] == Preference.YES) {
          found = false;
          break;
        }
      }
      if (found) {
        return i;
      }
    }
    return -1;
  }


  static boolean[] bfs(boolean[][] graph, int root) {
    boolean[] visitedNodes = new boolean[graph.length];
    Queue<Integer> queue = new LinkedList<>();

    queue.add(root);
    visitedNodes[root] = true;

    while (!queue.isEmpty()) {
      int nodeFromQueue = queue.remove();

      for (int i = 0; i < graph[0].length; i++) {
        if (!visitedNodes[i] && graph[nodeFromQueue][i]) {
          parent[i] = nodeFromQueue;
          queue.add(i);
          visitedNodes[i] = true;
        }
      }
    }
    return visitedNodes;
  }

  private enum Preference {
    YES, NO
  }

}