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
import java.util.*;

public class reo {

    public static void main(String[] args) {
        solution();
    }
    public static void solution() {
        final Scanner sc = new Scanner(System.in);
        int graphSize = sc.nextInt();
        int sumOfEdges = sc.nextInt();

        Vertex[] graph = new Vertex[graphSize+1];
        Vertex[] graphFalse = new Vertex[graphSize+1];
        for (int i = 0; i < graphSize; i++) {
            graph[i+1] = new Vertex(i+1);
            graphFalse[i+1] = new Vertex(i+1);
        }
        int edgeFrom, edgeTo;
        boolean isGraphTrue;
        for (int i = 0; i < sumOfEdges; i++) {
            edgeFrom = sc.nextInt();
            edgeTo = sc.nextInt();
            isGraphTrue = "T".equals(sc.next());
            if (isGraphTrue) {
                graph[edgeFrom].addiWant(graph[edgeTo]);
                graph[edgeTo].addWantMe(graph[edgeFrom]);
                graph[edgeFrom].addNeighbour(graph[edgeTo]);
            }
            else {
                graphFalse[edgeFrom].addNeighbour(graphFalse[edgeTo]);
                graph[edgeFrom].addIDontWant(graph[edgeTo]);
                graph[edgeTo].addDontWantMe(graph[edgeFrom]);
            }
        }


        boolean noSolution = false;
        for (int i = 1; i < graphSize+1; i++) {
            if (!graph[i].done)
                if (isCycle(graph[i])) {
                    noSolution = true;
                    break;
                }
        }
        if (noSolution)
            System.out.println("NIE");
        else {
            int counter = 0;
            int[] solution = new int[graphSize];
            Vertex[] possbile = new Vertex[graphSize];
            for (int i = 1; i < graphSize+1; i++) possbile[i-1] = graph[i];
            Vertex chosen;

            chosen = findWithoutIWantAndDontWantMe(possbile);
            int root = chosen != null ? chosen.number : -1;
            while (chosen != null) {
                chosen.visit();
                Vertex[] tmp = new Vertex[possbile.length-1];
                int j = 0;
                for (int i = 0; i < possbile.length; i++) {
                    if (!possbile[i].equals(chosen))
                        tmp[j++] = possbile[i];
                }
                possbile = tmp;
                solution[chosen.number-1] = counter;
                counter = chosen.number;
                chosen = findWithoutIWantAndDontWantMe(possbile);
                if (chosen == null)
                    chosen = findWitoutIWant(possbile, root);
            }
            if (possbile.length != 0)
                System.out.println("NIE");
            else {
                for (int i: solution)
                    System.out.println(i);
            }
        }

    }

    public static Vertex findWithoutIWantAndDontWantMe(Vertex[] possible) {
        for (Vertex v: possible)
            if (v.canBeTaken()) return v;
        return null;
    }

    public static Vertex findWitoutIWant(Vertex[] possible, int possibleParent) {
        if (possibleParent == -1)
            return null;
        for (Vertex v: possible)
            if (v.iWant.isEmpty()) return v;
        return null;
    }


    public static Vertex findToTake(Vertex[] possible) {
        for (Vertex v: possible)
            if (v.canBeTaken()) return v;
        return null;
    }

    public static boolean isCycle(Vertex vertex) {
        Stack<Vertex> stack = new Stack<>();
        stack.push(vertex);
        int cycleId = vertex.number;

        while (!stack.isEmpty()) {
            Vertex current = stack.pop();
            if (current.done)
                continue;
            if (current.cycleId == cycleId)
                return true;
            current.cycleId = cycleId;
            current.done = true;
            for (Vertex v: current.neighbours)
                stack.push(v);
        }
        return false;
    }


    public static class Vertex {

        List<Vertex> iWant = new ArrayList<>();
        List<Vertex> iDontWant = new ArrayList<>();
        List<Vertex> dontWantMe = new ArrayList<>();
        List<Vertex> wantMe = new ArrayList<>();

        int cycleId = -1;
        boolean done = false;

        int number;

        public Vertex(int number) {
            this.number = number;
        }

        List<Vertex> neighbours = new ArrayList<>();

        public void addNeighbour(Vertex neighbour) {
            neighbours.add(neighbour);
        }

        public void addiWant(Vertex neighbour) {
            iWant.add(neighbour);
        }

        public void addIDontWant(Vertex vertex) {
            iDontWant.add(vertex);
        }

        public void addDontWantMe(Vertex vertex) {
            dontWantMe.add(vertex);
        }

        public void addWantMe(Vertex vertex) {
            wantMe.add(vertex);
        }

        public void visit() {
            for (Vertex v: iDontWant) {
                v.dontWantMe.remove(this);
            }
            for (Vertex v: wantMe) {
                v.iWant.remove(this);
            }
        }

        public boolean canBeTaken() {
            return iWant.isEmpty() && dontWantMe.isEmpty();
        }
    }
}