VivoParc

来源:互联网 发布:淘宝售后率高 编辑:程序博客网 时间:2024/06/05 14:33

VivoParc

这里写图片描述
.
.
题意:给n个点和多条边,要求在n个点填充4种颜色之一,使得相邻点颜色不同
.
.
解法:又题意和定理知道一定有解,那么直接暴力就行了
.
.

#include <iostream>#include <stdio.h>#include <stdlib.h>#include <string.h>using namespace std;#pragma comment(linker, "/STACK:1024000000,1024000000")int n, m, x, y, f[3000], tot;bool flag[2000][2000];char c[2000];bool dfs(int x) {    bool fuck[5];    for (int i = 1; i <= 4; i++) fuck[i] = true;    for (int i = 1; i <= n; i++) if (flag[x][i]) {        if (f[i] == 1) fuck[1] = false;        if (f[i] == 2) fuck[2] = false;        if (f[i] == 3) fuck[3] = false;        if (f[i] == 4) fuck[4] = false;    }    for (int k = 1; k <= 4; k++) if (fuck[k]) {        f[x] = k;        bool cur = true;        for (int i = 1; i <= n; i++)if (flag[x][i] && f[i] == 0 && !dfs(i)) {            cur = false;            break;        }        if (cur) return true;    }    f[x] = 0;    return false;}int main() {    tot = 0;    while (scanf("%d", &n) != EOF) {        tot++;        memset(flag, 0, sizeof(flag));        memset(f, 0, sizeof(f));        char cc;        scanf("%c", &cc);        while (1) {            std::cin.getline(c, 1024);            if (strlen(c) == 0) break;            sscanf(c, "%d-%d", &x, &y);             flag[x][y] = true;            flag[y][x] = true;        }        for (int i = 1; i <= n; i++) if (f[i] == 0) {            dfs(i);        }        //if (tot >= 1) cout << endl;        for (int i = 1; i <= n; i++) {            cout << i << " " << f[i] << endl;        }        cout << endl;    }}
1 0
原创粉丝点击