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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import java.util.HashSet;
import java.util.Scanner;

public class reo {

    static class Node {
        int employeeNr;
        HashSet<Integer> loved = new HashSet<>();
        HashSet<Integer> hated = new HashSet<>();
        HashSet<Integer> children = new HashSet<>();
        boolean hasHated;
        boolean visited;
        Node parent;

        Node(int e) {
            employeeNr = e;
        }
    }

    static class Constraint {
        boolean want;
        int employee;
        int boss;

        Constraint(boolean w, int e, int b) {
            want = w;
            employee = e;
            boss = b;
        }
    }

    public static void main(String[] args) {
        solve();
    }

    private static void solve() {
        Scanner s = new Scanner(System.in);
        int count = s.nextInt();
        int constraintsCount = s.nextInt();

        Node[] nodes = new Node[count];
        for (int i = 0; i < count; i++) {
            nodes[i] = new Node(i);
        }
        for (int i = 0; i < constraintsCount; i++) {
            Constraint c = readConstraint(s);
            if (c.want) {
                nodes[c.employee].loved.add(c.boss);
                nodes[c.boss].children.add(c.employee);
            } else {
                nodes[c.employee].hated.add(c.boss);
            }
        }

        if (!fillHated(nodes)) {
            System.out.println("NIE");
            return;
        }

        for (int i = 0; i < count; i++) {
            Node node = nodes[i];
            if (node.loved.isEmpty()) {
                continue;
            }
            for (int loved : node.loved) {
                if (!isMyBoss(node, nodes[loved]) && !goUp(node, nodes[loved])) {
                    System.out.println("NIE");
                    return;
                }
            }
        }

        HashSet<Integer> bossCandidates = new HashSet<>();
        for (int i = 0; i < count; i++) {
            Integer bossOrNull = findBoss(nodes[i]);
            if (bossOrNull != null) {
                bossCandidates.add(bossOrNull);
            }
        }

        for (int i = 0; i < count; i++) {
            if (!bossCandidates.contains(i) && canBeBoss(i, nodes)) {
                bossCandidates.add(i);
            }
        }

        Node bigBossCandidate = null;
        for (int i : bossCandidates) {
            Node n = nodes[i];
            boolean fits = true;
            for (int j : bossCandidates) {
                if (i == j) continue;
                if (nodes[j].hated.contains(n.employeeNr)) {
                    fits = false;
                    break;
                }
            }
            if (fits) {
                bigBossCandidate = n;
                break;
            }
        }
        if (bigBossCandidate == null) {
            System.out.println("NIE");
            return;
        }
        if (bigBossCandidate.parent != null) {
            for (int i = 0; i < count; i++) {
                Node n = nodes[i];
                if (n.parent == bigBossCandidate) {
                    n.parent = bigBossCandidate.parent;
                }
            }
            bigBossCandidate.parent = null;
        }

        for (int i : bossCandidates) {
            if (i == bigBossCandidate.employeeNr) continue;
            nodes[i].parent = bigBossCandidate;
        }

        for (int i = 0; i < count; i++) {
            Node node = nodes[i];
            System.out.println(node.parent == null ? 0 : node.parent.employeeNr + 1);
        }
    }

    private static boolean canBeBoss(int employeeId, Node[] nodes) {
        Node child = nodes[employeeId];
        if (!child.loved.isEmpty()) {
            return false;
        }
        Node parent = child.parent;
        while (parent != null) {
            if (parent.hated.contains(child.employeeNr)) {
                return false;
            }
            parent = parent.parent;
        }
        return true;
    }

    private static Integer findBoss(Node node) {
        while (!node.visited && node.parent != null) {
            node.visited = true;
            node = node.parent;
        }
        return node.parent == null ? node.employeeNr : null;
    }

    private static boolean isMyBoss(Node node, Node parent) {
        Node boss = node.parent;
        while (boss != null) {
            if (boss.employeeNr == parent.employeeNr) {
                return true;
            }
            boss = boss.parent;
        }
        return false;
    }

    private static boolean goUp(Node child, Node parent) {
        if (child.hated.contains(parent.employeeNr)) {
            return false;
        }
        Node worseThenParent = findBestWorseThen(parent, child);
        if (worseThenParent == null || worseThenParent.hated.contains(parent.employeeNr)) {
            return false;
        }
        if (isMyBoss(parent, child)) {
            parent.parent = child.parent;
            child.parent = parent;
        } else {
            if (worseThenParent.parent == null) {
                worseThenParent.parent = parent;
            } else {
                parent.parent = worseThenParent.parent;
                worseThenParent.parent = parent;
            }
        }
        return true;
    }

    private static Node findBestWorseThen(Node parent, Node child) {
        Node candidate = child;
        boolean fail = false;

        while (child.parent != null) {
            child = child.parent;
            if (child.hated.contains(parent.employeeNr)) {
                fail = true;
            }
            if (parent.hated.contains(child.employeeNr)) {
                if (fail) {
                    return null;
                }
                candidate = child;
            }
        }
        return candidate;
    }

    private static boolean fillHated(Node[] nodes) {
        for (Node node : nodes) {
            if (node.hasHated) {
                continue;
            }

            if (!fillHatedForNode(node, nodes)) {
                return false;
            }
        }
        for (Node node : nodes) {
            node.visited = false;
        }
        return true;
    }

    private static boolean fillHatedForNode(Node node, Node[] nodes) {
        node.visited = true;
        for (int i : node.children) {
            Node child = nodes[i];
            if (child.visited) {
                return false;
            }
            if (!child.hasHated) {
                if (!fillHatedForNode(child, nodes)) {
                    return false;
                }
            }
            for (int hatedBoss : child.hated) {
                node.hated.add(hatedBoss);
            }
        }
        node.hasHated = true;
        node.visited = false;
        return true;
    }

    private static Constraint readConstraint(Scanner s) {
        int employee = s.nextInt() - 1;
        int boss = s.nextInt() - 1;
        boolean want = s.next().equals("T");
        return new Constraint(want, employee, boss);
    }
}