Coding/USACO Bronze

[USACO Bronze 1] The Great Revegetation

xeohyuni 2023. 11. 19. 14:45

Question

A lengthy drought has left Farmer John's  pastures devoid of grass. However, with the rainy season arriving soon, the time has come to "revegetate".

In Farmer John's shed, he has four buckets, each with a different type of grass seed. He wishes to sow each pasture with one of these types of seeds. Being a dairy farmer, Farmer John wants to make sure each of his cows has a varied diet. Each of his  cows has two favorite pastures, and he wants to be sure different types of grass are planted in each, so every cow can choose between two types of grass. Farmer John knows that no pasture is a favorite of more than 3 cows.

Please help Farmer John choose a grass type for each pasture so that the nutritional needs of all cows are satisfied.

Input

The first line of input contains  (2≤N≤100) and  (1≤M≤150). Each of the next  lines contains two integers in the range 1…N, describing the pair of pastures that are the two favorites for one of Farmer John's cows.

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

Output

Output an -digit number, with each digit in the range 1…4, describing the grass type to be planted in each field. The first digit corresponds to the grass type for field , the second digit to field 2, and so on. If there are multiple valid solutions, print only the -digit number that is smallest among all of them.

12133

 

Code

N, M = map(int,input().split())
land = [1] * N
pair = []
for i in range(M):
    a,b  = map(int, input().split())
    pair.append([a-1, b-1])
    pair.append([b-1, a-1])

for a,b in sorted(pair):
    if land[a] == land[b]:
        if a<b:
            land[b]+=1
        else:
            land[a]+=1
print("".join(map(str, land)))

Explanation

In this code, Farmer John aims to assign different grass types to each pasture based on the preferences of his cows. The input specifies the number of pastures (N) and cows (M), along with pairs of favorite pastures for each cow. The code initializes an array 'land' to represent the grass type for each pasture, setting them all initially to 1.

The crucial aspect of the code lies in the processing of cow preferences. The pairs of favorite pastures are iterated through, and for each pair, the code checks if the assigned grass types are the same. If they are, it ensures that the grass types for the two pastures differ, satisfying the condition that no pasture is a favorite for more than 3 cows.

By incrementing(increasing the number of variables in certain amount) the grass type for one of the pastures, the code maintains diversity in the assigned grass types across pastures. The sorting of the pairs helps ensure that the code starts with pairs involving pastures with lower indices, minimizing the resulting N-digit number. The code then prints this number, providing Farmer John with a solution that satisfies the dietary preferences of his cows and adheres to the given constraints. The approach is designed to find the smallest valid solution among multiple possibilities.

'Coding > USACO Bronze' 카테고리의 다른 글

[USACO Bronze 1] Haybale Stacking 5912  (1) 2023.12.08
[USACO Bronze 1] Blocked Billboard II  (0) 2023.11.19
[USACO 2017 Bronze 3] Milk Measurement  (2) 2023.11.19