热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

芽衣:染上你的颜色

染色初步给图上的顶点指定颜色,使图上任何临接的顶点都被染成不同的颜色我们想要知道的是最少使用几种颜色以及具体方案《组合数学》上面有个贪心的染色法设G是图,它的顶点按某一

染色初步...

给图上的顶点指定颜色,使图上任何临接的顶点都被染成不同的颜色

我们想要知道的是最少使用几种颜色以及具体方案

《组合数学》上面有个贪心的染色法...

设G是图,它的顶点按某一顺序记为x1,x2, ..., xn。

(1)对顶点x1指定颜色1

(2)对每个i=2,3,...,n,令p是不与xi邻接的顶点的颜色重复的, (序号)最小的颜色,对xi指定颜色p

Problem H. Path or Coloring

技术分享

Input file: Output file: Time limit: Memory limit:

pathorcoloring.in pathorcoloring.out 1 second
256 mebibytes

Do you like NP-hard problems? For example, let us consider the following two:

Coloring problem. You are given an undirected graph G and an integer k. For each vertex of the graph, choose color φ(v) so that all colors are integers between 1 and k, and any two vertices connected by an edge have different color.

Simple k-path problem. You are given an undirected graph G and an integer k. Find a simple path of length k. Formally, you need to choose such sequence of k + 1 unique vertices v1, v2, . . . , vk+1 that any two consecutive verices are connected by an edge.

You are given an undirected graph G and an integer k. Solve any one of these two problems for them. The choice is yours.

Input

The first line of input contains an integer T , the number of test cases (1 ≤ T ≤ 1000). The description of each test case consists of several lines.

The first line of each description of containts two integers n and m: the number of vertices and the number of edges in the graph (1 ≤ n ≤ 1000, 1 ≤ m ≤ 10 000). On the next line, the integer k is given (1 ≤ k ≤ n). Each of the next m lines contains two integers a and b: the numbers of two verices connected by an edge (1 ≤ a, b ≤ n).

It is guaranteed that the given graphs have no self-loops and no multiple edges. Additionally, the total number of edges in all given graphs does not exceed 100 000.

Output

For each test case, print one of the following:

? The word “path” followed by k + 1 numbers of vertices: a simple path of length k.
? The word “coloring” followed by n integers from 1 to k: a coloring of the graph into k colors. ? The word “neither” if there is no simple path of length k and no coloring into k colors.

If there exist both a valid path and a valid coloring, you can print either a path or a coloring. If there are several valid paths or several valid colorings, you can print any one path or any one coloring, respectively.

技术分享

尼玛啊我不喜欢NPhard问题!

给出一个简单图,和一个数字k,要求任意选一个问题做答:

1.输出图的一个k染色方案

2.输出图的一个长度为k的简单路径

如果两种都无解输出neither

不知道怎么做k长简单路径,就g了。实际上:

如果有k染色方案,输出即可

如果没有k染色方案,一定存在k长简单路径,neither不存在的

因为染色数小于等于最长路径:

考虑完全图Kn,染色数为n,最长路径n。

任意删去一些边(保持连通),染色数会减小或不变

如果在贪心染色时,在给随意一个点指定颜色1后,从这个点邻接的点进行接下来的染色(dfs序)

那这个路径就是:颜色1,2, ..., k, k+1对应的顶点

