#include<bits/stdc++.h> #define endl '\n' using namespace std; vector< int > tab[1010], tree[1010], must[1010], cant[1010], cp, rmust[1010]; vector< int > order; stack< int > S; pair< int, pair< int, char > > input[10010]; int n, m, x, y, tim, t, in[1010], out[1010], t1[1010], t2[1010], v, start; bool pos[1010][1010], blad; int met[1010]; int ans[1010]; char znak; void dfs2( int w ) { in[w] = ++tim; for( int a = 0; a < tree[w].size(); a++ ) { ans[tree[w][a]] = w; dfs2( tree[w][a] ); } out[w] = ++tim; } void dfs( int w ) { //cout<<"enter "<<w<<endl; met[w] = ++t; if( tab[w].size() == 0 )return; // for( int a = 0; a < tab[w].size(); a++ )cout<<tab[w][a]<<" "; // cout<<endl; for( int a = 0; a < tab[w].size(); a++ ) { if( pos[w][tab[w][a]] )S.push( tab[w][a] ); else continue; while( !S.empty() ) { x = S.top(); //cout<<"inside "<<x<<endl; cp.push_back( x ); pos[w][x] = 0; S.pop(); for( int b = 0; b < must[x].size(); b++ ) { if( pos[w][must[x][b]] ) { S.push( must[x][b] ); t1[x]++; } } for( int b = 0; b < rmust[x].size(); b++ ) { //cout<<" "<<rmust[x][b]<<endl; if( pos[w][rmust[x][b]] ) { S.push( rmust[x][b] ); t1[rmust[x][b]]++; } } } v = 0; random_shuffle( cp.begin(), cp.end() ); for( int b = 0; b < cp.size(); b++ ) { if( t1[cp[b]] == 0 )v = cp[b]; t1[cp[b]] = 0; } //cout<<"chosen "<<w<<" "<<v<<endl; if( !v ) { blad = 1; return; } if( v )tree[w].push_back( v ); while( !cp.empty() ) { if( cp.back() != v )tab[v].push_back( cp.back() ), pos[v][cp.back()] = 1; cp.pop_back(); } } for( int a = 0; a < tree[w].size(); a++ )dfs( tree[w][a] ); } void czysc() { for( int a = 1; a <= n; a++ ) { must[a].clear(); rmust[a].clear(); cant[a].clear(); tab[a].clear(); t = 0; t1[a] = 0; t2[a] = 0; ans[a] = 0; tree[a].clear(); } for( int a = 1; a <= n; a++ )for( int b = 1; b <= n; b++ )pos[a][b] = 0; cp.clear(); tim = 0; } int main() { ios_base::sync_with_stdio( 0 ); cin.tie( 0 ); srand( time( 0 ) ); cin>>n>>m; for( int a = 1; a <= m; a++ ) { cin>>input[a].first>>input[a].second.first>>input[a].second.second; } for( int licz = 1; licz <= 5; licz++ ) { for( int a = 1; a <= m; a++ ) { x = input[a].first; y = input[a].second.first; znak = input[a].second.second; if( znak == 'T' ) { must[x].push_back( y ); rmust[y].push_back( x ); t1[x] = 1; } else { cant[x].push_back( y ); t2[y] = 1; } } start = 0; order.clear(); for( int a = 1; a <= n; a++ )order.push_back( a ); random_shuffle( order.begin(), order.end() ); for( int iter = 0; iter < order.size(); iter++ ) { if( t1[order[iter]] == 0 && t2[order[iter]] == 0 ) { start = order[iter]; break; } } for( int a = 1; a <= n; a++ )t1[a] = 0, t2[a] = 0; if( !start ) { cout<<"NIE"; return 0; } for( int a = 1; a <= n; a++ )if( a != start )tab[start].push_back( a ), pos[start][a] = 1; //cout<<"ok "<<start<<endl; dfs( start ); ++t; if( !blad ) { dfs2( start ); //cout<<"ok ok"<<endl; for( int a = 1; a <= n; a++ ) { for( int b = 0; b < must[a].size(); b++ ) { if( in[must[a][b]] > in[a] || out[must[a][b]] < out[a] )blad = 1; } for( int b = 0; b < cant[a].size(); b++ ) { if( in[cant[a][b]] < in[a] && out[cant[a][b]] > out[a] )blad = 1; } } } if( !blad )break; czysc(); } if( blad ) { cout<<"NIE"; return 0; } else { //cout<<"TAK"; //cout<<endl<<endl; for( int a = 1; a <= n; a++ )cout<<ans[a]<<endl; } return 0; }
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 | #include<bits/stdc++.h> #define endl '\n' using namespace std; vector< int > tab[1010], tree[1010], must[1010], cant[1010], cp, rmust[1010]; vector< int > order; stack< int > S; pair< int, pair< int, char > > input[10010]; int n, m, x, y, tim, t, in[1010], out[1010], t1[1010], t2[1010], v, start; bool pos[1010][1010], blad; int met[1010]; int ans[1010]; char znak; void dfs2( int w ) { in[w] = ++tim; for( int a = 0; a < tree[w].size(); a++ ) { ans[tree[w][a]] = w; dfs2( tree[w][a] ); } out[w] = ++tim; } void dfs( int w ) { //cout<<"enter "<<w<<endl; met[w] = ++t; if( tab[w].size() == 0 )return; // for( int a = 0; a < tab[w].size(); a++ )cout<<tab[w][a]<<" "; // cout<<endl; for( int a = 0; a < tab[w].size(); a++ ) { if( pos[w][tab[w][a]] )S.push( tab[w][a] ); else continue; while( !S.empty() ) { x = S.top(); //cout<<"inside "<<x<<endl; cp.push_back( x ); pos[w][x] = 0; S.pop(); for( int b = 0; b < must[x].size(); b++ ) { if( pos[w][must[x][b]] ) { S.push( must[x][b] ); t1[x]++; } } for( int b = 0; b < rmust[x].size(); b++ ) { //cout<<" "<<rmust[x][b]<<endl; if( pos[w][rmust[x][b]] ) { S.push( rmust[x][b] ); t1[rmust[x][b]]++; } } } v = 0; random_shuffle( cp.begin(), cp.end() ); for( int b = 0; b < cp.size(); b++ ) { if( t1[cp[b]] == 0 )v = cp[b]; t1[cp[b]] = 0; } //cout<<"chosen "<<w<<" "<<v<<endl; if( !v ) { blad = 1; return; } if( v )tree[w].push_back( v ); while( !cp.empty() ) { if( cp.back() != v )tab[v].push_back( cp.back() ), pos[v][cp.back()] = 1; cp.pop_back(); } } for( int a = 0; a < tree[w].size(); a++ )dfs( tree[w][a] ); } void czysc() { for( int a = 1; a <= n; a++ ) { must[a].clear(); rmust[a].clear(); cant[a].clear(); tab[a].clear(); t = 0; t1[a] = 0; t2[a] = 0; ans[a] = 0; tree[a].clear(); } for( int a = 1; a <= n; a++ )for( int b = 1; b <= n; b++ )pos[a][b] = 0; cp.clear(); tim = 0; } int main() { ios_base::sync_with_stdio( 0 ); cin.tie( 0 ); srand( time( 0 ) ); cin>>n>>m; for( int a = 1; a <= m; a++ ) { cin>>input[a].first>>input[a].second.first>>input[a].second.second; } for( int licz = 1; licz <= 5; licz++ ) { for( int a = 1; a <= m; a++ ) { x = input[a].first; y = input[a].second.first; znak = input[a].second.second; if( znak == 'T' ) { must[x].push_back( y ); rmust[y].push_back( x ); t1[x] = 1; } else { cant[x].push_back( y ); t2[y] = 1; } } start = 0; order.clear(); for( int a = 1; a <= n; a++ )order.push_back( a ); random_shuffle( order.begin(), order.end() ); for( int iter = 0; iter < order.size(); iter++ ) { if( t1[order[iter]] == 0 && t2[order[iter]] == 0 ) { start = order[iter]; break; } } for( int a = 1; a <= n; a++ )t1[a] = 0, t2[a] = 0; if( !start ) { cout<<"NIE"; return 0; } for( int a = 1; a <= n; a++ )if( a != start )tab[start].push_back( a ), pos[start][a] = 1; //cout<<"ok "<<start<<endl; dfs( start ); ++t; if( !blad ) { dfs2( start ); //cout<<"ok ok"<<endl; for( int a = 1; a <= n; a++ ) { for( int b = 0; b < must[a].size(); b++ ) { if( in[must[a][b]] > in[a] || out[must[a][b]] < out[a] )blad = 1; } for( int b = 0; b < cant[a].size(); b++ ) { if( in[cant[a][b]] < in[a] && out[cant[a][b]] > out[a] )blad = 1; } } } if( !blad )break; czysc(); } if( blad ) { cout<<"NIE"; return 0; } else { //cout<<"TAK"; //cout<<endl<<endl; for( int a = 1; a <= n; a++ )cout<<ans[a]<<endl; } return 0; } |