Guest User

Untitled

a guest
Feb 6th, 2021
571
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include<algorithm>
  4. #include<time.h>
  5. #include<random>
  6. #include<queue>
  7. #include<set>
  8. #include<string>
  9. #include<map>
  10. #include<iomanip>
  11. #include<unordered_map>
  12. #include<deque>
  13. #define int long long
  14. using namespace std;
  15. const int inf = 1e9 + 7;
  16. void dbg() {
  17. cout << "! ";
  18. }
  19. vector<vector<int>> ga, oga, ogb, gb;
  20. bool ok = 0;
  21. vector<pair<int, int>> ans;
  22. void dfs(int a, int b, int h) {
  23. if (ok) return;
  24. if (h == 0) {
  25. ok = 1;
  26. return;
  27. }
  28. // cout << a << ' ' << b << endl;
  29. {
  30. if (ga[a].size() && oga[b].size()) {
  31. for (auto u : ga[a]) {
  32. if (a == u) continue;
  33. for (int i = 0; i < 40; ++i) {
  34. int x = oga[b].size();
  35. int ps = rand() % x;
  36. if (oga[b][ps] == b) continue;
  37. ans.push_back({ u,oga[b][ps] });
  38. dfs(u, oga[b][ps], h - 1);
  39. if (ok) return;
  40. ans.pop_back();
  41. }
  42. }
  43. }
  44. }
  45. {
  46. if (gb[a].size() && ogb[b].size()) {
  47. for (auto u : gb[a]) {
  48. if (u == a) continue;
  49. for (int i = 0; i < 40; ++i) {
  50. int x = ogb[b].size();
  51. int ps = rand() % x;
  52. if (ogb[b][ps] == b) continue;
  53. ans.push_back({ u,ogb[b][ps] });
  54. dfs(u, ogb[b][ps], h - 1);
  55. if (ok) return;
  56. ans.pop_back();
  57. }
  58. }
  59. }
  60. }
  61. }
  62. void solve() {
  63. ok = 0;
  64. int n, m;
  65. cin >> n >> m;
  66. vector<string> a(n);
  67. for (int i = 0; i < n; ++i) {
  68. cin >> a[i];
  69. }
  70. if (m % 2) {
  71. cout << "YES\n";
  72. cout << "1 ";
  73. for (int i = 0; i < m; ++i) {
  74. cout << 1 + (i % 2 == 0) << ' ';
  75. }
  76. cout << "\n";
  77. return;
  78. }
  79. ga.clear();
  80. oga.clear();
  81. ogb.clear();
  82. gb.clear();
  83. ans.clear();
  84. ga.resize(n);
  85. oga.resize(n);
  86. ogb.resize(n);
  87. gb.resize(n);
  88. for (int i = 0; i < n; ++i) {
  89. for (int j = 0; j < n; ++j) {
  90. if (a[i][j] == '*') continue;
  91. if (a[i][j] == 'a') {
  92. ga[i].push_back(j);
  93. oga[j].push_back(i);
  94. } else {
  95. gb[i].push_back(j);
  96. ogb[j].push_back(i);
  97. }
  98. }
  99. }
  100. for (int i = 0; i < n; ++i) {
  101. dfs(i, i, m / 2);
  102. if (ok) {
  103. cout << "YES\n";
  104. vector<int> answ;
  105. for (int i = 0; i < ans.size(); ++i) {
  106. answ.push_back(ans[ans.size() - i - 1].second + 1);
  107. }
  108. answ.push_back(i + 1);
  109. for (int i = 0; i < ans.size(); ++i) {
  110. answ.push_back(ans[i].first + 1);
  111. }
  112. //dbg();
  113. for (auto elem : answ) {
  114. //cout << elem.first << ' ' << elem.second << endl;
  115. cout << elem << ' ';
  116. }
  117. //dbg();
  118. cout << '\n';
  119. return;
  120.  
  121. }
  122. }
  123. cout << "NO\n";
  124.  
  125. }
  126. int32_t main() {
  127. ios_base::sync_with_stdio(0);
  128. cin.tie(0);
  129. cout.tie(0);
  130. //srand(time(0));
  131. int tt = 1;
  132. cin >> tt;
  133. while (tt--) {
  134. solve();
  135. }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment