Advertisement
Guest User

Untitled

a guest
Aug 29th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.25 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. used[v] = T;
  205. DP[v] = make_pair(1000000000, -1000000000);
  206.  
  207. for (int i = 0; i < g[v].size(); i++)
  208. {
  209. int to = g[v][i];
  210. if (on_cycle[v] && on_cycle[to] && (is_next(to, v)))
  211. continue;
  212.  
  213. //cout << v << "%" << to << " " << on_cycle[to] << endl;
  214.  
  215. if (on_cycle[to])
  216. {
  217. DP[v] = update(DP[v], to);
  218. continue;
  219. }
  220. DP[v] = combine(DP[v], magic_dfs(to));
  221. }
  222. // cout << v << "%" << DP[v].first << "%" << DP[v].second << endl;
  223. return DP[v];
  224. }
  225.  
  226. int Rand()
  227. {
  228. int res = 0;
  229. for (int i = 0; i<= 60; i++)
  230. {
  231. res = res * 2 + rand() % 2;
  232. if (res >= n)
  233. res -= n;
  234. }
  235. return res;
  236. }
  237.  
  238. int main(){
  239. //freopen("fabro.in","r",stdin);
  240. //freopen("fabro.out","w",stdout);
  241. //freopen("F:/input.txt", "r", stdin);
  242. //freopen("F:/output.txt", "w", stdout);
  243. ios_base::sync_with_stdio(0);
  244. //cin.tie(0);
  245.  
  246. // freopen("input.txt", "r", stdin);
  247.  
  248. cin >> n >> m;
  249. for (int i = 1; i <= m; i++)
  250. {
  251. //cout << i << endl;
  252. cin >> a >> b;
  253. /* a = Rand() + 1;
  254. b = Rand() + 1;
  255. while (b == a)
  256. {
  257. b = b + 1;
  258. if (b > n)
  259. b = 1;
  260. }
  261. */
  262. g[a].push_back(b);
  263. }
  264.  
  265. // cout << "E" << endl;
  266. for (int i = 1; i <= n; i++)
  267. {
  268. if (used[i])
  269. continue;
  270. if (dfs(i))
  271. break;
  272. }
  273.  
  274. // cout << "@" << endl;
  275.  
  276. /*for (int i = 1; i <= n; i++)
  277. {
  278. cout << i << "%" << par[i] << endl;
  279. }*/
  280.  
  281. //cout << "@" << endl;
  282. //cout << cycle_start << " " << cycle_end << " " <<endl;
  283.  
  284. if (cycle_start == 0)// dag
  285. {
  286. cout << "NIE" << endl;
  287. return 0;
  288. }
  289.  
  290. V = get_cycle();
  291.  
  292. for (int i = 1; i <= n; i++)
  293. used[i] = 0;
  294. for (int i = 0; i < V.size(); i++)
  295. {
  296. int v = V[i];
  297. used[v] = 2;
  298. }
  299.  
  300. cycle_start = 0;
  301. cycle_end = 0;
  302.  
  303. for (int i = 1; i <= n; i++)
  304. {
  305. if (used[i])
  306. continue;
  307. if (dfs(i))
  308. break;
  309. }
  310.  
  311. if (cycle_start != 0) // still have a cycle
  312. {
  313. cout << 0 << endl;
  314. cout << endl;
  315. return 0;
  316. }
  317. /*
  318. for (int i = 0; i < V.size(); i++)
  319. {
  320. cout << V[i] << " ";
  321. }
  322. cout << endl;
  323. */
  324. for (int i = 0; i < V.size(); i++)
  325. {
  326. on_cycle[V[i]] = i + 1;
  327. }
  328.  
  329. int covered_L = 0;
  330.  
  331. block_L = block_R = 0;
  332.  
  333. int Done = 0;
  334.  
  335. for (int i = 1; i <= n; i++)
  336. {
  337. used[i] = 0;
  338. }
  339.  
  340. while (true)
  341. {
  342. ++T;
  343.  
  344. //cout << block_L << " " << block_R << endl;
  345.  
  346. int new_max = block_R;
  347.  
  348. // cout << block_L << " %" << block_R << endl;
  349.  
  350. /*for (int i = 1; i <= 3; i++)
  351. {
  352. cout << DP[i].first << "%" << DP[i].second << endl;
  353. }
  354. */
  355. /* for (int i = 1; i <= n; i++)
  356. {
  357. cout << DP[i].first << " " << DP[i].second << " " << i << " "<<used[i]<<endl;
  358. }
  359. cout << endl;
  360. */
  361. for (int i = block_L; i <= block_R; i++)
  362. {
  363. ++Done;
  364. pair<int, int> Q = magic_dfs(V[i]);
  365. // max ovr this base, min ovr this base, use only vertices from last iteration + unused
  366. // cout << "@" <<i<<"^"<< Q.first << "^" << Q.second <<" "<<block_L<<" "<<block_R<< endl;
  367. // cout << DP[2].first << "%%" << DP[2].second << endl;
  368. // cout << DP[4].first << "%" << DP[4].second << endl;
  369.  
  370. if (Q.second < 0)
  371. continue;
  372. if (larger(Q.second, new_max))
  373. new_max = Q.second;
  374. add_edge(i, Q.first);
  375. add_edge(i, Q.second);
  376. //cout << Q.first<< "%" << Q.second << endl;
  377.  
  378. //cin.get();
  379.  
  380. if (Q.first <= i) // within block, <=1 fixed vertices; maybe this one
  381. {
  382. continue;
  383. run_magic_checker(i);
  384. return 0;
  385. }
  386. }
  387. // cout << block_L << " " << block_R << " " << new_max << endl;
  388. // cin.get();
  389.  
  390. block_L = block_R + 1;
  391. block_R = max(new_max, block_R + 1);
  392. if (block_L >= V.size())
  393. block_L = 0;
  394. if (block_R >= V.size())
  395. block_R = 0;
  396. if (Done > V.size())
  397. break;
  398. }
  399.  
  400.  
  401. for (int i = 0; i < V.size(); i++)
  402. {
  403. bad_vertices.insert(i);
  404. }
  405.  
  406. for (int i = 0; i < good_edges.size(); i++)
  407. {
  408. int v1 = good_edges[i].first;
  409. int v2 = good_edges[i].second;
  410.  
  411. //cout << "!!" << v1 << " " << v2 << endl;
  412.  
  413. if (v1 == v2)
  414. {
  415. int flag = 0;
  416.  
  417. if (bad_vertices.find(v1) != bad_vertices.end())
  418. {
  419. flag = 1;
  420. }
  421. bad_vertices.clear();
  422. if (flag)
  423. bad_vertices.insert(v1);
  424. }
  425.  
  426. if (v1 < v2)
  427. {
  428. while (true)
  429. {
  430. it = bad_vertices.upper_bound(v1);
  431. if (it == bad_vertices.end())
  432. break;
  433. int val = (*it);
  434. if (val >= v2)
  435. {
  436. break;
  437. }
  438. bad_vertices.erase(val);
  439. }
  440. }
  441. else // over cycle start
  442. {
  443. while (true)
  444. {
  445. it = bad_vertices.upper_bound(v1);
  446. if (it == bad_vertices.end())
  447. break;
  448. int val = (*it);
  449. bad_vertices.erase(val);
  450. }
  451. while (true)
  452. {
  453. it = bad_vertices.begin();
  454. if (it == bad_vertices.end())
  455. break;
  456. int val = (*it);
  457. if (val >= v2)
  458. break;
  459. bad_vertices.erase(val);
  460. }
  461. }
  462. }
  463.  
  464. vector<int> ans;
  465. for (it = bad_vertices.begin(); it != bad_vertices.end(); it++)
  466. {
  467. int val = (*it);
  468. ans.push_back(V[val]);
  469. }
  470.  
  471. sort(ans.begin(), ans.end());
  472.  
  473. cout << ans.size() << endl;
  474. for (int i = 0; i < ans.size(); i++)
  475. {
  476. if (i)
  477. cout << " ";
  478. cout << ans[i];
  479. }
  480. cout << endl;
  481.  
  482. cin.get(); cin.get();
  483. return 0;
  484. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement