Codeforces Round #699 (Div. 2) D. AB Graph 思维+构造

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

目录

    • 题意
    • 分析
    • Code

题意

n个点组成的完全图,边权为a和b,你可以走m步,问是否有可能走出回文串

分析

首先考虑m为奇数的情况,如果两点间的两条边相同,那么肯定是回文串,如果不相同,形如aba也可以构成回文串

接着考虑m为偶数,我们知道如果是偶数那么中间两个肯定是相同的,我们只要找到连着相同的两条边就可以。在三个点之间肯定能形成这种情况,可以自己模拟一下试试,因为是完全图,所以当n>=3时肯定可以构造出来的

最后再看n=2的情况,奇数条边已经考虑过了,偶数边要满足必须要双向边都相同

Code

#pragma GCC optimize(2)
#include 
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();
}


你可能感兴趣的:(思维)