#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <vector> #include <string> #include <map> #include <set> #include <cassert> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef long long ll; typedef pair<int,int> PII; const ll mod=1000000007; ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} // head const int N=10100; char op[4]; int n,m,vis[N],d[N],c[N],q[N],u[N],v[N],ty[N],T,par[N]; VI e[N],ss[N],cc[N],ff[N]; void solve(VI p,int f) { ++T; // for (auto v:p) printf("%d ",v); printf(": %d\n",f); for (auto v:p) vis[v]=T,d[v]=0,e[v].clear(),c[v]=0; rep(i,0,m) { if (vis[u[i]]==T&&vis[v[i]]==T&&ty[i]) { e[u[i]].pb(v[i]),e[v[i]].pb(u[i]); d[u[i]]=1; } } if (f==0) { int r=-1; rep(i,0,m) { if (vis[u[i]]==T&&vis[v[i]]==T&&ty[i]==0) { d[v[i]]=1; } } for (auto v:p) if (d[v]==0) { r=v; break; } if (r==-1) throw 0; par[r]=f; p.erase(find(all(p),r)); solve(p,r); return; } int Q=0; for (auto v:p) if (!c[v]) { ++Q; int t=0; q[t++]=v; c[v]=Q; rep(i,0,t) { int u=q[i]; for (auto w:e[u]) if (!c[w]) { c[w]=Q; q[t++]=w; } } cc[Q]=VI(q,q+t); } // for (auto v:p) printf("%d %d\n",v,c[v]); rep(i,0,m) if (vis[u[i]]==T&&vis[v[i]]==T&&ty[i]==0&&c[u[i]]==c[v[i]]) d[v[i]]=1; rep(i,1,Q+1) { int r=-1; for (auto v:cc[i]) if (d[v]==0) { r=v; break; } /* printf("Comp %d:",i); for (auto v:cc[i]) { printf(" %d",v); } puts(""); printf("%d %d\n",i,r);*/ if (r==-1) throw 0; par[r]=f; cc[i].erase(find(all(cc[i]),r)); ss[f].pb(r); ff[r]=cc[i]; } for (auto p:ss[f]) if (!ff[p].empty()) solve(ff[p],p); } int main() { // freopen("reo.in","r",stdin); // freopen("reo.out","w",stdout); scanf("%d%d",&n,&m); rep(i,0,m) { scanf("%d%d%s",u+i,v+i,op); ty[i]=op[0]=='T'; } memset(par,0,sizeof(par)); try { VI v; rep(i,1,n+1) v.pb(i); solve(v,0); } catch(int e) { puts("NIE"); return 0; } rep(i,1,n+1) printf("%d\n",par[i]); }
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 | #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <vector> #include <string> #include <map> #include <set> #include <cassert> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef long long ll; typedef pair<int,int> PII; const ll mod=1000000007; ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} // head const int N=10100; char op[4]; int n,m,vis[N],d[N],c[N],q[N],u[N],v[N],ty[N],T,par[N]; VI e[N],ss[N],cc[N],ff[N]; void solve(VI p,int f) { ++T; // for (auto v:p) printf("%d ",v); printf(": %d\n",f); for (auto v:p) vis[v]=T,d[v]=0,e[v].clear(),c[v]=0; rep(i,0,m) { if (vis[u[i]]==T&&vis[v[i]]==T&&ty[i]) { e[u[i]].pb(v[i]),e[v[i]].pb(u[i]); d[u[i]]=1; } } if (f==0) { int r=-1; rep(i,0,m) { if (vis[u[i]]==T&&vis[v[i]]==T&&ty[i]==0) { d[v[i]]=1; } } for (auto v:p) if (d[v]==0) { r=v; break; } if (r==-1) throw 0; par[r]=f; p.erase(find(all(p),r)); solve(p,r); return; } int Q=0; for (auto v:p) if (!c[v]) { ++Q; int t=0; q[t++]=v; c[v]=Q; rep(i,0,t) { int u=q[i]; for (auto w:e[u]) if (!c[w]) { c[w]=Q; q[t++]=w; } } cc[Q]=VI(q,q+t); } // for (auto v:p) printf("%d %d\n",v,c[v]); rep(i,0,m) if (vis[u[i]]==T&&vis[v[i]]==T&&ty[i]==0&&c[u[i]]==c[v[i]]) d[v[i]]=1; rep(i,1,Q+1) { int r=-1; for (auto v:cc[i]) if (d[v]==0) { r=v; break; } /* printf("Comp %d:",i); for (auto v:cc[i]) { printf(" %d",v); } puts(""); printf("%d %d\n",i,r);*/ if (r==-1) throw 0; par[r]=f; cc[i].erase(find(all(cc[i]),r)); ss[f].pb(r); ff[r]=cc[i]; } for (auto p:ss[f]) if (!ff[p].empty()) solve(ff[p],p); } int main() { // freopen("reo.in","r",stdin); // freopen("reo.out","w",stdout); scanf("%d%d",&n,&m); rep(i,0,m) { scanf("%d%d%s",u+i,v+i,op); ty[i]=op[0]=='T'; } memset(par,0,sizeof(par)); try { VI v; rep(i,1,n+1) v.pb(i); solve(v,0); } catch(int e) { puts("NIE"); return 0; } rep(i,1,n+1) printf("%d\n",par[i]); } |