#include <iostream> #include <cstdio> #include <vector> #include <cassert> using namespace std; #define M 1000 void MakeSet(int n); int Find(int x); void Union(int x, int y); struct {int parent,rank;} tab[M+1]; vector<pair<int,int> > nie; vector<vector<int> > G,sorted; vector<int> used,root,answer; int Root; bool DFS(int v) { used[v]=1; for (int i=0;i<G[v].size();i++) { int u=G[v][i]; if (used[u]==1) return false; if (used[u]==0) { bool dfs=DFS(u); if (!dfs) return false; } } used[v]=2; sorted[Find(v)].push_back(v); return true; } int main() { int n,m; cin >> n >> m; MakeSet(n); G.resize(n+1); root.resize(n+1,1); while (m--) { int a,b; string c; cin >> a >> b >> c; if (c=="T") { Union(a,b); G[a].push_back(b); root[a]=0; } else { nie.push_back(make_pair(a,b)); root[b]=0; } } for (int i=1;i<=n;i++) if (root[i]) { Root=i; break; } if (Root==0) { cout << "NIE"; return 0; } //cout << "Root="<<Root << endl; sorted.resize(n+1); used.resize(n+1,0); answer.resize(n+1); for (int i=0;i<nie.size();i++) { int a=nie[i].first,b=nie[i].second; if (Find(a)==Find(b)) G[b].push_back(a); } /* for (int i=1;i<=n;i++) cout << i << " w " << Find(i) << endl; for (int i=1;i<=n;i++) { cout << i <<": "; for (int j=0;j<G[i].size();j++) cout << G[i][j]<<","; cout << endl; }*/ for (int v=1;v<=n;v++) if (!used[v]) { bool dfs=DFS(v); if (!dfs) { cout << "NIE"; return 0; } } for (int i=1;i<=n;i++) if (!sorted[i].empty()) { //cout << i; for (int j=0;j<sorted[i].size();j++) cout << " <-" << sorted[i][j] ; cout << endl; answer[sorted[i][0]]=Root; for (int j=1;j<sorted[i].size();j++) answer[sorted[i][j]]=sorted[i][j-1]; } answer[Root]=0; for (int i=1;i<=n;i++) cout << answer[i]<< endl; //cout << "TAK"; return 0; } void MakeSet(int n) { //cout << x << endl; for (int x=1;x<=n;x++) { tab[x].parent = x; tab[x].rank = 0; } } int Find(int x) { if (tab[x].parent == x) { return x; } else { tab[x].parent =Find(tab[x].parent); return tab[x].parent; } } void Union(int x, int y){ //cout << "union "<< x << " " << y << endl; int xRoot = Find(x); int yRoot = Find(y); //cout << "xroot= "<< x << " yroot=" << y << endl; if (tab[xRoot].rank > tab[yRoot].rank) tab[yRoot].parent = xRoot; else if (tab[xRoot].rank < tab[yRoot].rank) tab[xRoot].parent = yRoot; else if (xRoot != yRoot) { tab[yRoot].parent = xRoot; tab[xRoot].rank++; } }
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 | #include <iostream> #include <cstdio> #include <vector> #include <cassert> using namespace std; #define M 1000 void MakeSet(int n); int Find(int x); void Union(int x, int y); struct {int parent,rank;} tab[M+1]; vector<pair<int,int> > nie; vector<vector<int> > G,sorted; vector<int> used,root,answer; int Root; bool DFS(int v) { used[v]=1; for (int i=0;i<G[v].size();i++) { int u=G[v][i]; if (used[u]==1) return false; if (used[u]==0) { bool dfs=DFS(u); if (!dfs) return false; } } used[v]=2; sorted[Find(v)].push_back(v); return true; } int main() { int n,m; cin >> n >> m; MakeSet(n); G.resize(n+1); root.resize(n+1,1); while (m--) { int a,b; string c; cin >> a >> b >> c; if (c=="T") { Union(a,b); G[a].push_back(b); root[a]=0; } else { nie.push_back(make_pair(a,b)); root[b]=0; } } for (int i=1;i<=n;i++) if (root[i]) { Root=i; break; } if (Root==0) { cout << "NIE"; return 0; } //cout << "Root="<<Root << endl; sorted.resize(n+1); used.resize(n+1,0); answer.resize(n+1); for (int i=0;i<nie.size();i++) { int a=nie[i].first,b=nie[i].second; if (Find(a)==Find(b)) G[b].push_back(a); } /* for (int i=1;i<=n;i++) cout << i << " w " << Find(i) << endl; for (int i=1;i<=n;i++) { cout << i <<": "; for (int j=0;j<G[i].size();j++) cout << G[i][j]<<","; cout << endl; }*/ for (int v=1;v<=n;v++) if (!used[v]) { bool dfs=DFS(v); if (!dfs) { cout << "NIE"; return 0; } } for (int i=1;i<=n;i++) if (!sorted[i].empty()) { //cout << i; for (int j=0;j<sorted[i].size();j++) cout << " <-" << sorted[i][j] ; cout << endl; answer[sorted[i][0]]=Root; for (int j=1;j<sorted[i].size();j++) answer[sorted[i][j]]=sorted[i][j-1]; } answer[Root]=0; for (int i=1;i<=n;i++) cout << answer[i]<< endl; //cout << "TAK"; return 0; } void MakeSet(int n) { //cout << x << endl; for (int x=1;x<=n;x++) { tab[x].parent = x; tab[x].rank = 0; } } int Find(int x) { if (tab[x].parent == x) { return x; } else { tab[x].parent =Find(tab[x].parent); return tab[x].parent; } } void Union(int x, int y){ //cout << "union "<< x << " " << y << endl; int xRoot = Find(x); int yRoot = Find(y); //cout << "xroot= "<< x << " yroot=" << y << endl; if (tab[xRoot].rank > tab[yRoot].rank) tab[yRoot].parent = xRoot; else if (tab[xRoot].rank < tab[yRoot].rank) tab[xRoot].parent = yRoot; else if (xRoot != yRoot) { tab[yRoot].parent = xRoot; tab[xRoot].rank++; } } |