Codeforces Round #699 (Div. 2) D. AB Graph 思維+構造

https://codeforces.com/contest/1481/problem/D

目錄

題意

n個點組成的完全圖,邊權爲a和b,你可以走m步,問是否有可能走出迴文串

分析

首先考慮m爲奇數的情況,如果兩點間的兩條邊相同,那麼肯定是迴文串,如果不相同,形如aba也可以構成迴文串

接着考慮m爲偶數,我們知道如果是偶數那麼中間兩個肯定是相同的,我們只要找到連着相同的兩條邊就可以。在三個點之間肯定能形成這種情況,可以自己模擬一下試試,因爲是完全圖,所以當n>=3時肯定可以構造出來的

最後再看n=2的情況,奇數條邊已經考慮過了,偶數邊要滿足必須要雙向邊都相同

Code

#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
//#define ACM_LOCAL
#define fi first
#define se second
const int N = 1e6 + 10;
const int M = 5e5 + 10;
const int INF = 0x3f3f3f3f;
const double eps = 1e-4;
const int MOD = 1e9+7;
typedef long long ll;
typedef pair<int, int> PII;
char mp[1005][1005];
void solve() {
   
   
    int T; cin >> T; while (T--) {
   
   
        int n, m; cin >> n >> m;
        for (int i = 1; i <= n; i++) cin >> mp[i] + 1;
        if (m & 1) {
   
   
            cout << "YES" << endl;
            for (int i = 1; i <= m+1; i++) cout << i % 2 + 1 << " ";
            cout << endl;
            continue;
        }
        if (n == 2) {
   
   
            if (mp[1][2] == mp[2][1]) {
   
   
                cout << "YES" << endl;
                for (int i = 1; i <= m+1; i++) cout << i % 2 + 1 << " ";
                cout << endl;
            } else cout << "NO" << endl;
        } else {
   
   
            cout << "YES" << endl;
            int flag;
            if (mp[1][2] == mp[2][3]) flag = 1;
            else if (mp[3][1] == mp[1][2]) flag = 0;
            else flag = 2;
            for (int i = 0; i <= m; i++) cout << (flag + i + m) % 3 + 1 << " ";
            cout << endl;
        }
    }
}

signed main() {
   
   
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
#ifdef ACM_LOCAL
    freopen("input", "r", stdin);
    freopen("output", "w", stdout);
#endif
    solve();
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章