#include <iostream> #include <algorithm> #include <vector> #include <string> #include <map> #include <set> #include <queue> #include <stack> #include <cmath> #include <cassert> #include <cstring> #include <bitset> #include <sstream> //#include <bits/stdc++.h> #include <ext/numeric> using namespace std ; using namespace __gnu_cxx ; typedef long long LL ; typedef pair<int,int> PII ; typedef vector<int> VI ; const int INF = 1e9+100 ; const LL INFLL = (LL)INF*INF ; #define REP(i,n) for(int i=0;i<(n);++i) #define ALL(c) c.begin(),c.end() #define FOREACH(i,c) for(auto i=(c).begin();i!=(c).end();++i) #define CLEAR(t) memset(t,0,sizeof(t)) #define PB push_back #define MP make_pair #define FI first #define SE second template<class T1,class T2> ostream& operator<<(ostream &s, const pair<T1,T2> &x) { return s << "(" << x.FI << "," << x.SE << ")" ;} template<class T> ostream& operator<<(ostream &s, const vector<T> &t) { FOREACH(it, t) s << *it << " " ; return s ; } template<class T> ostream& operator<<(ostream &s, const set<T> &t) { FOREACH(it, t) s << *it << " " ; return s ; } template<class T1,class T2> ostream& operator<<(ostream &s, const map<T1, T2> &t) { FOREACH(it, t) s << *it << " " ; return s ; } template<class T1, class T2> T1 conv(const T2 &x) { stringstream sss ; sss << x ; T1 y ; sss >> y ; return y ; } template<class T> void _debug(bool printName, const char* s, const T &x) { if(printName) cerr << s << "=" ; cerr << x << endl ; } void _debug(bool, const char* s, const char*) { for(;*s;s++) if(*s!='"') { cerr << *s ; } cerr << endl ; } template<class T, class... R> void _debug(bool printName, const char* s, const T &x, R... y) { bool o=0, str=(*s=='"') ; for(; o || *s!=',' ; s++) if(*s=='"') o=!o ; else if(printName||str) cerr<<*s ; for(s++;*s && *s==' '; s++) ; if(!str) { if(printName) cerr << "=" ; cerr << x ; if(*s!='"' && printName) cerr << "," ; } cerr << " " ; _debug(printName, s, y...) ; } template<class T> void _coord(const T &x) { cerr << "[" << x << "]" ; } template<class T, class... R> void _coord(const T &x, R... y) { cerr << "[" << x << "]" ; _coord(y...) ; } template<class T, class I> void _val(T &t, const I &i) { cerr << t[i] ; } template<class T, class I, class... J> void _val(T &t, const I &i, J... j) { _val(t[i], j...) ; } #ifndef LOCAL #define CERR(...) #define DEBUG(...) #define DARRAY(...) #define DFLAG 0 #else #define CERR(...) if(DFLAG) _debug(0, #__VA_ARGS__, __VA_ARGS__) #define DEBUG(...) if(DFLAG) _debug(1, #__VA_ARGS__, __VA_ARGS__) #define DARRAY(t,...) if(DFLAG) { cerr << #t ; _coord(__VA_ARGS__) ; cerr << " = " ; _val(t,__VA_ARGS__) ; cerr << endl ; } #define DFLAG 0 #endif //////////////////////////////////////////////////////////////////////////////// const int MAXN = 1100 ; set<int> likes[MAXN] ; // kogo lubimy VI revLikes[MAXN] ; // kto nas lubi VI hates[MAXN] ; // kogo nienawidzimy int haters[MAXN] ; // ilu nas nienawidzi int groups[MAXN] ; // kolorowanie grafu (wyznaczajace podzial firmy) int nextGroupId=1 ; bool ERROR=false ; int ret[MAXN] ; // wynik VI newGroup(int u, int newGroupId) { VI ret = {u} ; groups[u] = newGroupId ; for(int i=0 ; i<ret.size() ; i++) { u = ret[i] ; FOREACH(it, revLikes[u]) if(groups[*it]!=newGroupId) { ret.PB(*it) ; groups[*it] = newGroupId ; } FOREACH(it, likes[u]) if(groups[*it]!=newGroupId) { ret.PB(*it) ; groups[*it] = newGroupId ; } } return ret ; } void reorgenize(const VI &company, int boss) { FOREACH(it, company) { if(haters[*it]==0 && likes[*it].empty()) { ret[*it] = boss ; CERR(company, " -> ", *it) ; FOREACH(who, revLikes[*it]) // czyscimy krawedzie w grafie od nowego korzenia likes[*who].erase(*it) ; revLikes[*it].clear() ; vector<VI> partitions ; // robimy podzial FOREACH(it2, company) { if(*it2 != *it && groups[*it2]==groups[*it]) partitions.PB(newGroup(*it2, nextGroupId++)) ; } DEBUG(partitions) ; FOREACH(a, company) // usuwamy haterow z innej grupy for(int j=0 ; j<hates[*a].size() ; ) { int b = hates[*a][j] ; if(groups[*a]!=groups[b]) { DEBUG(*a, "przestaje nie lubic", b) ; swap(hates[*a][j], hates[*a].back()) ; hates[*a].pop_back() ; haters[b]-- ; } else j++ ; } FOREACH(partition, partitions) { reorgenize(*partition, *it) ; if(ERROR) return ; } return ; } } ERROR=true ; CERR("error for ", company) ; } // Dodatkowy kod do sprawdzania poprawnosci vector<PII> T, N ; bool CHECK_CORRECTNESS=false;//true ; int main() { ios_base::sync_with_stdio(0) ; int n, m, a, b ; cin >> n >> m ; char c ; while(m--) { cin >> a >> b >> c ; if(c=='T') { likes[a].insert(b) ; revLikes[b].PB(a) ; if(CHECK_CORRECTNESS) T.PB(MP(a,b)) ; } else if(c=='N') { hates[a].PB(b) ; haters[b]++ ; if(CHECK_CORRECTNESS) N.PB(MP(a,b)) ; } else assert(false) ; } memset(ret, -1, sizeof(ret)) ; VI company ; REP(i, n) company.PB(i+1) ; reorgenize(company, 0) ; if(ERROR) cout << "NIE" << endl ; else { assert(count(ret+1,ret+n+1, -1)==0) ; REP(i, n) cout << ret[i+1] << endl ; //cout << "TAK" << endl ; if(CHECK_CORRECTNESS) { //cerr << "sprawdzam" << endl ; VI *parents = new VI[n+1] ; for(int u=1 ; u<=n ; u++) { int a=ret[u] ; while(a!=0) { parents[u].PB(a) ; a=ret[a] ; } sort(ALL(parents[u])) ; } FOREACH(it, T) assert( binary_search(ALL(parents[it->FI]), it->SE)) ; FOREACH(it, N) assert(!binary_search(ALL(parents[it->FI]), it->SE)) ; delete [] parents ; } } }
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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | #include <iostream> #include <algorithm> #include <vector> #include <string> #include <map> #include <set> #include <queue> #include <stack> #include <cmath> #include <cassert> #include <cstring> #include <bitset> #include <sstream> //#include <bits/stdc++.h> #include <ext/numeric> using namespace std ; using namespace __gnu_cxx ; typedef long long LL ; typedef pair<int,int> PII ; typedef vector<int> VI ; const int INF = 1e9+100 ; const LL INFLL = (LL)INF*INF ; #define REP(i,n) for(int i=0;i<(n);++i) #define ALL(c) c.begin(),c.end() #define FOREACH(i,c) for(auto i=(c).begin();i!=(c).end();++i) #define CLEAR(t) memset(t,0,sizeof(t)) #define PB push_back #define MP make_pair #define FI first #define SE second template<class T1,class T2> ostream& operator<<(ostream &s, const pair<T1,T2> &x) { return s << "(" << x.FI << "," << x.SE << ")" ;} template<class T> ostream& operator<<(ostream &s, const vector<T> &t) { FOREACH(it, t) s << *it << " " ; return s ; } template<class T> ostream& operator<<(ostream &s, const set<T> &t) { FOREACH(it, t) s << *it << " " ; return s ; } template<class T1,class T2> ostream& operator<<(ostream &s, const map<T1, T2> &t) { FOREACH(it, t) s << *it << " " ; return s ; } template<class T1, class T2> T1 conv(const T2 &x) { stringstream sss ; sss << x ; T1 y ; sss >> y ; return y ; } template<class T> void _debug(bool printName, const char* s, const T &x) { if(printName) cerr << s << "=" ; cerr << x << endl ; } void _debug(bool, const char* s, const char*) { for(;*s;s++) if(*s!='"') { cerr << *s ; } cerr << endl ; } template<class T, class... R> void _debug(bool printName, const char* s, const T &x, R... y) { bool o=0, str=(*s=='"') ; for(; o || *s!=',' ; s++) if(*s=='"') o=!o ; else if(printName||str) cerr<<*s ; for(s++;*s && *s==' '; s++) ; if(!str) { if(printName) cerr << "=" ; cerr << x ; if(*s!='"' && printName) cerr << "," ; } cerr << " " ; _debug(printName, s, y...) ; } template<class T> void _coord(const T &x) { cerr << "[" << x << "]" ; } template<class T, class... R> void _coord(const T &x, R... y) { cerr << "[" << x << "]" ; _coord(y...) ; } template<class T, class I> void _val(T &t, const I &i) { cerr << t[i] ; } template<class T, class I, class... J> void _val(T &t, const I &i, J... j) { _val(t[i], j...) ; } #ifndef LOCAL #define CERR(...) #define DEBUG(...) #define DARRAY(...) #define DFLAG 0 #else #define CERR(...) if(DFLAG) _debug(0, #__VA_ARGS__, __VA_ARGS__) #define DEBUG(...) if(DFLAG) _debug(1, #__VA_ARGS__, __VA_ARGS__) #define DARRAY(t,...) if(DFLAG) { cerr << #t ; _coord(__VA_ARGS__) ; cerr << " = " ; _val(t,__VA_ARGS__) ; cerr << endl ; } #define DFLAG 0 #endif //////////////////////////////////////////////////////////////////////////////// const int MAXN = 1100 ; set<int> likes[MAXN] ; // kogo lubimy VI revLikes[MAXN] ; // kto nas lubi VI hates[MAXN] ; // kogo nienawidzimy int haters[MAXN] ; // ilu nas nienawidzi int groups[MAXN] ; // kolorowanie grafu (wyznaczajace podzial firmy) int nextGroupId=1 ; bool ERROR=false ; int ret[MAXN] ; // wynik VI newGroup(int u, int newGroupId) { VI ret = {u} ; groups[u] = newGroupId ; for(int i=0 ; i<ret.size() ; i++) { u = ret[i] ; FOREACH(it, revLikes[u]) if(groups[*it]!=newGroupId) { ret.PB(*it) ; groups[*it] = newGroupId ; } FOREACH(it, likes[u]) if(groups[*it]!=newGroupId) { ret.PB(*it) ; groups[*it] = newGroupId ; } } return ret ; } void reorgenize(const VI &company, int boss) { FOREACH(it, company) { if(haters[*it]==0 && likes[*it].empty()) { ret[*it] = boss ; CERR(company, " -> ", *it) ; FOREACH(who, revLikes[*it]) // czyscimy krawedzie w grafie od nowego korzenia likes[*who].erase(*it) ; revLikes[*it].clear() ; vector<VI> partitions ; // robimy podzial FOREACH(it2, company) { if(*it2 != *it && groups[*it2]==groups[*it]) partitions.PB(newGroup(*it2, nextGroupId++)) ; } DEBUG(partitions) ; FOREACH(a, company) // usuwamy haterow z innej grupy for(int j=0 ; j<hates[*a].size() ; ) { int b = hates[*a][j] ; if(groups[*a]!=groups[b]) { DEBUG(*a, "przestaje nie lubic", b) ; swap(hates[*a][j], hates[*a].back()) ; hates[*a].pop_back() ; haters[b]-- ; } else j++ ; } FOREACH(partition, partitions) { reorgenize(*partition, *it) ; if(ERROR) return ; } return ; } } ERROR=true ; CERR("error for ", company) ; } // Dodatkowy kod do sprawdzania poprawnosci vector<PII> T, N ; bool CHECK_CORRECTNESS=false;//true ; int main() { ios_base::sync_with_stdio(0) ; int n, m, a, b ; cin >> n >> m ; char c ; while(m--) { cin >> a >> b >> c ; if(c=='T') { likes[a].insert(b) ; revLikes[b].PB(a) ; if(CHECK_CORRECTNESS) T.PB(MP(a,b)) ; } else if(c=='N') { hates[a].PB(b) ; haters[b]++ ; if(CHECK_CORRECTNESS) N.PB(MP(a,b)) ; } else assert(false) ; } memset(ret, -1, sizeof(ret)) ; VI company ; REP(i, n) company.PB(i+1) ; reorgenize(company, 0) ; if(ERROR) cout << "NIE" << endl ; else { assert(count(ret+1,ret+n+1, -1)==0) ; REP(i, n) cout << ret[i+1] << endl ; //cout << "TAK" << endl ; if(CHECK_CORRECTNESS) { //cerr << "sprawdzam" << endl ; VI *parents = new VI[n+1] ; for(int u=1 ; u<=n ; u++) { int a=ret[u] ; while(a!=0) { parents[u].PB(a) ; a=ret[a] ; } sort(ALL(parents[u])) ; } FOREACH(it, T) assert( binary_search(ALL(parents[it->FI]), it->SE)) ; FOREACH(it, N) assert(!binary_search(ALL(parents[it->FI]), it->SE)) ; delete [] parents ; } } } |