//============================================================================ // Name : reo.cpp // Author : // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <iostream> #include <vector> #include <deque> #include <utility> #include <algorithm> #include <set> using namespace std; int ide=0; class worker{ public: short no; short parent; short id; set<short> parids; set<short> cannot; deque<short> needs; deque<short> sub; worker(){ no=0; parent=-1; id=ide; ide++; } }; vector<worker> wrks; deque<pair<worker*,worker*> > yespair; deque<pair<worker*,worker*> > nopair; set<short> leftwrks; set<int> uncompatdir; bool compar(short val1,short val2){ return wrks[val1].no>wrks[val2].no; } /*void fill(worker* par){ par->parids.insert(par->id); for (set<short>::iterator it = leftwrks.begin(); it != leftwrks.end(); ) { cout<<*it<<" "; bool notfound=false; for(set<short>::iterator ite=wrks[*it].needs.begin();ite!=wrks[*it].needs.end();ite++){ if(par->parids.find(*ite)==par->parids.end()){ notfound=true; break; } } if(notfound){ it++; } else{ par->sub.push_back(*it); wrks[*it].parent=par->id; wrks[*it].parids=set<short>(par->parids); it = leftwrks.erase(it); } } for(deque<short>::iterator it=par->sub.begin();it!=par->sub.end();it++){ fill(&wrks[*it]); } }*/ bool broken=false; void push_parent(worker* wrk, worker* par){ //cout<<wrk->id<<"-"<<par->id<<" "; if(par==wrk || par->parent==wrk->id){ broken=true; return; } worker* parpar=par; while(par->parent!=-1){ } par->cannot.insert(wrk->cannot.begin(), wrk->cannot.end()); if(wrk->parent==-1){ wrk->parent=par->id; } else{ push_parent(&wrks[wrk->parent],par); } } int main() { ios_base::sync_with_stdio(0); int wc; int rc; cin>>wc; cin>>rc; wrks=vector<worker>(wc); worker* boss; for(int i=0;i<wc;i++){ wrks[i].id=i; leftwrks.insert(i); } for(int i=0;i<rc;i++){ short firstid; short secid; char comp; cin>>firstid; cin>>secid; cin>>comp; //cout<<i<<"m"<<firstid; if(comp=='T'){ yespair.push_back(pair<worker*,worker*>(&wrks[firstid-1],&wrks[secid-1])); //push_parent(&wrks[firstid-1],&wrks[secid-1]); uncompatdir.insert(firstid-1); wrks[firstid-1].needs.push_back(secid-1); } else{ nopair.push_back(pair<worker*,worker*>(&wrks[firstid-1],&wrks[secid-1])); wrks[secid-1].no++; wrks[firstid-1].cannot.insert(secid-1); uncompatdir.insert(secid-1); } } if(uncompatdir.size()==wrks.size()){ cout<<"NIE"; return 0; } /*for(deque<pair<worker*,worker*>>::iterator it=nopair.begin();it!=nopair.end();it++){ }*/ for(int i=0;i<wc;i++){ //cout<<i<<" "; sort(wrks[i].needs.begin(),wrks[i].needs.end(),compar); for(deque<short>::iterator it=wrks[i].needs.begin();it!=wrks[i].needs.end();it++){ push_parent(&wrks[i],&wrks[*it]); if(broken){ cout<<"NIE"; return 0; } } } /*for(set<int>::iterator it=uncompatdir.begin();it!=uncompatdir.end();it++){ cout<<(*it)<<" "<<wrks[*it].id; }*/ for(int i=0;i<=wc;i++){ if(uncompatdir.find(i)==uncompatdir.end()){ boss=&wrks[i]; for(int j=0;j<=wc;j++){ if(j!=i&&wrks[j].parent==-1){ wrks[j].parent=i; } } //leftwrks.erase(i); break; } } for(int i=0;i<wc;i++){ cout<<(wrks[i].parent+1)<<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 | //============================================================================ // Name : reo.cpp // Author : // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <iostream> #include <vector> #include <deque> #include <utility> #include <algorithm> #include <set> using namespace std; int ide=0; class worker{ public: short no; short parent; short id; set<short> parids; set<short> cannot; deque<short> needs; deque<short> sub; worker(){ no=0; parent=-1; id=ide; ide++; } }; vector<worker> wrks; deque<pair<worker*,worker*> > yespair; deque<pair<worker*,worker*> > nopair; set<short> leftwrks; set<int> uncompatdir; bool compar(short val1,short val2){ return wrks[val1].no>wrks[val2].no; } /*void fill(worker* par){ par->parids.insert(par->id); for (set<short>::iterator it = leftwrks.begin(); it != leftwrks.end(); ) { cout<<*it<<" "; bool notfound=false; for(set<short>::iterator ite=wrks[*it].needs.begin();ite!=wrks[*it].needs.end();ite++){ if(par->parids.find(*ite)==par->parids.end()){ notfound=true; break; } } if(notfound){ it++; } else{ par->sub.push_back(*it); wrks[*it].parent=par->id; wrks[*it].parids=set<short>(par->parids); it = leftwrks.erase(it); } } for(deque<short>::iterator it=par->sub.begin();it!=par->sub.end();it++){ fill(&wrks[*it]); } }*/ bool broken=false; void push_parent(worker* wrk, worker* par){ //cout<<wrk->id<<"-"<<par->id<<" "; if(par==wrk || par->parent==wrk->id){ broken=true; return; } worker* parpar=par; while(par->parent!=-1){ } par->cannot.insert(wrk->cannot.begin(), wrk->cannot.end()); if(wrk->parent==-1){ wrk->parent=par->id; } else{ push_parent(&wrks[wrk->parent],par); } } int main() { ios_base::sync_with_stdio(0); int wc; int rc; cin>>wc; cin>>rc; wrks=vector<worker>(wc); worker* boss; for(int i=0;i<wc;i++){ wrks[i].id=i; leftwrks.insert(i); } for(int i=0;i<rc;i++){ short firstid; short secid; char comp; cin>>firstid; cin>>secid; cin>>comp; //cout<<i<<"m"<<firstid; if(comp=='T'){ yespair.push_back(pair<worker*,worker*>(&wrks[firstid-1],&wrks[secid-1])); //push_parent(&wrks[firstid-1],&wrks[secid-1]); uncompatdir.insert(firstid-1); wrks[firstid-1].needs.push_back(secid-1); } else{ nopair.push_back(pair<worker*,worker*>(&wrks[firstid-1],&wrks[secid-1])); wrks[secid-1].no++; wrks[firstid-1].cannot.insert(secid-1); uncompatdir.insert(secid-1); } } if(uncompatdir.size()==wrks.size()){ cout<<"NIE"; return 0; } /*for(deque<pair<worker*,worker*>>::iterator it=nopair.begin();it!=nopair.end();it++){ }*/ for(int i=0;i<wc;i++){ //cout<<i<<" "; sort(wrks[i].needs.begin(),wrks[i].needs.end(),compar); for(deque<short>::iterator it=wrks[i].needs.begin();it!=wrks[i].needs.end();it++){ push_parent(&wrks[i],&wrks[*it]); if(broken){ cout<<"NIE"; return 0; } } } /*for(set<int>::iterator it=uncompatdir.begin();it!=uncompatdir.end();it++){ cout<<(*it)<<" "<<wrks[*it].id; }*/ for(int i=0;i<=wc;i++){ if(uncompatdir.find(i)==uncompatdir.end()){ boss=&wrks[i]; for(int j=0;j<=wc;j++){ if(j!=i&&wrks[j].parent==-1){ wrks[j].parent=i; } } //leftwrks.erase(i); break; } } for(int i=0;i<wc;i++){ cout<<(wrks[i].parent+1)<<endl; } return 0; } |