首页 > 其他分享 >1154 Vertex Coloring

1154 Vertex Coloring

时间:2023-08-25 22:55:24浏览次数:28  
标签:Coloring No color 1154 Vertex edge int coloring each

题目

proper vertex coloring is a labeling of the graph's vertices with colors such that no two vertices sharing the same edge have the same color. A coloring using at most k colors is called a (proper) k-coloring.

Now you are supposed to tell if a given coloring is a proper k-coloring.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers N and M (both no more than 104), being the total numbers of vertices and edges, respectively. Then M lines follow, each describes an edge by giving the indices (from 0 to N−1) of the two ends of the edge.

After the graph, a positive integer K (≤ 100) is given, which is the number of colorings you are supposed to check. Then K lines follow, each contains N colors which are represented by non-negative integers in the range of int. The i-th color is the color of the i-th vertex.

Output Specification:

For each coloring, print in a line k-coloring if it is a proper k-coloring for some positive k, or No if not.

Sample Input:

10 11
8 7
6 8
4 5
8 4
8 1
1 2
1 4
9 8
9 1
1 0
2 4
4
0 1 0 1 4 1 0 1 3 0
0 1 0 1 4 1 0 1 0 0
8 1 0 1 4 1 0 5 3 0
1 2 3 4 5 6 7 8 8 9

Sample Output:

4-coloring
No
6-coloring
No

 

题目大意:给出一个图(先给出所有边,后给出每个点的颜色),问是否满足:所有的边的两个点的颜色不相同

 

思路:

1、使用结构体数组来存储边,然后遍历所有的边,若存在边上两个端点的颜色相同的情况就输出No

2、可以利用集合来计算不同颜色的个数

 

代码:

#include<stdio.h>
#include<iostream>
#include<set>
#include<string.h>
int n, m, k;
bool v[10005][10005] = {0};
int color[10005];
using namespace std;
struct Edge{
    int node1, node2;
}edge[10005];
int main(){
    scanf("%d%d", &n, &m);
    for(int i = 0; i < m; i++){
        int x, y;
        scanf("%d%d", &x, &y);
        edge[i].node1 = x;
        edge[i].node2 = y;
    }
    scanf("%d", &k);
    for(int i = 0; i < k; i++){
        set<int> se;
        memset(color, -1, sizeof(color));
        for(int j = 0; j < n; j++){
            scanf("%d", &color[j]);
            se.insert(color[j]);
        }
        bool flag = true;
        for(int j = 0; j < m; j++){
            if(color[edge[j].node1] == color[edge[j].node2]){
                flag = false;
                break;
            }
        }
        if(flag){
            printf("%d-coloring\n", se.size());
        }else{
            printf("No\n");
        }
    }
    return 0;
}

 

标签:Coloring,No,color,1154,Vertex,edge,int,coloring,each
From: https://www.cnblogs.com/yccy/p/17658123.html

相关文章

  • com.alibaba.excel.exception.ExcelWriteDataConvertException: Can not find 'Conver
    这个异常是由于使用阿里巴巴的EasyExcel库时,没有找到映射为Map类型的数据转换器所导致的。在使用EasyExcel进行Excel文件读写时,需要指定正确的数据转换器以实现对象与Excel单元格的相互转换。对于Map类型的数据,EasyExcel需要知道如何将Map转换为Excel中的单元格数据,因此需要自定义......
  • 【CDX随笔总结】P1_Vertex 的整理和分析【未完成,持续编写】
    效果图提交单:https://github.com/CartmanORCamille/CDX/commit/afc7a52fc96466ddb1ab5233e4986bb739037e33关键点渲染管线基础。C与C++交叉编译和全局变量。位表(键盘事件,摄像机视角与观察点)。渲染几何体基础画一个正方体【猜测】demo里(gif图)在旋转的时候看不到底......
  • Coloring Tree (牛客多校) (BFS序列妙用+ f(n)-f(n+1)+ 组合数学)
    题目大意:给一个树,然后有k种颜色可以给树上色权值是2个相同颜色节点的最短距离问让权值为D的方案数 题解:首先要让2个节点为D,怎么处理呢?利用f(D)-f(D+1)即可因为问的是2个相同颜色点的最短距离,因此直接bfs用一个bfs序列然后在bfs一下,因为之前co......
  • 谷歌大模型云服务Vertex AI上线
    想让AI帮你解释代码为什么出错?用谷歌的大模型服务。 上周末,谷歌宣布基于VertexAI的生成式人工智能服务全面上线了。VertexAI是谷歌云提供的机器学习平台服务(MLPaaS)。随着本次发布,谷歌大模型的服务已普遍可用,企业和组织现在可以将该平台的功能与自身应用进行......
  • [ICDE 2023] Minimizing the Influence of Misinformation via Vertex Blocking
    MinimizingtheInfluenceofMisinformationviaVertexBlockingMotivationandApplication其实就是经典的RumorBlocking问题,即通过一系列的操作使得rumor在社交网络中的影响力最小。主流的方法有三种:找到一组seedset去和rumor节点竞争,社交网络中的节点都只能被激活一次,......
  • Codeforces 1444E - Finding the Vertex
    非常神秘的一道题,当之无愧的*3500。首先考虑转化题意。考虑一种决策树,由于我们每次问一条边之后,相当于会根据信息删掉两个连通块中的一个,因此一种决策树实际上对应了原树的一棵边分树。而为了让最坏情况下的询问次数最少,我们目标实际上是最小化边分树的深度。考虑借鉴P5912JA......
  • Codeforces Round #552 (Div. 3) 1154
    A:传送门就是解个方程,也没什么说的#include<bits/stdc++.h>usingnamespacestd;intmain(){inta[4],x,b,c;for(inti=0;i<4;i++)cin>>a[i];sort(a,a+4);c=a[3]-a[0];x=a[1]-c;b=a[2]-c;cout<<x......
  • AtCoder Beginner Contest 223 G Vertex Deletion
    洛谷传送门AtCoder传送门设\(f_{u,0/1}\)为\(u\)的子树,\(u\)是否在匹配内的最大匹配数。注意到对于一个匹配,在它深度较浅的点上才会被计入答案。转移大概是\(f_{u,0}\)取\(\sum\limits_{v\inson_u}\max(f_{v,0},f_{v,1})\),\(f_{u,1}\)要从儿子中选一个\(f_{v,0......
  • Atcoder Grand Contest 059 E - Grid 3-coloring(转化+思维)
    首先先是一步很猛的操作——将三染色视作构造一个矩阵使得相邻元素相差\(1\)且每个元素\(\bmod3\)的值就等于其颜色。证明是显然的,我们按从上到下从左到右的顺序填数,可以归纳证明,对于一个相邻格子颜色互不相同的矩阵的填数方案,处于斜对角的两个格子上写的数要么差\(2\),要么......
  • Codeforces 1781G - Diverse Coloring(构造)
    vp时候想到大致思路了,但是死活调不对,赛后套取cf的数据调了好久才过/ll首先直觉告诉我们答案不会太大。稍微猜一猜可以猜出除了四个点的菊花之外答案都是\(n\bmod2\),下面我们来通过构造证明这件事情。首先,链的情况是trivial的,直接根据奇偶性间隔染色即可。如果不是链,那么......