Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. vector<set<int>> links; //Предки и дети чисел
  7. vector<set<int>> ans; //Распределение по группам
  8. set<int> cur; //Набор чисел с которыми я работаю
  9.  
  10. bool changed = true;
  11. bool *added;
  12.  
  13. void addRelatives(int num)
  14. {
  15. if (added[num])
  16. return;
  17.  
  18. added[num] = true;
  19.  
  20. int ind = 0, indchild = 1;
  21. if (ans[1].count(num))
  22. {
  23. ind = 1;
  24. indchild = 2;
  25. }
  26. else if (ans[2].count(num))
  27. {
  28. ind = 2;
  29. indchild = 0;
  30. }
  31.  
  32. for (auto i : links[num])
  33. {
  34. if (cur.count(i))
  35. cur.erase(cur.find(i));
  36.  
  37. ans[indchild].insert(i);
  38. addRelatives(i);
  39. }
  40.  
  41. changed = true;
  42. }
  43.  
  44. void descrPos(int num)
  45. {
  46. for (auto i : links[num])
  47. {
  48. if (ans[0].count(i))
  49. {
  50. ans[2].insert(num);
  51. cur.erase(cur.find(num));
  52. addRelatives(num);
  53. return;
  54. }
  55. else if (ans[1].count(i))
  56. {
  57. ans[0].insert(num);
  58. cur.erase(cur.find(num));
  59. addRelatives(num);
  60. return;
  61. }
  62. else if (ans[2].count(i))
  63. {
  64. ans[1].insert(num);
  65. cur.erase(cur.find(num));
  66. addRelatives(num);
  67. return;
  68. }
  69. }
  70. }
  71.  
  72. void tryToChange()
  73. {
  74. set<int>::iterator it = cur.begin();
  75. int e = *it;
  76.  
  77. cur.erase(cur.begin());
  78.  
  79. ans[0].insert(e);
  80. addRelatives(e);
  81. }
  82.  
  83. bool proof()
  84. {
  85. for (int i = 0; i < links.size(); i++)
  86. {
  87. int ind = 0, indchild = 1;
  88. if (ans[1].count(i))
  89. {
  90. ind = 1;
  91. indchild = 2;
  92. }
  93. else if (ans[2].count(i))
  94. {
  95. ind = 2;
  96. indchild = 0;
  97. }
  98.  
  99. for (auto elem : links[i])
  100. {
  101. if (!ans[indchild].count(elem))
  102. {
  103. return true;
  104. }
  105. }
  106. }
  107.  
  108. return false;
  109. }
  110.  
  111. signed main()
  112. {
  113. int n, m;
  114.  
  115. ans = vector<set<int>>(3, set<int>());
  116.  
  117. cin >> n >> m;
  118. bool used[n];
  119. added = new bool[n];
  120.  
  121. links = vector<set<int>>();
  122.  
  123. for (int i = 0; i < n; i++)
  124. {
  125. set<int> s;
  126.  
  127. links.push_back(s);
  128. used[i] = false;
  129. added[i] = false;
  130. }
  131.  
  132. int x, y;
  133.  
  134. for (int i = 0; i < m; i++)
  135. {
  136. cin >> x >> y;
  137. x--;
  138. y--;
  139.  
  140. if (x == y)
  141. {
  142. cout << "NO" << endl;
  143. return 0;
  144. }
  145.  
  146. links[x].insert(y); //Добавляю ребенка
  147.  
  148. cur.insert(x);
  149. used[x] = true;
  150. used[y] = true;
  151. }
  152.  
  153. for (int i = 0; i < n; i++)
  154. {
  155. if (!used[i])
  156. {
  157. ans[0].insert(i);
  158. }
  159. }
  160.  
  161. if (cur.size() > 0)
  162. tryToChange();
  163.  
  164. int count, e;
  165. while (changed)
  166. {
  167. changed = false;
  168. count = cur.size();
  169. for (int i = 0; i < count; i++)
  170. {
  171. set<int>::iterator it = cur.begin();
  172. e = *it;
  173.  
  174. descrPos(e);
  175. }
  176.  
  177. if (cur.size() > 0 && !changed)
  178. tryToChange();
  179. }
  180.  
  181. if (proof())
  182. {
  183. cout << "NO" << endl;
  184. }
  185. else
  186. {
  187. cout << "YES" << endl;
  188.  
  189. for (int i = 0; i < 3; i++)
  190. {
  191. cout << ans[i].size() << " ";
  192. for (auto elem : ans[i])
  193. {
  194. cout << elem + 1 << " ";
  195. }
  196. cout << endl;
  197. }
  198. }
  199.  
  200. return 0;
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement