1#include <iostream>2#include <cstdio>3#include <cstdlib>4#include <cstring>5#include <string>6#include <queue>7#include <algorithm>8#include <map>9#include <iomanip>Ten#include <climits> One#include <string.h> A#include <numeric> -#include <cmath> -#include <stdlib.h> the#include <vector> -#include <stack> -#include <Set> - #defineFor (x, B, E) for (int x=b;x<= (e); x + +) + #defineREP (x, N) for (int x=0;x< (n); x + +) - #defineINF 1e7 + #defineMAXN 100010 A #defineMAXN 1000010 at #defineMod 1000007 - #defineN 33 - using namespacestd; -typedefLong LongLL; - - in intG[n][n]; - intCol[n]; to intN, CNT; + BOOLD[n]; - the intMain () * { $ stringtmp;Panax Notoginseng while(Cin >>N, N) { -memset (G,0,sizeof(G)); thememset (Col,0,sizeof(col)); + for(intK =0; K < n; ++k) { ACIN >>tmp; the for(inti =0; Tmp[i +2]; ++i) +g[tmp[0] -'A'+1][tmp[i +2] -'A'+1] =1; - } $ $CNT =0; - intI, J; - for(i =1; I <= N; ++i) { thememset (D,0,sizeof(d)); - for(j =1; J <= I; ++J)//find a conflicting channel numberWuyi if(G[i][j]) d[col[j]] =1;//D[k]==1 says there's a conflict on channel K . the for(j =1;; J + +)//find the channel with the smallest number you can use - if(D[j] = =0) Break; WuCol[i] =J; -CNT =Max (CNT, j); About } $ if(CNT = =1) -printf"%d channel needed.\n", CNT); - Else -printf"%d channels needed.\n", CNT); A } + return 0; the}View Code
1</Pre><pre name ="Code" class="CPP"> #include"stdio.h"2#include"string.h"3#include"algorithm"4 using namespacestd;5 intmap[ -][ -];6 intused[ -];7 Charch[ -];8 intN;9 /*This DFS is based on the four-color theorem, so DFS first-tier loop I max is 4Ten This kind of writing can work in the four-color theorem of the topic, no map of the adjacent two points of different colors One A */ - voidDfsintnode) { - intflag, I, J; the if(node = =N) - return; - for(i =1; I <=4; i++){ -Flag =0; + //If the new node has the same color as the neighboring node, it needs to be colored, i.e. I plus 1 - for(j =0; j<n; J + +){ + if(Map[node][j] && used[j] = =i) { AFlag =1; at Break; - } - } - //this if means that if the new node is not the same color as the neighboring nodes, then the old color is dyed, and then the next node is searched . - if(!flag) { -Used[node] =i; inDFS (node +1); - Break; to } + } - } the * intMain () { $ intI, J;Panax Notoginseng while(SCANF ("%d", &N), N) { -memset (Map,0,sizeof(map)); thememset (Used,0,sizeof(used)); + for(i =0; I < n; i++){ Ascanf"%s", ch); the for(j =2; J < strlen (CH); J + +) + { -MAP[I][CH[J]-'A'] =1; $MAP[CH[J]-'A'][i] =1; $ } - } -Dfs0); theSort (used, used + -); - if(used[ -] ==1)Wuyiprintf"1 channel needed.\n"); the Else -printf"%d channels needed.\n", used[ -]); Wu } -}DFS
POJ 1129 Channel Allocation (four-color theorem)