fork(1) download
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <map>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. typedef pair<int,int> pii;
  9. #define mp make_pair
  10. #define pb push_back
  11.  
  12. int n, m;
  13.  
  14. vector<int> g[100005];
  15.  
  16. map<pii,int> done, blocked;
  17.  
  18. int processed[100005];
  19.  
  20. void print(int a, int b, int c) {
  21. cout << a << ' ' << b << ' ' << c << '\n';
  22. done[mp(a,b)] = done[mp(b,a)] = true;
  23. done[mp(b,c)] = done[mp(c,b)] = true;
  24. }
  25.  
  26. int cut(int v) {
  27. vector<int> adj;
  28. for(size_t i = 0; i < g[v].size(); i++) {
  29. int u = g[v][i];
  30. if(!blocked[mp(v,u)]) {
  31. blocked[mp(v,u)] = blocked[mp(u,v)] = true;
  32. adj.pb(u);
  33. }
  34. }
  35.  
  36. int sz = adj.size();
  37. for(int i = 0; i < sz; i++) {
  38. int u = adj[i];
  39. if(!processed[u]) {
  40. int w = cut(u);
  41. if(w != 0) {
  42. print(v,u,w);
  43. }
  44. }
  45. }
  46.  
  47. int u = 0;
  48. for(int i = 0; i < sz; i++) {
  49. int curr = adj[i];
  50. if(!done[mp(v,curr)]) {
  51. if(u==0) {
  52. u = curr;
  53. } else {
  54. print(u,v,curr);
  55. u = 0;
  56. }
  57. }
  58. }
  59.  
  60. processed[v] = true;
  61. return u;
  62. }
  63.  
  64. int main() {
  65. ios_base::sync_with_stdio(false);
  66.  
  67. cin >> n >> m;
  68.  
  69. if(m%2==1) {
  70. cout << "No solution" << endl;
  71. return 0;
  72. }
  73.  
  74. for(int i = 0; i < m; i++) {
  75. int a, b; cin >> a >> b;
  76. g[a].pb(b);
  77. g[b].pb(a);
  78. }
  79.  
  80. cut(1);
  81. }
  82.  
Success #stdin #stdout 0s 4996KB
stdin
Standard input is empty
stdout
Standard output is empty