OJ-ID:
    POJ-1129

author:
    Caution_X

date of submission:
    20190927

tags:
    DFS+四色原理的应用

description modelling:
    给定n个点的无向连通图,问至少需要几种颜色可以完成染色

major steps to solve it:
    1.任选从一点开始染色
    2.DFS不断向其他点进行染色
    3.所有点都染过色,结束DFS

warnings:
    根据四色原理,所有的图都可以用四种颜色完成染色

AC code:

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cctype>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define ll long long
#define ull unsigned long long
using namespace std;
int n, ans, ok, vis[28];
bool ma[28][28];
bool check(int u, int sb)
{
    for (int i = 1; i <= n; i++)
        if (ma[u][i] && vis[i] == sb) {
            return 0;
        }
 
    return 1;
}
void dfs(int u, int s)
{
 
    if (ok) {
        return ;
    }
 
    if (u == n + 1) {
        ans = s;
        ok = 1;
        return ;
    }
 
    for (int k = 1; k <= s; k++) {
        if (check(u, k)) {
            vis[u] = k;
            dfs(u + 1, s);
        }
    }
 
    vis[u] = ++s;
    dfs(u + 1, s);
}
int main()
{
    char s[38];
 
    while (scanf("%d", &n) != EOF && n) {
        memset(ma, 0, sizeof(ma));
        memset(vis, 0, sizeof(vis));
        getchar();
 
        for (int i = 1; i <= n; i++) {
            scanf("%s", s);
            int len = strlen(s);
 
            for (int j = 2; j <= len - 1; j++) {
                ma[i][s[j] - 'A' + 1] = 1;
                ma[s[j] - 'A' + 1][i] = 1;
            }
        }
 
        ok = 0;
        dfs(1, 1);
 
        if (ans == 1) {
            puts("1 channel needed.");
        } else {
            printf("%d channels needed.\n", ans);
        }
    }
 
    return 0;
}

POJ-1129 DFS染色+四色原理的应用的更多相关文章

  1. poj 1129(dfs+图的四色定理)

    题目链接:http://poj.org/problem?id=1129 思路:根据图的四色定理,最多四种颜色就能满足题意,使得相邻的两部分颜色不同.而最多又只有26个点,因此直接dfs即可. #inc ...

  2. Channel Allocation (poj 1129 dfs)

    Language: Default Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12 ...

  3. 【POJ - 2386】Lake Counting (dfs+染色)

    -->Lake Counting 直接上中文了 Descriptions: 由于近日阴雨连天,约翰的农场中中积水汇聚成一个个不同的池塘,农场可以用 N x M (1 <= N <= ...

  4. 迭代加深搜索 POJ 1129 Channel Allocation

    POJ 1129 Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14191   Acc ...

  5. cf804C(dfs染色)

    题目链接: http://codeforces.com/problemset/problem/804/C 题意: 有一颗含有 n 个顶点的树, 第 i 个顶点上有 k 个冰激凌, 每个冰激凌的种类为 ...

  6. .NET应用架构设计—面向对象分析与设计四色原型模式(彩色建模、领域无关模型)(概念版)

    阅读目录: 1.背景介绍 2.问自己,UML对你来说有意义吗?它帮助过你对系统进行分析.建模吗? 3.一直以来其实我们被一个缝隙隔开了,使我们对OOAD遥不可及 4.四色原型模式填补这个历史缝隙,让我 ...

  7. DDD:四色原型中Role的 “六” 种实现方式

    背景 一个实体在不同的上下文中具备不同的职责,如:产品在“生产完成上下文”中具备的一些职责,在“质检相关上下文”中具备另外一些职责.四色原型.DIC和“UML事物模式”在不同的维度阐述了这一情况,在代 ...

  8. hdu 4751(dfs染色)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4751 思路:构建新图,对于那些两点连双向边的,忽略,然后其余的都连双向边,于是在新图中,连边的点是能不 ...

  9. hdu 5313 Bipartite Graph(dfs染色 或者 并查集)

    Problem Description Soda has a bipartite graph with n vertices and m undirected edges. Now he wants ...

随机推荐

  1. String substring(int start,int end)截取当前字符串中指定范围内的字符串

    package seday01;/** * String substring(int start,int end) * 截取当前字符串中指定范围内的字符串. * java api有一个特点:通常用两个 ...

  2. SpringBoot(五) SpringBoot整合mybatis

    一:项目结构: 二:pom文件如下: <parent> <groupId>org.springframework.boot</groupId> <artifa ...

  3. java.lang.IllegalArgumentException: java.util.zip.ZipException: invalid LOC header (bad signature)异常解决方法

    java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start com ...

  4. SQLi-LABS Page-3 (Stacked injections) Less-38-Less-45

    Less-38 堆叠注入原理简介堆叠注入简介 Stacked injections: 堆叠注入.从名词的含义就可以看到应该是一堆 sql 语句(多条)一起执行.而在真实的运用中也是这样的, 我们知道在 ...

  5. javascript 生成指定范围内的随机数

    js 生成任意2个区间内的随机整数,js 生成两个数之间的随机数 function random(m,n) { return Math.floor(Math.random() * (n - m)) + ...

  6. jsp页面格式化时间 fmt:formatDate格式化日期

    使用fmt函数需在jsp中引入 <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" ...

  7. Shell命令-搜索文件或目录之whereis、locate

    文件及内容处理 - whereis.locate 1. whereis:查找二进制命令,按环境变量PATH路径查找 whereis命令的功能说明 whereis 命令用于查找文件.该指令会在特定目录中 ...

  8. Hadoop序列化案例实操

    需求 统计每一个手机号耗费的总上行流量.下行流量.总流量. 输入数据: 1 13736230513 192.196.100.1 www.atguigu.com 2481 24681 200 2 138 ...

  9. Firefox 与 geckodriver 版本兼容问题

    打开 python shell,执行以下脚本: from selenium import webdriverdriver = webdriver.Firefox()driver.maximize_wi ...

  10. TensorFlow Federated:基于分散式数据的机器学习

    https://www.tensorflow.org/federated/ TensorFlow Federated (TFF) 是一个开源框架,用于对分散式数据进行机器学习和其他计算.我们开发 TF ...