Analysis of USACO past bronze group real questions | The Great Revegetation in February 2019

Learn C++ from a young age! Record the questions in the study process of preparing for the USACO (American Informatics Olympiad) bronze category competition and record every moment.

Attached is a summary post:Analysis of USACO past bronze group real questions | Summary-CSDN Blog


[Title description]

A long drought has left Farmer John's N grasslands short of pasture. With the rainy season approaching, now should be the time to replant.

In Farmer John's storage shed are four buckets, each containing a different grass seed. He wanted to seed each patch of grass with one of these grasses. As a dairy farmer, Farmer John wants to make sure each of his cows gets a rich diet. Each of his M cows has two favorite pastures, and he wants to make sure that the two pastures are planted with different types of grass so that each cow has two grasses to choose from. Farmer John knows that no patch of grass is loved by more than three cows.

Please help Farmer John choose the type of grass to plant in each field so that the nutritional needs of all cows are met.

【enter】

The first line of input contains N (2≤N≤100) and M (1≤M≤150). The following M lines, each line contains two integers in the range 1...N, are the two patches of grass that one of Farmer John's cows likes.

【Output】

Output an N number of digits, each digit is one of 1...4, indicating the type of grass planted on each piece of grass. The first digit corresponds to the type of grass in grassland 1, the second digit corresponds to grassland 2, and so on. If there are multiple feasible solutions, just output the smallest N number of digits among all solutions.

【Input sample】

5 6
4 1
4 2
4 3
2 5
1 2
1 5

【Output sample】

12133

[Detailed code explanation]

// 尝试用过贪心算法,后发现只有3个测试点通过,其余均超时
#include <bits/stdc++.h>
using namespace std;
int a1[105][105], G[105];
int n, m;
ifstream filein("revegetate.in");
ofstream fileout("revegetate.out");
int main()
{
    filein >> n >> m;  // 输入n和m

    for (int i=1; i<=m; i++) {  // 遍历m条边
        int a, b;
        filein >> a >> b;  // 输入边的端点
        a1[a][b] = 1;  // 因为是无向图,所以边的两个方向均置为1
        a1[b][a] = 1;
    }
    for (int i=1; i<=n; i++) {  // 遍历n个顶点
        int k;  // 定义颜色(没放到循环中是因为后面要通过G[i]来赋值)
        for (k=1; k<=4; k++) {  // 遍历4个颜色
            bool ok=true;  // 定义ok,用于判断边的两个端点颜色是否冲突
            for (int j=1; j<=n; j++) {  // 再遍历n个顶点
                if (a1[i][j]==1 && G[j]==k) {  // 如果i和j是一条边上的两个顶点,且j这个点的颜色为k(即已经有颜色了)
                    ok =false;  // 修改ok为false
                }
            }
            if (ok) break;  // 没有一条边上的两个顶点颜色冲突,则退出循环,即颜色留在当前k上
        }
        G[i] = k;  // 将这个k赋值给G[i]
    }
    for (int i=1; i<=n; i++) {  // 遍历n个顶点
        fileout << G[i];  // 将每个顶点的颜色打印出来,中间不空格
    }
    fileout << endl;
    return 0;
}

【operation result】

5 6
4 1
4 2
4 3
2 5
1 2
1 5
12133

Guess you like

Origin blog.csdn.net/guolianggsta/article/details/134865782