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
import static java.util.Collections.emptySet;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.function.Function;

/**
 * Created by stawicad on 2016-11-23.
 */
public class reo {

    public static void main(String[] args) {

        long s = System.currentTimeMillis();
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        Map<Integer, Set<Integer>> want = new HashMap<>();
        Map<Integer, Set<Integer>> hate = new HashMap<>();
        Map<Integer, Set<Integer>> childs = new HashMap<>();
        Function<Integer, Set<Integer>> newHashSet = x -> new HashSet<>();
        boolean[] roots = new boolean[n + 1];
        for (int i = 0; i < m; i++) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            String c = sc.next();
            if("T".equals(c)) {
                want.computeIfAbsent(a, newHashSet).add(b);
                childs.computeIfAbsent(b, newHashSet).add(a);
                roots[a] = true;
            }
            else {
                hate.computeIfAbsent(a, newHashSet).add(b);
                roots[b] = true;
            }
        }
        boolean calc = calc(n, want, hate, childs, roots);
//        System.out.print(System.currentTimeMillis() - s + " ");
        if(!calc) {
            System.out.println("NIE");
        }
//        else {
//            System.out.println("TAK");
//        }

    }

    private static boolean calc(final int n, final Map<Integer, Set<Integer>> want, final Map<Integer, Set<Integer>> hate, final Map<Integer, Set<Integer>> childs, final boolean[] roots) {
        int[] timeout = {10000};
        for(int i = 1; i <= n; i++) if(!roots[i]) {
            boolean compute = compute(want, hate, childs, i, n, timeout);
//            if(timeout[0] < 0) System.out.print("timeout ");
            return compute;
        }
        return false;
    }

    private static boolean compute(final Map<Integer, Set<Integer>> want, final Map<Integer, Set<Integer>> hate, final Map<Integer, Set<Integer>> childs, final int root, final int n, final int[] timeout) {
        if(root < 0) return false;
        int[] answer = new int[n + 1];
        boolean[] creating = new boolean[n + 1];
        for (int i = 1; i <= n; i++) if(i != root && childs.getOrDefault(i, emptySet()).isEmpty()){
            if(findParent(i, want, hate, answer, root, emptySet(), emptySet(), creating, timeout) < 0) {
                return false;
            }
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 1; i <= n; i++) {
            if(i != root && answer[i] == 0) return false;
            sb.append(answer[i]).append(" ");
        }
        System.out.println(sb.toString());
        return true;
    }

    private static int findParent(int node, Map<Integer, Set<Integer>> want, Map<Integer, Set<Integer>> hate, int[] answer, int root, Set<Integer> must, Set<Integer> mustNot, boolean[] creating, final int[] timeout) {
        if(node == root) {
            if(must.isEmpty()) return 0;
            else return -1;
        }
        timeout[0]--;
        if(timeout[0] < 0) return -1;
        Set<Integer> myPreferences = want.getOrDefault(node, emptySet());
        creating[node] = true;
        try {
            if (hasCreating(myPreferences, creating) || hasCreating(must, creating)) return -1;

            Set<Integer> it = new HashSet<>(myPreferences);
            it.addAll(must);
            if (it.isEmpty()) {
                return answer[node] = root;
            }

            Set<Integer> orDefault = hate.getOrDefault(node, emptySet());
            for (Integer p : it) {
                if(!hatesBosess(orDefault, mustNot, myPreferences, must)) {
                    Set<Integer> nextMust = new HashSet<>(must);
                    nextMust.addAll(myPreferences);
                    nextMust.remove(p);
                    Set<Integer> nextMustNot = new HashSet<>(mustNot);
                    nextMustNot.addAll(orDefault);
                    if(findParent(p, want, hate, answer, root, nextMust, nextMustNot, creating, timeout) >= 0) {
                        return answer[node] = p;
                    }
                }
            }
            return -1;
        } finally {
            creating[node] = false;
        }
    }

    private static boolean hatesBosess(Set<Integer> hates1, Set<Integer> hates2, Set<Integer> bosses1, Set<Integer> bosses2) {
        for(Set<Integer> hates : Arrays.asList(hates1, hates2))
        for (int i : hates) {
            if(bosses1.contains(i) || bosses2.contains(i)) {
                return true;
            }
        }
        return false;
    }

    private static boolean hasCreating(final Set<Integer> set, final boolean[] creating) {
        for (Integer i : set) {
            if(creating[i]) return true;
        }
        return false;
    }
}