zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    MySQL 命令(一)
    HTML5 表单新增元素与属性
    怎样用SQL修改某个字段的部分内容
    百度sitemap.xml
    Dedecms自定义表单后台列表展现方式样式更改
    织梦seo
    织梦DedeCMS自定义表单diy_list.htm
    织梦采集文章
    播放视频插件swfobject.js与Video Html5
    织梦简单的自定义表单字段
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/10197225.html
Copyright © 2011-2022 走看看