#include <cstdlib> #include <iostream> #include <vector> using namespace std; struct FromTo { int From; int To; }; enum CityType { normal = 0, process = 1, critic = 2, // cycle = 3, // oneway = 4, blank = 5, // external = 6 }; struct City { City() : group(normal), next(-1) {} CityType group; //0 - not founded yet; 1 - current process; 2 - possible critic; 3 - cycle, but not critic; 4 - 1 way connected to cycle; 5 - blank path int count; vector<int> neight; int next; }; vector<FromTo> excluded; City Cities[500001]; int Map[500001]; int RevMap[500001]; int zlicz[500001]; int CycleStart = -1; int CurrentStart = -1; bool CycleFirstDepth = false; int SearchCycle(int idx) //return 0 - blank, 1 - cycle found but not for you, 2 - found cycle { if(Cities[idx].group == process) { Cities[idx].group = critic; CycleStart = idx; return 2; } if(Cities[idx].group == blank) return 0; Cities[idx].group = process; for(vector<int>::iterator it = Cities[idx].neight.begin(); it != Cities[idx].neight.end(); it++) { int result = SearchCycle(*it); if(result == 0) //not cycle found continue; if(result == 1) // cycle found, but currently not on critic cycle { Cities[idx].group = normal; return 1; } //result == 2 //critic cycle Cities[idx].next = *it; if(Cities[idx].group == critic) return 1; Cities[idx].group = critic; return 2; } Cities[idx].group = blank; return 0; } int SearchOtherCycle(int idx) // 0 - blank; 1 - cycle (different than critic) { if(Cities[CurrentStart].next == idx) return 0; if(CycleFirstDepth == false && Cities[idx].group == critic) { if(CurrentStart != -1) { FromTo ft; ft.From = CurrentStart; ft.To = idx; excluded.push_back(ft); } return 0; } CycleFirstDepth = false; if(Cities[idx].group == blank) return 0; if(Cities[idx].group == process)//found other cycle return 1; if(Cities[idx].group == normal) Cities[idx].group = process; for(vector<int>::iterator it = Cities[idx].neight.begin(); it != Cities[idx].neight.end(); it++) { if(SearchOtherCycle(*it) == 1) return 1; } if(Cities[idx].group == process) Cities[idx].group = blank; return 0; } int main(int argc, char *argv[]) { int n,m; cin>>n>>m; for(int i = 0; i < m; i++) { int a,b; cin>>a>>b; Cities[a].neight.push_back(b); } for(int i = 1; i <= n; i++) { if(Cities[i].group != normal) continue; if(SearchCycle(i) != 0) break; } if(CycleStart == -1) { cout<<"NIE"; return 0; } bool first = true; for(CurrentStart = CycleStart; CurrentStart != CycleStart || first; CurrentStart = Cities[CurrentStart].next) { first = false; CycleFirstDepth = true; if(SearchOtherCycle(CurrentStart) == 1) { cout<<"0"<<endl; return 0; } } CurrentStart = -1; for(int i = 1; i <= n; i++) { if(Cities[i].group != normal) continue; if(SearchOtherCycle(i) == 1) { cout<<"0"<<endl; return 0; } } //we need to remove from critic cycle all founded noncycle //create map first = true; int lenght = 0; for(CurrentStart = CycleStart; CurrentStart != CycleStart || first; CurrentStart = Cities[CurrentStart].next) { first = false; RevMap[lenght] = CurrentStart; Map[CurrentStart] = lenght++; } for(vector<FromTo>::iterator it = excluded.begin(); it != excluded.end(); it++) { int mTo = Map[it->To]; int mFrom = Map[it->From]; int diff = (lenght + mTo - mFrom) % lenght; if(diff == 0) diff = lenght; if(zlicz[mFrom] < diff) zlicz[mFrom] = diff; } //mark uncritical as blank int remains = 0; for(int i = 0; i < lenght; i++) { if(remains > 0) Cities[RevMap[i]].group = blank; if(remains < zlicz[i]) remains = zlicz[i]; remains--; } for(int i = 0; remains > 0 && i < lenght; i++) { Cities[RevMap[i]].group = blank; remains--; } int Remaining = 0; first = true; for(CurrentStart = CycleStart; CurrentStart != CycleStart || first; CurrentStart = Cities[CurrentStart].next) { first = false; if(Cities[CurrentStart].group == critic) Remaining++; } cout<<Remaining<<endl; for(int i = 1; i <= n; i++) { if(Cities[i].group == critic) cout<<i<<" "; } /* for(int i = 1; i <= n; i++) { Cities[i].count = Cities[i].neight.size(); if(Cities[i].count < d) { Cities[i].group = -1; ToProcess.push(i); } } //part one remove cities with neightbours less than d while(ToProcess.empty() == false) { int a = ToProcess.front(); for(vector<int>::iterator it = Cities[a].neight.begin(); it != Cities[a].neight.end(); ++it) { int b = *it; if(Cities[b].group != 0) //it is or was on ToProcess queue continue; Cities[b].count--; if(Cities[b].count < d) { Cities[b].group = -1; ToProcess.push(b); } } ToProcess.pop(); } //all cities have minimum d roads //lets split this to group int maxGroup = 0; int maxGroupSize = 0; int CurrentGroup = 1; for(int s = 1; s <= n; s++) { if(Cities[s].group != 0) continue; Cities[s].group = CurrentGroup; ToProcess.push(s); int CurrentGroupSize = 0; while(ToProcess.empty() == false) { CurrentGroupSize++; int a = ToProcess.front(); for(vector<int>::iterator it = Cities[a].neight.begin(); it != Cities[a].neight.end(); ++it) { int b = *it; if(Cities[b].group != 0) //it is or was on ToProcess queue continue; Cities[b].group = CurrentGroup; ToProcess.push(b); } ToProcess.pop(); } if(CurrentGroupSize > maxGroupSize) { maxGroupSize = CurrentGroupSize; maxGroup = CurrentGroup; } CurrentGroup++; } if(maxGroupSize == 0) { cout<<"NIE"; return 0; } cout<<maxGroupSize<<endl; vector<int> Final; for(int i = 1; i <= n; i++) { if(Cities[i].group == maxGroup) Final.push_back(i); } for(vector<int>::iterator it = Final.begin(); it != Final.end(); ++it) { cout<<*it<<" "; } cin>>n;*/ }
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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 | #include <cstdlib> #include <iostream> #include <vector> using namespace std; struct FromTo { int From; int To; }; enum CityType { normal = 0, process = 1, critic = 2, // cycle = 3, // oneway = 4, blank = 5, // external = 6 }; struct City { City() : group(normal), next(-1) {} CityType group; //0 - not founded yet; 1 - current process; 2 - possible critic; 3 - cycle, but not critic; 4 - 1 way connected to cycle; 5 - blank path int count; vector<int> neight; int next; }; vector<FromTo> excluded; City Cities[500001]; int Map[500001]; int RevMap[500001]; int zlicz[500001]; int CycleStart = -1; int CurrentStart = -1; bool CycleFirstDepth = false; int SearchCycle(int idx) //return 0 - blank, 1 - cycle found but not for you, 2 - found cycle { if(Cities[idx].group == process) { Cities[idx].group = critic; CycleStart = idx; return 2; } if(Cities[idx].group == blank) return 0; Cities[idx].group = process; for(vector<int>::iterator it = Cities[idx].neight.begin(); it != Cities[idx].neight.end(); it++) { int result = SearchCycle(*it); if(result == 0) //not cycle found continue; if(result == 1) // cycle found, but currently not on critic cycle { Cities[idx].group = normal; return 1; } //result == 2 //critic cycle Cities[idx].next = *it; if(Cities[idx].group == critic) return 1; Cities[idx].group = critic; return 2; } Cities[idx].group = blank; return 0; } int SearchOtherCycle(int idx) // 0 - blank; 1 - cycle (different than critic) { if(Cities[CurrentStart].next == idx) return 0; if(CycleFirstDepth == false && Cities[idx].group == critic) { if(CurrentStart != -1) { FromTo ft; ft.From = CurrentStart; ft.To = idx; excluded.push_back(ft); } return 0; } CycleFirstDepth = false; if(Cities[idx].group == blank) return 0; if(Cities[idx].group == process)//found other cycle return 1; if(Cities[idx].group == normal) Cities[idx].group = process; for(vector<int>::iterator it = Cities[idx].neight.begin(); it != Cities[idx].neight.end(); it++) { if(SearchOtherCycle(*it) == 1) return 1; } if(Cities[idx].group == process) Cities[idx].group = blank; return 0; } int main(int argc, char *argv[]) { int n,m; cin>>n>>m; for(int i = 0; i < m; i++) { int a,b; cin>>a>>b; Cities[a].neight.push_back(b); } for(int i = 1; i <= n; i++) { if(Cities[i].group != normal) continue; if(SearchCycle(i) != 0) break; } if(CycleStart == -1) { cout<<"NIE"; return 0; } bool first = true; for(CurrentStart = CycleStart; CurrentStart != CycleStart || first; CurrentStart = Cities[CurrentStart].next) { first = false; CycleFirstDepth = true; if(SearchOtherCycle(CurrentStart) == 1) { cout<<"0"<<endl; return 0; } } CurrentStart = -1; for(int i = 1; i <= n; i++) { if(Cities[i].group != normal) continue; if(SearchOtherCycle(i) == 1) { cout<<"0"<<endl; return 0; } } //we need to remove from critic cycle all founded noncycle //create map first = true; int lenght = 0; for(CurrentStart = CycleStart; CurrentStart != CycleStart || first; CurrentStart = Cities[CurrentStart].next) { first = false; RevMap[lenght] = CurrentStart; Map[CurrentStart] = lenght++; } for(vector<FromTo>::iterator it = excluded.begin(); it != excluded.end(); it++) { int mTo = Map[it->To]; int mFrom = Map[it->From]; int diff = (lenght + mTo - mFrom) % lenght; if(diff == 0) diff = lenght; if(zlicz[mFrom] < diff) zlicz[mFrom] = diff; } //mark uncritical as blank int remains = 0; for(int i = 0; i < lenght; i++) { if(remains > 0) Cities[RevMap[i]].group = blank; if(remains < zlicz[i]) remains = zlicz[i]; remains--; } for(int i = 0; remains > 0 && i < lenght; i++) { Cities[RevMap[i]].group = blank; remains--; } int Remaining = 0; first = true; for(CurrentStart = CycleStart; CurrentStart != CycleStart || first; CurrentStart = Cities[CurrentStart].next) { first = false; if(Cities[CurrentStart].group == critic) Remaining++; } cout<<Remaining<<endl; for(int i = 1; i <= n; i++) { if(Cities[i].group == critic) cout<<i<<" "; } /* for(int i = 1; i <= n; i++) { Cities[i].count = Cities[i].neight.size(); if(Cities[i].count < d) { Cities[i].group = -1; ToProcess.push(i); } } //part one remove cities with neightbours less than d while(ToProcess.empty() == false) { int a = ToProcess.front(); for(vector<int>::iterator it = Cities[a].neight.begin(); it != Cities[a].neight.end(); ++it) { int b = *it; if(Cities[b].group != 0) //it is or was on ToProcess queue continue; Cities[b].count--; if(Cities[b].count < d) { Cities[b].group = -1; ToProcess.push(b); } } ToProcess.pop(); } //all cities have minimum d roads //lets split this to group int maxGroup = 0; int maxGroupSize = 0; int CurrentGroup = 1; for(int s = 1; s <= n; s++) { if(Cities[s].group != 0) continue; Cities[s].group = CurrentGroup; ToProcess.push(s); int CurrentGroupSize = 0; while(ToProcess.empty() == false) { CurrentGroupSize++; int a = ToProcess.front(); for(vector<int>::iterator it = Cities[a].neight.begin(); it != Cities[a].neight.end(); ++it) { int b = *it; if(Cities[b].group != 0) //it is or was on ToProcess queue continue; Cities[b].group = CurrentGroup; ToProcess.push(b); } ToProcess.pop(); } if(CurrentGroupSize > maxGroupSize) { maxGroupSize = CurrentGroupSize; maxGroup = CurrentGroup; } CurrentGroup++; } if(maxGroupSize == 0) { cout<<"NIE"; return 0; } cout<<maxGroupSize<<endl; vector<int> Final; for(int i = 1; i <= n; i++) { if(Cities[i].group == maxGroup) Final.push_back(i); } for(vector<int>::iterator it = Final.begin(); it != Final.end(); ++it) { cout<<*it<<" "; } cin>>n;*/ } |