#include <algorithm> #include <cstdio> #include <cstdlib> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <vector> #include <cmath> #include <cstring> #include <string> #include <iostream> #include <complex> #include <sstream> using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef long double LD; typedef vector<int> VI; typedef pair<int,int> PII; #define REP(i,n) for(int i=0;i<(n);++i) #define SIZE(c) ((int)((c).size())) #define FOR(i,a,b) for (int i=(a); i<(b); ++i) #define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i) #define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i) #define ALL(v) (v).begin(), (v).end() #define pb push_back #define mp make_pair #define st first #define nd second vector<int> want[3000], dont_want[3000], is_wanted_by[3000]; int subset_id[3000]; void mark(int v, int current_sid, int new_sid) { if (subset_id[v] != current_sid) return; subset_id[v] = new_sid; for (auto& u: want[v]) mark(u, current_sid, new_sid); for (auto& u: is_wanted_by[v]) mark(u, current_sid, new_sid); } int parent[3000]; bool wants[3000]; bool is_unwanted[3000]; int N; int NEXT_SID = 0; int go(int SID) { REP(i,N) is_unwanted[i] = wants[i] = false; REP(i,N) { if (subset_id[i] != SID) continue; for (auto& v: want[i]) if (subset_id[v] == SID) wants[i] = true; for (auto& v: dont_want[i]) if (subset_id[v] == SID) is_unwanted[v] = true; } int root = -1; REP(i,N) if (subset_id[i] == SID && !wants[i] && !is_unwanted[i]) root = i; if (root == -1) return -1; subset_id[root] = -1; REP(i,N) if (subset_id[i] == SID) { ++NEXT_SID; mark(i, SID, NEXT_SID); int child = go(NEXT_SID); if (child == -1) return -1; parent[child] = root; } return root; } int main() { ios_base::sync_with_stdio(0); int M; scanf("%d%d", &N, &M); REP(i,M) { int a, b; char c[2]; scanf("%d%d%1s", &a, &b, c); --a, --b; if (c[0] == 'T') { want[a].pb(b); is_wanted_by[b].pb(a); } else { dont_want[a].pb(b); } } int root = go(0); if (root == -1) { printf("NIE\n"); return 0; } parent[root] = -1; REP(i,N) printf("%d\n", parent[i] + 1); }
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 | #include <algorithm> #include <cstdio> #include <cstdlib> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <vector> #include <cmath> #include <cstring> #include <string> #include <iostream> #include <complex> #include <sstream> using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef long double LD; typedef vector<int> VI; typedef pair<int,int> PII; #define REP(i,n) for(int i=0;i<(n);++i) #define SIZE(c) ((int)((c).size())) #define FOR(i,a,b) for (int i=(a); i<(b); ++i) #define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i) #define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i) #define ALL(v) (v).begin(), (v).end() #define pb push_back #define mp make_pair #define st first #define nd second vector<int> want[3000], dont_want[3000], is_wanted_by[3000]; int subset_id[3000]; void mark(int v, int current_sid, int new_sid) { if (subset_id[v] != current_sid) return; subset_id[v] = new_sid; for (auto& u: want[v]) mark(u, current_sid, new_sid); for (auto& u: is_wanted_by[v]) mark(u, current_sid, new_sid); } int parent[3000]; bool wants[3000]; bool is_unwanted[3000]; int N; int NEXT_SID = 0; int go(int SID) { REP(i,N) is_unwanted[i] = wants[i] = false; REP(i,N) { if (subset_id[i] != SID) continue; for (auto& v: want[i]) if (subset_id[v] == SID) wants[i] = true; for (auto& v: dont_want[i]) if (subset_id[v] == SID) is_unwanted[v] = true; } int root = -1; REP(i,N) if (subset_id[i] == SID && !wants[i] && !is_unwanted[i]) root = i; if (root == -1) return -1; subset_id[root] = -1; REP(i,N) if (subset_id[i] == SID) { ++NEXT_SID; mark(i, SID, NEXT_SID); int child = go(NEXT_SID); if (child == -1) return -1; parent[child] = root; } return root; } int main() { ios_base::sync_with_stdio(0); int M; scanf("%d%d", &N, &M); REP(i,M) { int a, b; char c[2]; scanf("%d%d%1s", &a, &b, c); --a, --b; if (c[0] == 'T') { want[a].pb(b); is_wanted_by[b].pb(a); } else { dont_want[a].pb(b); } } int root = go(0); if (root == -1) { printf("NIE\n"); return 0; } parent[root] = -1; REP(i,N) printf("%d\n", parent[i] + 1); } |