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

题意

题目链接

005S5cb6ly1fynqy9j2yzj30ep081gmh.jpg

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;
}

猜你喜欢

转载自blog.csdn.net/weixin_34085658/article/details/86032759