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
#include <vector>
#include <math.h>
#include <string>
#include <iostream>
#include <sstream>
#include <limits>
#include <cstring>
#include <array>

//#define PA2016_TEST

#ifdef PA2016_TEST
#define ASSERT( condition ) if ( !( condition ) ) { __debugbreak(); }
#define PAUSE_CONSOLE system( "pause" );
#define DEBUG_ONLY( line ) line;
#else
#define ASSERT( condition )
#define PAUSE_CONSOLE 
#define DEBUG_ONLY( line )
#endif

typedef unsigned int uint;

struct SOpinion
{
	uint m_opinionGiverIdx;
	uint m_opinionTargetIdx;
};

struct SWorkerInfo
{
	SWorkerInfo() : m_bossIdx( -1 ), m_negativesCount( 0 ), m_positivesCount( 0 ) {}

	int m_bossIdx;
	uint m_negativesCount; // how many negative opinions this worker GOT.
	uint m_positivesCount; // how many positive opinions this worker GAVE.
};

typedef std::vector< SWorkerInfo > TWorkers;
typedef std::vector< SOpinion > TOpinions;

class Hierarchy
{
public:
	static const uint BOSS_IDX = 0;

	void Setup( uint maxWorkers, uint opinionCount )
	{
		m_workers.resize( maxWorkers + 1 ); // 0 idx is a dummy idx, it is easier to work input data.
		m_negativeOpinions.reserve( opinionCount / 2 );
		m_postiveOpinions.reserve( opinionCount / 2 );
	}

	void AddNegativeOpinion( uint giverIdx, uint targetIdx )
	{
		m_negativeOpinions.push_back( SOpinion{ giverIdx, targetIdx } );
		AddWokerOpinion( targetIdx, false );
	}

	void AddPositiveOpinion( uint giverIdx, uint targetIdx )
	{
		m_postiveOpinions.push_back( SOpinion{ giverIdx, targetIdx } );
		AddWokerOpinion( giverIdx, true );
	}

	bool FindAndSetDirector()
	{
		int dirIdx = -1;
		const uint size = m_workers.size();
		for( uint i = 1; i < size; ++i )
		{
			uint opinions = m_workers[ i ].m_negativesCount + m_workers[ i ].m_positivesCount;
			if( opinions == 0 )
			{
				dirIdx = i;
				break;
			}
		}

		if( dirIdx > 0 )
		{
			for( uint i = 1; i < size; ++i )
			{
				m_workers[ i ].m_bossIdx = dirIdx;
			}

			m_workers[ dirIdx ].m_bossIdx = BOSS_IDX;
			return true;
		}

		return false;
	}

	bool TryToBuildHierarchy()
	{
		bool succes = ProcessPositives();

		if( !succes )
			return false;

		return ProcessNegatives();
	}

	void PrintHierarchy()
	{
		const uint size = m_workers.size();
		for( uint i = 1; i < size; ++i )
		{
			std::cout << m_workers[ i ].m_bossIdx << "\n";
		}
	}

private:
	void AddWokerOpinion( uint workerIdx, bool isPositive )
	{
		isPositive ? ++m_workers[ workerIdx ].m_positivesCount : ++m_workers[ workerIdx ].m_negativesCount;
	}
	bool ProcessPositives()
	{
		for( const auto& positive : m_postiveOpinions )
		{
			if( !IsBoss( positive.m_opinionGiverIdx, positive.m_opinionTargetIdx, m_workers.size() ) )
			{
				m_workers[ positive.m_opinionGiverIdx ].m_bossIdx = positive.m_opinionTargetIdx;

				bool hasCycle = !IsBoss( positive.m_opinionGiverIdx, BOSS_IDX, m_workers.size() );

				if( hasCycle )
					return false;
			}
		}

		return true;
	}

	bool ProcessNegatives()
	{
		for( const auto& negative : m_negativeOpinions )
		{
			if( IsBoss( negative.m_opinionGiverIdx, negative.m_opinionTargetIdx, m_workers.size() ) )
			{
				// Initially every worker has only a director as a boss, it can be only changed by positive opinion,
				// so we have a collision.
				return false;
			}
		}

		return true;
	}

	bool IsBoss( uint workerIdx, uint potentialBossIdx, uint maxIterations )
	{
		uint currIdx = workerIdx;
		uint i = 0;
		do
		{
			uint workerBoss = m_workers[ currIdx ].m_bossIdx;
			if( workerBoss == potentialBossIdx )
				return true;

			if( workerBoss == -1 || workerBoss == BOSS_IDX )
				return false;

			currIdx = workerBoss;
			++i;

		} while( i < maxIterations );

		return false;

	}

	TWorkers m_workers;
	TOpinions m_negativeOpinions;
	TOpinions m_postiveOpinions;
};

void TestAll()
{
}

int main()
{
	DEBUG_ONLY( TestAll() );

	uint numOfWorkers = 0;
	std::cin >> numOfWorkers;

	uint numOfOpinions = 0;
	std::cin >> numOfOpinions;

	Hierarchy hierarchy;
	hierarchy.Setup( numOfWorkers, numOfOpinions );

	for( uint i = 0; i < numOfOpinions; ++i )
	{
		uint opinionGiverIdx = 0;
		std::cin >> opinionGiverIdx;

		uint opinionTargetIdx = 0;
		std::cin >> opinionTargetIdx;

		char opinionType = 0;
		std::cin >> opinionType;

		if( opinionType == 'T' )
			hierarchy.AddPositiveOpinion( opinionGiverIdx, opinionTargetIdx );
		else
			hierarchy.AddNegativeOpinion( opinionGiverIdx, opinionTargetIdx );
	}

	if( hierarchy.FindAndSetDirector() && hierarchy.TryToBuildHierarchy() )
	{
		hierarchy.PrintHierarchy();
	}
	else
	{
		std::cout << "NIE";
	}

	return 0;
}