Poj 1129 Channel Allocation (dfs)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.