Advertisement
Guest User

Untitled

a guest
Aug 29th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.30 KB | None | 0 0
  1. /*
  2. */
  3.  
  4. //#pragma comment(linker, "/STACK:16777216")
  5. #define _CRT_SECURE_NO_WARNINGS
  6.  
  7. #include <fstream>
  8. #include <iostream>
  9. #include <string>
  10. #include <complex>
  11. #include <math.h>
  12. #include <set>
  13. #include <vector>
  14. #include <map>
  15. #include <queue>
  16. #include <stdio.h>
  17. #include <stack>
  18. #include <algorithm>
  19. #include <list>
  20. #include <ctime>
  21. #include <memory.h>
  22. #include <assert.h>
  23.  
  24. #define y0 sdkfaslhagaklsldk
  25. #define y1 aasdfasdfasdf
  26. #define yn askfhwqriuperikldjk
  27. #define j1 assdgsdgasghsf
  28. #define tm sdfjahlfasfh
  29. #define lr asgasgash
  30. #define norm asdfasdgasdgsd
  31.  
  32. #define eps 1e-9
  33. #define M_PI 3.141592653589793
  34. #define bs 1000000007ll
  35. #define bsize 350
  36.  
  37. using namespace std;
  38.  
  39. const int INF = 1e9;
  40. const int N = 550000;
  41.  
  42. int n, m;
  43. int a, b;
  44. vector<int> g[N];
  45. int used[N];
  46.  
  47. int par[N];
  48. int cycle_start;
  49. int cycle_end;
  50.  
  51. int dfs(int v)
  52. {
  53. //cout << v << "%!" << par[v] << endl;
  54. used[v] = 1;
  55. for (int i = 0; i < g[v].size(); i++)
  56. {
  57. int to = g[v][i];
  58. if (used[to] == 2)
  59. continue;
  60. if (used[to] == 0)
  61. {
  62. par[to] = v;
  63. //cout << "!!!" << v << " " << to << endl;
  64. if (dfs(to))
  65. return true;
  66. }
  67. else
  68. {
  69. cycle_end = v;
  70. cycle_start = to;
  71. return true;
  72. }
  73. }
  74. used[v] = 2;
  75. return false;
  76. }
  77.  
  78. vector<int> get_cycle()
  79. {
  80. int cur = cycle_end;
  81. vector<int> res;
  82. res.push_back(cur);
  83. while (true)
  84. {
  85. //cout << cur << endl;
  86. //cin.get();
  87. cur = par[cur];
  88. res.push_back(cur);
  89. if (cur == cycle_start)
  90. break;
  91. }
  92.  
  93. reverse(res.begin(), res.end());
  94.  
  95. return res;
  96. }
  97.  
  98. int on_cycle[N];
  99. int block_L, block_R;
  100. int T;
  101. set<int>::iterator it;
  102. set<int> bad_vertices;
  103.  
  104. vector<pair<int, int> > good_edges;
  105.  
  106. void add_edge(int a, int b)
  107. {
  108. good_edges.push_back(make_pair(a, b));
  109. }
  110.  
  111. vector<int> V;
  112.  
  113. void run_magic_checker(int id) // either id-th vertex of cycle or no vertices at all
  114. {
  115. for (int i = 1; i <= n; i++)
  116. {
  117. used[i] = 0;
  118. }
  119. used[V[id]] = 2;
  120.  
  121.  
  122. cycle_start = 0;
  123. cycle_end = 0;
  124.  
  125. for (int i = 1; i <= n; i++)
  126. {
  127. if (used[i])
  128. continue;
  129. if (dfs(i))
  130. break;
  131. }
  132.  
  133. if (cycle_start != 0)
  134. {
  135. cout << 0 << endl;
  136. cout << endl;
  137. }
  138. else
  139. {
  140. cout << 1 << endl;
  141. cout << V[id] << endl;
  142. }
  143. }
  144.  
  145. pair<int, int> DP[N];
  146.  
  147.  
  148. bool larger(int a, int b)
  149. {
  150. if (b >= block_L)
  151. {
  152. if (a > b)
  153. return true;
  154. if (a < block_L)
  155. return true;
  156. return false;
  157. }
  158. return (a>b&&a < block_L);
  159. }
  160.  
  161. pair<int, int> update(pair<int, int> P, int to)
  162. {
  163. to = on_cycle[to] - 1;
  164. if (larger(P.first, to) || P.first == 1000000000)
  165. P.first = to;
  166. if (larger(to, P.second) || P.second == -1000000000)
  167. P.second = to;
  168. return P;
  169. }
  170.  
  171. pair<int, int> combine(pair<int, int> P1, pair<int, int> P2)
  172. {
  173.  
  174. if (larger(P1.first, P2.first) || P1.first == 1000000000)
  175. P1.first = P2.first;
  176.  
  177. if (P2.second>=0&&(larger(P2.second, P1.second) || P1.second == -1000000000))
  178. P1.second = P2.second;
  179. return P1;
  180. }
  181.  
  182. bool is_next(int a, int b)
  183. {
  184. int Q = on_cycle[b];
  185. Q++;
  186. if (Q > V.size())
  187. Q = 1;
  188. return (Q == on_cycle[a]);
  189. }
  190.  
  191. pair<int, int> magic_dfs(int v)
  192. {
  193. //cout << v << "^!" << on_cycle[v] << endl;
  194.  
  195.  
  196. if (used[v] == T)
  197. return DP[v];
  198. if (used[v] <= T - 2&&used[v]!=0)
  199. {
  200. DP[v] = make_pair(1000000000, -1000000000);
  201. return DP[v];
  202. }
  203.  
  204. if (used[v] == 0)
  205. used[v] = T;
  206.  
  207. else
  208. used[v] = -100500;
  209.  
  210. DP[v] = make_pair(1000000000, -1000000000);
  211.  
  212. for (int i = 0; i < g[v].size(); i++)
  213. {
  214. int to = g[v][i];
  215. if (on_cycle[v] && on_cycle[to] && (is_next(to, v)))
  216. continue;
  217.  
  218. //cout << v << "%" << to << " " << on_cycle[to] << endl;
  219.  
  220. if (on_cycle[to])
  221. {
  222. DP[v] = update(DP[v], to);
  223. continue;
  224. }
  225. DP[v] = combine(DP[v], magic_dfs(to));
  226. }
  227. // cout << v << "%" << DP[v].first << "%" << DP[v].second << endl;
  228. return DP[v];
  229. }
  230.  
  231. int Rand()
  232. {
  233. int res = 0;
  234. for (int i = 0; i<= 60; i++)
  235. {
  236. res = res * 2 + rand() % 2;
  237. if (res >= n)
  238. res -= n;
  239. }
  240. return res;
  241. }
  242.  
  243. int main(){
  244. //freopen("fabro.in","r",stdin);
  245. //freopen("fabro.out","w",stdout);
  246. //freopen("F:/input.txt", "r", stdin);
  247. //freopen("F:/output.txt", "w", stdout);
  248. ios_base::sync_with_stdio(0);
  249. //cin.tie(0);
  250.  
  251. // freopen("input.txt", "r", stdin);
  252.  
  253. cin >> n >> m;
  254. for (int i = 1; i <= m; i++)
  255. {
  256. //cout << i << endl;
  257. cin >> a >> b;
  258. /* a = Rand() + 1;
  259. b = Rand() + 1;
  260. while (b == a)
  261. {
  262. b = b + 1;
  263. if (b > n)
  264. b = 1;
  265. }
  266. */
  267. g[a].push_back(b);
  268. }
  269.  
  270. // cout << "E" << endl;
  271. for (int i = 1; i <= n; i++)
  272. {
  273. if (used[i])
  274. continue;
  275. if (dfs(i))
  276. break;
  277. }
  278.  
  279. // cout << "@" << endl;
  280.  
  281. /*for (int i = 1; i <= n; i++)
  282. {
  283. cout << i << "%" << par[i] << endl;
  284. }*/
  285.  
  286. //cout << "@" << endl;
  287. //cout << cycle_start << " " << cycle_end << " " <<endl;
  288.  
  289. if (cycle_start == 0)// dag
  290. {
  291. cout << "NIE" << endl;
  292. return 0;
  293. }
  294.  
  295. V = get_cycle();
  296.  
  297. for (int i = 1; i <= n; i++)
  298. used[i] = 0;
  299. for (int i = 0; i < V.size(); i++)
  300. {
  301. int v = V[i];
  302. used[v] = 2;
  303. }
  304.  
  305. cycle_start = 0;
  306. cycle_end = 0;
  307.  
  308. for (int i = 1; i <= n; i++)
  309. {
  310. if (used[i])
  311. continue;
  312. if (dfs(i))
  313. break;
  314. }
  315.  
  316. if (cycle_start != 0) // still have a cycle
  317. {
  318. cout << 0 << endl;
  319. cout << endl;
  320. return 0;
  321. }
  322. /*
  323. for (int i = 0; i < V.size(); i++)
  324. {
  325. cout << V[i] << " ";
  326. }
  327. cout << endl;
  328. */
  329. for (int i = 0; i < V.size(); i++)
  330. {
  331. on_cycle[V[i]] = i + 1;
  332. }
  333.  
  334. int covered_L = 0;
  335.  
  336. block_L = block_R = 0;
  337.  
  338. int Done = 0;
  339.  
  340. for (int i = 1; i <= n; i++)
  341. {
  342. used[i] = 0;
  343. }
  344.  
  345. while (true)
  346. {
  347. ++T;
  348.  
  349. //cout << block_L << " " << block_R << endl;
  350.  
  351. int new_max = block_R;
  352.  
  353. // cout << block_L << " %" << block_R << endl;
  354.  
  355. /*for (int i = 1; i <= 3; i++)
  356. {
  357. cout << DP[i].first << "%" << DP[i].second << endl;
  358. }
  359. */
  360. /* for (int i = 1; i <= n; i++)
  361. {
  362. cout << DP[i].first << " " << DP[i].second << " " << i << " "<<used[i]<<endl;
  363. }
  364. cout << endl;
  365. */
  366. for (int i = block_L; i <= block_R; i++)
  367. {
  368. ++Done;
  369. pair<int, int> Q = magic_dfs(V[i]);
  370. // max ovr this base, min ovr this base, use only vertices from last iteration + unused
  371. // cout << "@" <<i<<"^"<< Q.first << "^" << Q.second <<" "<<block_L<<" "<<block_R<< endl;
  372. // cout << DP[2].first << "%%" << DP[2].second << endl;
  373. // cout << DP[4].first << "%" << DP[4].second << endl;
  374.  
  375. if (Q.second < 0)
  376. continue;
  377. if (larger(Q.second, new_max))
  378. new_max = Q.second;
  379. add_edge(i, Q.first);
  380. add_edge(i, Q.second);
  381. //cout << Q.first<< "%" << Q.second << endl;
  382.  
  383. //cin.get();
  384.  
  385. if (Q.first <= i) // within block, <=1 fixed vertices; maybe this one
  386. {
  387. continue;
  388. run_magic_checker(i);
  389. return 0;
  390. }
  391. }
  392. // cout << block_L << " " << block_R << " " << new_max << endl;
  393. // cin.get();
  394.  
  395. block_L = block_R + 1;
  396. block_R = max(new_max, block_R + 1);
  397. if (block_L >= V.size())
  398. block_L = 0;
  399. if (block_R >= V.size())
  400. block_R = 0;
  401. if (Done > V.size())
  402. break;
  403. }
  404.  
  405.  
  406. for (int i = 0; i < V.size(); i++)
  407. {
  408. bad_vertices.insert(i);
  409. }
  410.  
  411. for (int i = 0; i < good_edges.size(); i++)
  412. {
  413. int v1 = good_edges[i].first;
  414. int v2 = good_edges[i].second;
  415.  
  416. //cout << "!!" << v1 << " " << v2 << endl;
  417.  
  418. if (v1 == v2)
  419. {
  420. int flag = 0;
  421.  
  422. if (bad_vertices.find(v1) != bad_vertices.end())
  423. {
  424. flag = 1;
  425. }
  426. bad_vertices.clear();
  427. if (flag)
  428. bad_vertices.insert(v1);
  429. }
  430.  
  431. if (v1 < v2)
  432. {
  433. while (true)
  434. {
  435. it = bad_vertices.upper_bound(v1);
  436. if (it == bad_vertices.end())
  437. break;
  438. int val = (*it);
  439. if (val >= v2)
  440. {
  441. break;
  442. }
  443. bad_vertices.erase(val);
  444. }
  445. }
  446. else // over cycle start
  447. {
  448. while (true)
  449. {
  450. it = bad_vertices.upper_bound(v1);
  451. if (it == bad_vertices.end())
  452. break;
  453. int val = (*it);
  454. bad_vertices.erase(val);
  455. }
  456. while (true)
  457. {
  458. it = bad_vertices.begin();
  459. if (it == bad_vertices.end())
  460. break;
  461. int val = (*it);
  462. if (val >= v2)
  463. break;
  464. bad_vertices.erase(val);
  465. }
  466. }
  467. }
  468.  
  469. vector<int> ans;
  470. for (it = bad_vertices.begin(); it != bad_vertices.end(); it++)
  471. {
  472. int val = (*it);
  473. ans.push_back(V[val]);
  474. }
  475.  
  476. sort(ans.begin(), ans.end());
  477.  
  478. cout << ans.size() << endl;
  479. for (int i = 0; i < ans.size(); i++)
  480. {
  481. if (i)
  482. cout << " ";
  483. cout << ans[i];
  484. }
  485. cout << endl;
  486.  
  487. cin.get(); cin.get();
  488. return 0;
  489. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement