POJ--1129-channel allocation [sequential coloring method]

Source: Internet
Author: User

Link:Http://poj.org/problem? Id = 1129

Question:When a broadcast station broadcasts data to a wide area, it uses a repeater to forward signals so that the receiver can receive sufficient signals. Then, the channels used by each relay must be well selected to ensure that adjacent relay do not interfere with each other. To meet this condition, the adjacent repeater must use different channels. However, channels are very valuable. For a repeater network, we should use as few channels as possible. Now we will tell you how many channels are needed to relay network information.


Ideas:Build a diagram based on the question and regard the repeater as a vertex. Two vertices connected by edges should use different channels. Therefore, this is a problem of vertex coloring of a plan. Such problems do not have effective algorithms, the sequential coloring method adopts the greedy idea: When coloring any vertex, the color with the smallest number is used in the adjacent vertex.


# Include <cstring> # include <string> # include <fstream> # include <iostream> # include <iomanip> # include <cstdio> # include <cctype> # include <algorithm> # include <queue> # include <map> # include <set> # include <vector> # include <stack> # include <ctime> # include <cstdlib> # include <functional> # include <cmath> using namespace STD; # define PI ACOs (-1.0) # define maxn 30 # define EPS 1e-7 # define INF 0x3f3f3f3f // 0x7fffffffff # define llinf 0x7ff Fffffffffff # define seed 1313131 # define mod 1000000007 # define ll long # define ull unsigned ll # define lson L, M, RT <1 # define rson m + 1, R, RT <1 | 1 struct node {int U, V, next;} edge [maxn * maxn]; int head [maxn], CNT, N; int ans, v_color [maxn]; // The color of each vertex, int color_use [maxn]; // The identifier used for each color, equal to 1 indicates that void add_edge (int u, int v) {edge [CNT] is used for this color. U = u; edge [CNT]. V = V; edge [CNT]. next = head [u]; head [u] = Cn T ++;} void greedy () {int I, K; for (I = 1; I <= N; I ++) {// color memset (color_use, 0, sizeof (color_use) to vertex I; For (k = head [I]; k! =-1; k = edge [K]. Next) {int v = edge [K]. V; If (v_color [v]! =-1) // The adjacent vertex V has been colored. The color is v_color [v] color_use [v_color [v] = 1 ;}for (k = 1; k <= I; k ++) {// K indicates the color of the smallest number not used in all adjacent vertices of I. The number starts from 1 If (! Color_use [k]) break;} v_color [I] = K; // color the current vertex} for (I = 1; I <= N; I ++) {If (ANS <v_color [I]) ans = v_color [I] ;}} int main () {int I, j; char STR [500]; while (scanf ("% d", & N), n) {CNT = 0; memset (Head,-1, sizeof (head); for (I = 0; I <n; I ++) {scanf ("% s", STR); int u = STR [0]-'A' + 1; int L = strlen (STR); For (j = 2; j <L; j ++) {int v = STR [J]-'A' + 1; add_edge (u, v) ;}}ans = 0; memset (v_color,-1, sizeof (v_color); greedy (); If (ANS = 1) puts ("1 channel needed. "); else printf (" % d channels needed. \ n ", ANS);} return 0 ;}


POJ--1129-channel allocation [sequential coloring method]

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.