Advertisement
Rentib

Untitled

Feb 14th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. bool w[100007];
  4. int n, m;
  5. class Graph{
  6. int V;
  7. list<int> *G;
  8. public:
  9. Graph(int V) { this->V = V; G = new list<int>[V]; }
  10. ~Graph() { delete [] G; }
  11. void dfs(int a);
  12. bool is_ok(){
  13. for(int i = 1;i <= V;i++)
  14. if(G[i].size() % 2 == 1)
  15. return 0;
  16. dfs(1);
  17. for(int i = 1;i <= n;i++) if(!w[i]) return 0;
  18. return 1;
  19. }
  20. void addEdge(int u, int v){
  21. G[u].push_back(v);
  22. G[v].push_back(u);
  23. }
  24. void rmvEdge(int u, int v);
  25. void ET(int s);
  26. int DFSCount(int v, bool visited[]);
  27. bool most(int u, int v);
  28. };
  29. void Graph::dfs(int a){
  30. w[a] = 1;
  31. for(auto i : G[a])
  32. if(!w[i])
  33. dfs(i);
  34. }
  35. void Graph::ET(int u){
  36. list<int>::iterator i;
  37. for (i = G[u].begin(); i != G[u].end(); ++i){
  38. int v = *i;
  39. if(v != -1 && most(u, v)){
  40. cout << u << ' ';
  41. rmvEdge(u, v);
  42. ET(v);
  43. }
  44. }
  45. }
  46. bool Graph::most(int u, int v){
  47. int count = 0;
  48. list<int>::iterator i;
  49. for(i = G[u].begin(); i != G[u].end(); ++i)
  50. if(*i != -1)
  51. count++;
  52. if(count == 1)
  53. return true;
  54. bool visited[V];
  55. memset(visited, false, V);
  56. int count1 = DFSCount(u, visited);
  57. rmvEdge(u, v);
  58. memset(visited, false, V);
  59. int count2 = DFSCount(u, visited);
  60. addEdge(u, v);
  61. return (count1 > count2)? false: true;
  62. }
  63. void Graph::rmvEdge(int u, int v){
  64. list<int>::iterator iv = find(G[u].begin(), G[u].end(), v);
  65. *iv = -1;
  66. list<int>::iterator iu = find(G[v].begin(), G[v].end(), u);
  67. *iu = -1;
  68. }
  69. int Graph::DFSCount(int v, bool visited[]){
  70. visited[v] = true;
  71. int count = 1;
  72. list<int>::iterator i;
  73. for (i = G[v].begin(); i != G[v].end(); ++i)
  74. if (*i != -1 && !visited[*i])
  75. count += DFSCount(*i, visited);
  76. return count;
  77. }
  78. int main(){
  79. cin >> n >> m;
  80. Graph g(n + 1);
  81. for(int i = 0, a, b;i < m;i++){
  82. cin >> a >> b;
  83. g.addEdge(a, b);
  84. }
  85. if(g.is_ok() == 0){
  86. cout << "NIE";
  87. return 0;
  88. }
  89. cout << "TAK\n";
  90. g.ET(1);
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement