cf623A. Graph and String(二分图 构造)

Posted zwfymqz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cf623A. Graph and String(二分图 构造)相关的知识,希望对你有一定的参考价值。

题意

题目链接

技术分享图片

Sol

可以这样考虑,在原图中没有边相连的点的值肯定是a / c

那么直接二分图染色即可

#include<bits/stdc++.h>
#define LL long long 
using namespace std;
const int MAXN = 1001, INF = 1e9 + 10;
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int N, M, mp[MAXN][MAXN], col[MAXN], inder[MAXN];
bool flag = 1;
void dfs(int x) {
    for(int i = 1; i <= N; i++) 
        if(!mp[x][i]) 
            if(col[i] == -1) col[i] = col[x] ^ 1, dfs(i);
}
int main() {
    N = read(); M = read();
    for(int i = 1; i <= N; i++) mp[i][i] = 1;
    for(int i = 1; i <= M; i++) {
        int x = read(), y = read();
        mp[x][y] = mp[y][x] = 1; 
    }
    for(int i = 1; i <= N; i++)
        for(int j = i + 1; j <= N; j++)
            if(!mp[i][j]) inder[i]++, inder[j]++;
    memset(col, -1, sizeof(col));
    for(int i = 1; i <= N; i++) if(col[i] == -1) col[i] = 0, dfs(i);
    for(int i = 1; i <= N; i++) {
        for(int j = i + 1; j <= N; j++) {
            if(!mp[i][j] && (col[i] == col[j])) {puts("NO"); return 0;}
            if(mp[i][j] && inder[i] && inder[j] && (col[i] != col[j])) {puts("NO"); return 0;}
        }
    }
    puts("Yes");
    for(int i = 1; i <= N; i++) 
        if(inder[i] == 0) putchar('b');
        else if(col[i] == 1) putchar('c'); 
        else putchar('a');  
    return 0;
}

以上是关于cf623A. Graph and String(二分图 构造)的主要内容,如果未能解决你的问题,请参考以下文章

[Codeforces 623A] Graph and String

codeforces 623A. Graph and String 构造

CF 990D Graph And Its Complement 第十八 构造思维

CF1109D Sasha and Interesting Fact from Graph Theory 组合数

CF623E Transforming Sequence

CF1082G Petya and Graph