Link: poj 1129
If the adjacent repeater uses different channels, it will not interfere with each other.
Given the adjacent relationship of some relay, you must select at least several different channels to avoid mutual interference between the relay.
Analysis: This problem can be converted into an undirected graph dyeing problem,
That is, the adjacent points cannot be colored in the same color. How many colors are needed?
The maximum number of vertices in this question is 26. You can simply use a brute force search.
Idea: if the total number of vertices is n, a maximum of n colors (1, 2... n) are required ),
From the smallest vertex, each time it is marked with the color that is adjacent to the vertex,
Then select a minimum color from the unmarked (unused) color to dye the color,
After all vertices are dyed, several colors are used to calculate the answer.
Note: If the answer is 1, the output channel is singular; otherwise, it is a plural channel.S
# Include <stdio. h> # include <string. h> int main () {int n, I, j, vis [30], color [30], sum; char s [30]; while (scanf ("% d", & n )! = EOF) {if (n = 0) break; memset (color, 0, sizeof (color); for (I = 0; I <n; I ++) {scanf ("% s", s); memset (vis, 0, sizeof (vis); for (j = 2; s [j]! = '\ 0'; j ++) // Mark the color of the adjacent vertex if (color [s [j]-'A']) vis [color [s [j]-'A'] = 1; for (j = 1; j <= n; j ++) // Find the smallest unused color and dye vertex I if (! Vis [j]) {color [I] = j; // dye vertex I in the j color break;} memset (vis, 0, sizeof (vis )); sum = 0; for (I = 0; I <n; I ++) {// count the different colors used if (! Vis [color [I]) {sum ++; vis [color [I] = 1 ;}}if (sum = 1) printf ("% d channel needed. \ n ", sum); else printf (" % d channels needed. \ n ", sum);} return 0 ;}
Poj 1129 Channel Allocation (dfs)