Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<vector>
- #include<algorithm>
- #include<time.h>
- #include<random>
- #include<queue>
- #include<set>
- #include<string>
- #include<map>
- #include<iomanip>
- #include<unordered_map>
- #include<deque>
- #define int long long
- using namespace std;
- const int inf = 1e9 + 7;
- void dbg() {
- cout << "! ";
- }
- vector<vector<int>> ga, oga, ogb, gb;
- bool ok = 0;
- vector<pair<int, int>> ans;
- void dfs(int a, int b, int h) {
- if (ok) return;
- if (h == 0) {
- ok = 1;
- return;
- }
- // cout << a << ' ' << b << endl;
- {
- if (ga[a].size() && oga[b].size()) {
- for (auto u : ga[a]) {
- if (a == u) continue;
- for (int i = 0; i < 40; ++i) {
- int x = oga[b].size();
- int ps = rand() % x;
- if (oga[b][ps] == b) continue;
- ans.push_back({ u,oga[b][ps] });
- dfs(u, oga[b][ps], h - 1);
- if (ok) return;
- ans.pop_back();
- }
- }
- }
- }
- {
- if (gb[a].size() && ogb[b].size()) {
- for (auto u : gb[a]) {
- if (u == a) continue;
- for (int i = 0; i < 40; ++i) {
- int x = ogb[b].size();
- int ps = rand() % x;
- if (ogb[b][ps] == b) continue;
- ans.push_back({ u,ogb[b][ps] });
- dfs(u, ogb[b][ps], h - 1);
- if (ok) return;
- ans.pop_back();
- }
- }
- }
- }
- }
- void solve() {
- ok = 0;
- int n, m;
- cin >> n >> m;
- vector<string> a(n);
- for (int i = 0; i < n; ++i) {
- cin >> a[i];
- }
- if (m % 2) {
- cout << "YES\n";
- cout << "1 ";
- for (int i = 0; i < m; ++i) {
- cout << 1 + (i % 2 == 0) << ' ';
- }
- cout << "\n";
- return;
- }
- ga.clear();
- oga.clear();
- ogb.clear();
- gb.clear();
- ans.clear();
- ga.resize(n);
- oga.resize(n);
- ogb.resize(n);
- gb.resize(n);
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < n; ++j) {
- if (a[i][j] == '*') continue;
- if (a[i][j] == 'a') {
- ga[i].push_back(j);
- oga[j].push_back(i);
- } else {
- gb[i].push_back(j);
- ogb[j].push_back(i);
- }
- }
- }
- for (int i = 0; i < n; ++i) {
- dfs(i, i, m / 2);
- if (ok) {
- cout << "YES\n";
- vector<int> answ;
- for (int i = 0; i < ans.size(); ++i) {
- answ.push_back(ans[ans.size() - i - 1].second + 1);
- }
- answ.push_back(i + 1);
- for (int i = 0; i < ans.size(); ++i) {
- answ.push_back(ans[i].first + 1);
- }
- //dbg();
- for (auto elem : answ) {
- //cout << elem.first << ' ' << elem.second << endl;
- cout << elem << ' ';
- }
- //dbg();
- cout << '\n';
- return;
- }
- }
- cout << "NO\n";
- }
- int32_t main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- //srand(time(0));
- int tt = 1;
- cin >> tt;
- while (tt--) {
- solve();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment