Purple Book Exercises 8-9 UVa 1613 (DFS Coloring + Properties of Graphs)

At the beginning of this question, I didn't think about anything to start dyeing directly, but the for loop dyes one node by one node, and then WA

After reading https://www.cnblogs.com/jerryRey/p/4702323.html

I found that I still need to prove that the dyeing can be done, and the dyeing method is dfs

(1) Proof

First of all, if the maximum degree sum is even, then according to the meaning of the title, k is sum+1.

At this time, in the worst case, the color of the maximum degree point is different from that of the surrounding points, which requires sum+1 colors, which is exactly

k, so this case must be dyed with k colors

Secondly, if the maximum degree sum is an odd number, k=sum at this time, and the worst case is that the color of the maximum degree point and the surrounding points are different.

But this situation can be proved not to exist, because n is odd

Assuming this situation exists, then a neighboring point v of the maximum degree point u, then v must have degree[u] (the degree of u)

points of different colors are around v. Why, if not, then there are multiple possibilities for the color of v

(not limited by the surrounding points), then it does not meet the above-mentioned maximum degree point and the color of the surrounding points is not the same,

Because v can be other colors. So v must have degree[u] (degree of u) points with different colors around v.

Then it can be deduced that this is the case for all points around u, and the same is true for the surrounding points of points around u.

So this graph is a complete graph, which means that a point can be directly connected to all other nodes.

Then, as mentioned earlier, the maximum degree sum is an odd number, then the number of points in the graph is sum+1 (itself), 

That is, the number of points is an even number. It is contradictory to the question that n is an odd number.

Therefore, in the case where the maximum degree sum is an odd number, there must be no difference in the color of the maximum degree point and the surrounding points.

In the middle case (the number of colors required at this time is sum+1), that is to say, the required color must be smaller than the required color in this case (the required color < sum + 1) 

That is, color <= sum = k, so k colors must be dyed.


(2) Dyeing method

It seems that the way of dyeing the surrounding points like dfs is better. My previous method of using a for loop to iterate through the points to color them would "block" the following points.


(3) In addition, I learned that a | 1 is an odd number unchanged, an even number is added by 1, and the usage of max_element, plus * indicates the value of the pointer


#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
#define REP(i, a, b) for(int i = (a); i <= (b); i++)
using namespace std;

const int MAXN = 11234;
vector<int> g[MAXN];
int degree[MAXN], color[MAXN], vis[MAXN], n, m, k;

void dfs(int u) //dfs coloring
{
	REP(i, 0, g[u].size() - 1)
		vis[color[g[u][i]]] = u;
	
	REP(i, 1, k)
		if(vis[i] != u)
		{
			color[u] = i;
			break;
		}
	
	REP(i, 0, g[u].size() - 1)
		if(!color[g[u][i]])
			dfs(g[u][i]);
}

intmain()
{
	while(~scanf("%d%d", &n, &m))
	{
		REP(i, 1, n) g[i].clear();
		memset(degree, 0, sizeof(degree));
		memset(color, 0, sizeof(color));
		memset(vis, 0, sizeof(vis));
		
		while(m--)
		{
			int x, y;
			scanf("%d%d", &x, &y);
			g[x].push_back(y);
			g[y].push_back(x);
			degree[x]++; degree[y]++;
		}
		
		k = (*max_element(degree+1, degree+n+1)) | 1; //even+1, odd unchanged.
		dfs(1); // max_element finds the maximum value and returns a pointer
		
		printf("%d\n", k);
		REP(i, 1, n) printf("%d\n", color[i]);
		puts("");
	}
	
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325949818&siteId=291194637