PAT Advanced 1154 Vertex Coloring (25) [set,hash]

topic

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

The meaning of problems

FIG vertex of each edge information is known, the condition is known as the query color of each vertex of FIG. Under known conditions required query FIG two vertex color different from each edge, the output condition satisfies the condition K-coloring (K is the number of colors), otherwise the output No

Topic analysis

  1. Definition of the structure edge, each edge recording digital two vertices
  2. Defined edge es [M], the vertex information recording all edges
  3. Defined int acs [N], the color of each vertex of each of the recording query criteria; set cs, record the number of colors each query
  4. Traversing all edges of the color of each vertex, the authentication request condition is satisfied each edge two vertices of different colors

Knowledge Point

  1. Local definition flag BOOL;
    after topical defined in the cycle bool flag, a particular cycle flag = true, the next cycle to execute bool flag, flag will not be re-initialized to false, but the use of a result of execution of the loop
  2. 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;
}


Guess you like

Origin www.cnblogs.com/houzm/p/12241041.html