Each of his M cows has two favorite pastures, and he wants to be sure different types of grass are planted in each N pastures.
My 1st Correct Code
#include <iostream>
int main() {
int N, M;
int pasture[101][101];
int seed[101];
std::cin >> N >> M;
for (int i = 1; i <= M; i++) {
int t1, t2;
std::cin >> t1 >> t2;
pasture[t1][t2] = 1;
pasture[t2][t1] = 1;
}
seed[1] = 1;
for (int i = 1; i <= N; i++) {
int tmp[5] = { 0, };
for (int j = 1; j < i; j++) {
if (pasture[i][j] != 1) continue;
tmp[seed[j]] = 1;
}
for (int k = 1; k <= 4; k++) {
if (tmp[k] == 0) {
seed[i] = k;
break;
}
}
}
for (int k = 1; k <= N; k++) {
printf("%d", seed[k]);
}
}
comments
2020-01-19 Can it be shortened?