技术分享技术分享
#include
const int maxn = 1e3+7, maxm = 2e4+7;
int v[maxm], col[maxn], vis[maxn], head[maxn], nxt[maxm];
int n, m, k, tot, pos;
void addedge(int x, int y){
    v[++tot] = y;
    nxt[tot] = head[x];
    head[x] = tot;
}
void dfs(int u){
    pos++;
    for(int i = head[u]; i; i = nxt[i]){
        if(col[v[i]]){
            //printf("pos : %d ", pos);
            //printf("near %d is %d\n", u, v[i]);
            vis[col[v[i]]] = pos;
        }
    }
    for(int i = 1; ; i++){
        if(vis[i] < pos){
            col[u] = i;
            //printf("pos : %d ", pos);
            //printf("give %d color %d\n", u, i);
            break;
        }
    }
    for(int i = head[u]; i; i = nxt[i]){
        //printf("%d %d %d col v i %d\n", pos, i, v[i], col[v[i]]);
        if(!col[v[i]])
            dfs(v[i]);
    }
}
void solve(){
    scanf("%d%d%d", &n, &m, &k);
    for(int i = 1; i <= n; i++)
        col[i] = head[i] = vis[i] = 0;
    pos = tot = 0;
    while(m--){
        int x, y;
        scanf("%d%d", &x, &y);
        addedge(x, y);
        addedge(y, x);
    }
    for(int i = 1; i <= n; i++){
        if(!col[i])
            dfs(i);
    }
    int x = 0;
    bool flag = 1;
    for(int i = 1; i <= n; i++){
        if(col[i] > k){
            x = i;
            flag = 0;
            break;
        }
    }
    /*for(int i = 1; i <= n; i++)
        printf(" %d", col[i]);
    puts("color");*/
    if(flag){
        printf("coloring");
        for(int i = 1; i <= n; i++)
            printf(" %d", col[i]);
        return;
    }
    printf("path");
    int loop = k;
    printf(" %d", x);
    while(loop){
        for(int i = head[x]; i; i = nxt[i]){
            if(col[v[i]] == loop){
                x = v[i];
                break;
            }
        }
        printf(" %d", x);
        loop--;
    }
}
int main(){
    int t;
    scanf("%d", &t);
    while(t--){
        solve();
        puts("");
    }
    return 0;
}
/*
 1
 5 10 4
 1 2 2 3 3 4 4 5 5 1 1 3 1 4 2 5 4 2 3 5
*/
View Code

芽衣:染上你的颜色


推荐阅读
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 本文详细介绍了Linux中进程控制块PCBtask_struct结构体的结构和作用,包括进程状态、进程号、待处理信号、进程地址空间、调度标志、锁深度、基本时间片、调度策略以及内存管理信息等方面的内容。阅读本文可以更加深入地了解Linux进程管理的原理和机制。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • 动态规划算法的基本步骤及最长递增子序列问题详解
    本文详细介绍了动态规划算法的基本步骤,包括划分阶段、选择状态、决策和状态转移方程,并以最长递增子序列问题为例进行了详细解析。动态规划算法的有效性依赖于问题本身所具有的最优子结构性质和子问题重叠性质。通过将子问题的解保存在一个表中,在以后尽可能多地利用这些子问题的解,从而提高算法的效率。 ... [详细]
  • 高质量SQL书写的30条建议
    本文提供了30条关于优化SQL的建议,包括避免使用select *,使用具体字段,以及使用limit 1等。这些建议是基于实际开发经验总结出来的,旨在帮助读者优化SQL查询。 ... [详细]
  • 本文介绍了指针的概念以及在函数调用时使用指针作为参数的情况。指针存放的是变量的地址,通过指针可以修改指针所指的变量的值。然而,如果想要修改指针的指向,就需要使用指针的引用。文章还通过一个简单的示例代码解释了指针的引用的使用方法,并思考了在修改指针的指向后,取指针的输出结果。 ... [详细]
  • 猜字母游戏
    猜字母游戏猜字母游戏——设计数据结构猜字母游戏——设计程序结构猜字母游戏——实现字母生成方法猜字母游戏——实现字母检测方法猜字母游戏——实现主方法1猜字母游戏——设计数据结构1.1 ... [详细]
  • CentOS 7部署KVM虚拟化环境之一架构介绍
    本文介绍了CentOS 7部署KVM虚拟化环境的架构,详细解释了虚拟化技术的概念和原理,包括全虚拟化和半虚拟化。同时介绍了虚拟机的概念和虚拟化软件的作用。 ... [详细]
  • 本文介绍了一种解析GRE报文长度的方法,通过分析GRE报文头中的标志位来计算报文长度。具体实现步骤包括获取GRE报文头指针、提取标志位、计算报文长度等。该方法可以帮助用户准确地获取GRE报文的长度信息。 ... [详细]
author-avatar
手机用户2602926791
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有