#include<bits/stdc++.h> #define ll long long #define boost ios_base::sync_with_stdio(false);cin.tie(0);cout.precision(20); #define inf 1000000000 #define INF 1000000000000000000LL #define mod 1000000007 #define VI vector<int> #define PII pair<int, int> #define st first #define nd second #define pb push_back #define mp make_pair #define endl '\n' #define REP(x, y) for(int x = 0; x < (y); ++x) #define FOR(x, y, z) for(int x = y; x <= (z); ++x) #define FORR(x, y, z) for(int x = y; x >= (z); --x) using namespace std; #define N 10007 int tab[10]; bool czy; VI g[N]; int h[N]; int par[N]; vector <pair <PII,char> > query; int n,m,v1,v2,node; char c; void clear() { FOR(i,1,n) { h[i]=0; par[i]=0; g[i].clear(); } } void dfs(int v,int p) { if(par + 1) h[v] = h[p] + 1; par[v] = p; for(auto u : g[v]) if(p - u) dfs(u,v); } int lca(int v,int u) { if(v == u) return v; if(h[v] < h[u]) swap(v,u); return lca(par[v], u); } bool check() { clear(); FOR(i,1,n) { if (tab[i]==-1) { node=i; continue; } g[tab[i]].pb(i); g[i].pb(tab[i]); } dfs(node,-1); FOR(i,1,n) if (par[i]==0) return false; FOR(i,0,m-1) { int k=lca( query[i].st.st,query[i].st.nd ); if ( query[i].nd=='T' ) { if (k==query[i].st.nd) { continue; } else { return false; } } else { if (k!=query[i].st.nd) { continue; } else { return false; } } } return true; } void gen(int x,int n) { if (czy) return; if (x==n) { if(check()) { FOR(i,1,n) if (tab[i]==-1) cout << 0 << endl; else cout << tab[i] << endl; czy=true; } return; } FOR(i,1,n) { if (tab[i]==0) { FOR(j,1,n-1) { if (czy) break; if (i==j) continue; tab[i]=j; gen(x+1,n); } tab[i]=0; break; } if (czy) break; } return; } int main() { boost cin >> n >> m; if (n>7) { cout << "NIE" << endl; return 0; } FOR(i,1,m) { cin >> v1 >> v2 >> c; query.pb(mp(mp(v1,v2),c)); } FOR(i,1,n) { if (czy) break; tab[i]=-1; gen(1,n); tab[i]=0; } if (!czy) cout << "NIE" << 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 | #include<bits/stdc++.h> #define ll long long #define boost ios_base::sync_with_stdio(false);cin.tie(0);cout.precision(20); #define inf 1000000000 #define INF 1000000000000000000LL #define mod 1000000007 #define VI vector<int> #define PII pair<int, int> #define st first #define nd second #define pb push_back #define mp make_pair #define endl '\n' #define REP(x, y) for(int x = 0; x < (y); ++x) #define FOR(x, y, z) for(int x = y; x <= (z); ++x) #define FORR(x, y, z) for(int x = y; x >= (z); --x) using namespace std; #define N 10007 int tab[10]; bool czy; VI g[N]; int h[N]; int par[N]; vector <pair <PII,char> > query; int n,m,v1,v2,node; char c; void clear() { FOR(i,1,n) { h[i]=0; par[i]=0; g[i].clear(); } } void dfs(int v,int p) { if(par + 1) h[v] = h[p] + 1; par[v] = p; for(auto u : g[v]) if(p - u) dfs(u,v); } int lca(int v,int u) { if(v == u) return v; if(h[v] < h[u]) swap(v,u); return lca(par[v], u); } bool check() { clear(); FOR(i,1,n) { if (tab[i]==-1) { node=i; continue; } g[tab[i]].pb(i); g[i].pb(tab[i]); } dfs(node,-1); FOR(i,1,n) if (par[i]==0) return false; FOR(i,0,m-1) { int k=lca( query[i].st.st,query[i].st.nd ); if ( query[i].nd=='T' ) { if (k==query[i].st.nd) { continue; } else { return false; } } else { if (k!=query[i].st.nd) { continue; } else { return false; } } } return true; } void gen(int x,int n) { if (czy) return; if (x==n) { if(check()) { FOR(i,1,n) if (tab[i]==-1) cout << 0 << endl; else cout << tab[i] << endl; czy=true; } return; } FOR(i,1,n) { if (tab[i]==0) { FOR(j,1,n-1) { if (czy) break; if (i==j) continue; tab[i]=j; gen(x+1,n); } tab[i]=0; break; } if (czy) break; } return; } int main() { boost cin >> n >> m; if (n>7) { cout << "NIE" << endl; return 0; } FOR(i,1,m) { cin >> v1 >> v2 >> c; query.pb(mp(mp(v1,v2),c)); } FOR(i,1,n) { if (czy) break; tab[i]=-1; gen(1,n); tab[i]=0; } if (!czy) cout << "NIE" << endl; return 0; } |