Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. const char TBD = 2;
  7. char rooms[200000];
  8.  
  9. struct link{
  10. link();
  11. link(int _a, int _b,char _c) :
  12. a(_a), b(_b), c(_c) {}
  13. int a;
  14. int b;
  15. char c;
  16. };
  17.  
  18. void find(vector<link>& links, vector<int>& results, int curr_result) {
  19. if (links.empty()) {
  20. results.push_back(curr_result);
  21. return;
  22. }
  23.  
  24. link top = links.back();
  25. links.pop_back();
  26.  
  27.  
  28. if (rooms[top.a] == TBD && rooms[top.b] == TBD) {
  29. rooms[top.a] = 0;
  30. find(links, results, curr_result);
  31.  
  32. rooms[top.a] = 1;
  33. find(links, results, curr_result + 1);
  34. }
  35. else if (rooms[top.a] != TBD && rooms[top.b] != TBD) {
  36. if (rooms[top.a] + rooms[top.b] != top.c)
  37. return; //Can not happen
  38. else if (links.empty()) {
  39. results.push_back(curr_result);
  40. }
  41. }
  42. else if (rooms[top.a] != TBD) { // One end is filled
  43. rooms[top.b] = !(rooms[top.a]);
  44. find(links, results, curr_result + rooms[top.b]);
  45. }
  46. else {
  47. rooms[top.a] = !(rooms[top.b]);
  48. find(links, results, curr_result + rooms[top.a]);
  49. }
  50. }
  51.  
  52. int main() {
  53. vector<link> links;
  54. vector<int> results;
  55.  
  56. int n, m, a, b, c;
  57. scanf("%d %d", &n, &m);
  58. int temp_res = n;
  59. fill(rooms, rooms + n, TBD);
  60.  
  61. int unknown = n;
  62. for (int i = 0; i < m; i++){
  63. scanf("%d %d %d", &a, &b, &c);
  64.  
  65. if (c != 1) {
  66. rooms[a - 1] = c / 2;
  67. rooms[b - 1] = c / 2;
  68. temp_res -= c;
  69. }
  70. else {
  71. links.push_back(link(a-1, b-1,c));
  72. rooms[a - 1] = TBD;
  73. rooms[b - 1] = TBD;
  74. }
  75. }
  76.  
  77. find(links, results, temp_res);
  78.  
  79. if (results.empty()) {
  80. printf("impossible");
  81. }
  82. return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement