cf623A. Graph and String(二分圖 構造)
阿新 • • 發佈:2018-12-29
turn name size its ref != names 直接 const
題意
題目鏈接
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(二分圖 構造)