PAT Advanced 1154 Vertex Coloring (25) [set,hash]
题目
A 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 10^4), 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. Afer 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
题目分析
已知图每条边的顶点信息,查询已知条件为图每个顶点的颜色。要求在查询已知条件下图每条边两个顶点颜色不同,满足条件条件输出K-coloring(K为颜色数),否则输出No
解题思路
- 定义结构体edge,记录每条边两个顶点的数字
- 定义edge es[M],记录所有边的顶点信息
- 定义int acs[N],记录每个查询条件中每个顶点的颜色;set cs,记录每个查询条件颜色数
- 遍历所有边,验证查询条件每个顶点的颜色是否满足每条边两个顶点颜色不同
知识点
- 局部定义bool flag;
局部循环中定义bool flag,某次循环将flag=true后,下一次循环执行到bool flag,并不会将flag重新初始化为false,而是使用上一次循环执行的结果 - set
set<int> s1; //初始化
s1.insert(10); //插入
//set<int>::iterator pos = s1.find(30); if (pos != s1.end()){//可找到} //查找
//int num = s1.count(30); //统计
Code
Code 01
#include <iostream>
#include <set>
using namespace std;
struct edge {
int left,right;
};
int main(int argc,char * argv[]) {
int N,M,K,T;
scanf("%d %d",&N,&M);
edge es[M];
for(int i=0; i<M; i++) {
scanf("%d %d",&es[i].left,&es[i].right);
}
scanf("%d",&K);
for(int i=0; i<K; i++) {
int acs[N] = {0};
set<int> cs;
for(int j=0;j<N;j++){
scanf("%d",&acs[j]);
cs.insert(acs[j]);
}
int j;
for(j=0;j<M;j++){
if(acs[es[j].left]==acs[es[j].right])break;
}
if(j==M)printf("%d-coloring\n",cs.size());
else printf("No\n");
}
return 0;
}
Code 02
#include <iostream>
#include <set>
using namespace std;
struct edge {
int left,right;
};
int main(int argc,char * argv[]) {
int N,M,K,T;
scanf("%d %d",&N,&M);
edge es[M];
for(int i=0; i<M; i++) {
scanf("%d %d",&es[i].left,&es[i].right);
}
scanf("%d",&K);
for(int i=0; i<K; i++) {
int acs[N] = {0};
set<int> cs;
for(int j=0;j<N;j++){
scanf("%d",&acs[j]);
cs.insert(acs[j]);
}
bool flag=false;// 如果不写=false;初始化是false,但是之后的循环,并不会重置为false,而依旧使用的是上一次循环处理结束的值
for(int j=0;j<M;j++){
if(acs[es[j].left]==acs[es[j].right]){
flag = true;
break;
}
}
if(!flag)printf("%d-coloring\n",cs.size());
else printf("No\n");
}
return 0;
}
PAT Advanced 1154 Vertex Coloring (25) [set,hash]的更多相关文章
- PAT Advanced 1154 Vertex Coloring (25 分)
A proper vertex coloring is a labeling of the graph's vertices with colors such that no two vertices ...
- pat甲级 1154 Vertex Coloring (25 分)
A proper vertex coloring is a labeling of the graph's vertices with colors such that no two vertices ...
- PAT 甲级 1154 Vertex Coloring
https://pintia.cn/problem-sets/994805342720868352/problems/1071785301894295552 A proper vertex color ...
- PAT Advanced 1134 Vertex Cover (25) [hash散列]
题目 A vertex cover of a graph is a set of vertices such that each edge of the graph is incident to at ...
- PTA 1154 Vertex Coloring
题目链接:1154 Vertex Coloring A proper vertex coloring is a labeling of the graph's vertices with colors ...
- PAT Advanced 1048 Find Coins (25) [Hash散列]
题目 Eva loves to collect coins from all over the universe, including some other planets like Mars. On ...
- PAT Advanced 1020 Tree Traversals (25 分)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
- PAT (Advanced Level) 1078. Hashing (25)
二次探测法.表示第一次听说这东西... #include<cstdio> #include<cstring> #include<cmath> #include< ...
- PAT (Advanced Level) 1070. Mooncake (25)
简单贪心.先买性价比高的. #include<cstdio> #include<cstring> #include<cmath> #include<vecto ...
随机推荐
- ES6 之 Proxy
概述 Proxy 用于修改某些操作的默认行为,等同于在语言层面做出修改. Proxy 可以理解在目标对象架设一个“拦截”层外界对该对象的访问都必须先通过这层拦截,因此提供了一种机制可以对外界的访问进行 ...
- Mac修改用户名后程序配置和文件都找不到了?
小编今天手残,修改mac 的用户名,幸亏文件没丢失,要不然配置程序估计至少要花费周末的两天时间了.. 所幸的是,各种Google,终于找回了原用户名下的所有配置. 接下来,讲讲小编如何入坑又如何脱坑的 ...
- es6 part 1 //const let
1. let 命令 ES6 新增了let命令,用来声明变量.它的用法类似于var,但是所声明的变量,只在let命令所在的代码块内有效. { let a = 10; var b = 1; } a // ...
- 2016蓝桥杯省赛C/C++A组第八题 四平方和
题意: 四平方和定理,又称为拉格朗日定理: 每个正整数都可以表示为至多4个正整数的平方和. 如果把0包括进去,就正好可以表示为4个数的平方和. 比如: 5 = 0^2 + 0^2 + 1^2 + 2^ ...
- windows driver 获取本地时间
#define ArrayLength 260 void MyGetLocalTime() { LARGE_INTEGER li_system; LARGE_INTEGER li_Local; cha ...
- 14 ~ express ~ 显示用户数据
一,router/admin.js var express = require('express') var router = express.Router() var User = require( ...
- Vulkan 开发学习资料汇总
开发资料汇总 1.API Reference 2.Vulkan Spec 有详细说明的pdf 文章 1.知乎Vulkan-高性能渲染 2.Life of a triangle - NVIDIA's l ...
- 三、在SAP中文本如何换行
一.在一段文字前面,加上右斜杠符合\ ,这句话就会换行了,如图: 效果如下:
- 第二阶段scrum-3
1.整个团队的任务量: 2.任务看板: 会议照片: 产品状态: 前端制作完成,数据库正在配置
- Javascript里EQ、NE、GT、LT、GE、LE含义
EQ 就是 EQUAL等于 NE就是 NOT EQUAL不等于 GT 就是 GREATER THAN大于 LT 就是 LESS THAN小于 GE 就是 GREATER THAN OR EQUAL ...