具有最少颜色的颜色树,使得入射到顶点的边的颜色不同
给定一棵有N个节点的树。任务是用最小数量的颜色( K )为树着色,使得入射到顶点的边的颜色不同。在第一行打印K ,然后在下一行打印N – 1 个空格分隔的整数,表示边缘的颜色。
例子:
Input: N = 3, edges[][] = {{0, 1}, {1, 2}}
0
/
1
/
2
Output:
2
1 2
0
/ (1)
1
/ (2)
2
Input: N = 8, edges[][] = {{0, 1}, {1, 2},
{1, 3}, {1, 4},
{3, 6}, {4, 5},
{5, 7}}
0
/
1
/ \ \
2 3 4
/ \
6 5
\
7
Output:
4
1 2 3 4 1 1 2
方法:首先,让我们考虑最小颜色数K 。对于每个顶点v , deg(v) ≤ K应该满足(其中deg(v)表示顶点v的度数)。事实上,存在一个所有K种不同颜色的顶点。首先,选择一个顶点并将其作为根,因此T将是一棵有根树。从根执行广度优先搜索。对于每个顶点,逐一确定其子节点之间的边的颜色。对于每条边,使用那些不使用的颜色中索引最小的颜色作为端点之一是当前顶点的边的颜色。那么每个颜色索引不超过K 。
下面是上述方法的实现:
CPP
// C++ implementation of the approach
#include
using namespace std;
// Add an edge between the vertexes
void add_edge(vector >& gr, int x,
int y, vector >& edges)
{
gr[x].push_back(y);
gr[y].push_back(x);
edges.push_back({ x, y });
}
// Function to color the tree with minimum
// number of colors such that the colors of
// the edges incident to a vertex are different
int color_tree(int n, vector >& gr,
vector >& edges)
{
// To store the minimum colors
int K = 0;
// To store color of the edges
map, int> color;
// Color of edge between its parent
vector cs(n, 0);
// To check if the vertex is
// visited or not
vector used(n, 0);
queue que;
used[0] = 1;
que.emplace(0);
while (!que.empty()) {
// Take first element of the queue
int v = que.front();
que.pop();
// Take the possible value of K
if (K < (int)gr[v].size())
K = gr[v].size();
// Current color
int cur = 1;
for (int u : gr[v]) {
// If vertex is already visited
if (used[u])
continue;
// If the color is similar
// to it's parent
if (cur == cs[v])
cur++;
// Assign the color
cs[u] = color[make_pair(u, v)]
= color[make_pair(v, u)] = cur++;
// Mark it visited
used[u] = 1;
// Push into the queue
que.emplace(u);
}
}
// Print the minimum required colors
cout << K << endl;
// Print the edge colors
for (auto p : edges)
cout << color[p] << " ";
}
// Driver code
int main()
{
int n = 8;
vector > gr(n);
vector > edges;
// Add edges
add_edge(gr, 0, 1, edges);
add_edge(gr, 1, 2, edges);
add_edge(gr, 1, 3, edges);
add_edge(gr, 1, 4, edges);
add_edge(gr, 3, 6, edges);
add_edge(gr, 4, 5, edges);
add_edge(gr, 5, 7, edges);
// Function call
color_tree(n, gr, edges);
return 0;
}
Python
# Python3 implementation of the approach
from collections import deque as queue
gr = [[] for i in range(100)]
edges = []
# Add an edge between the vertexes
def add_edge(x, y):
gr[x].append(y)
gr[y].append(x)
edges.append([x, y])
# Function to color the tree with minimum
# number of colors such that the colors of
# the edges incident to a vertex are different
def color_tree(n):
# To store the minimum colors
K = 0
# To store color of the edges
color = dict()
# Color of edge between its parent
cs = [0] * (n)
# To check if the vertex is
# visited or not
used = [0] * (n)
que = queue()
used[0] = 1
que.append(0)
while (len(que) > 0):
# Take first element of the queue
v = que.popleft()
# Take the possible value of K
if (K < len(gr[v])):
K = len(gr[v])
# Current color
cur = 1
for u in gr[v]:
# If vertex is already visited
if (used[u]):
continue
# If the color is similar
# to it's parent
if (cur == cs[v]):
cur += 1
# Assign the color
cs[u] = cur
color[(u, v)] = color[(v, u)] = cur
cur += 1
# Mark it visited
used[u] = 1
# Push into the queue
que.append(u)
# Print minimum required colors
print(K)
# Print edge colors
for p in edges:
i = (p[0], p[1])
print(color[i], end = " ")
# Driver code
n = 8
# Add edges
add_edge(0, 1)
add_edge(1, 2)
add_edge(1, 3)
add_edge(1, 4)
add_edge(3, 6)
add_edge(4, 5)
add_edge(5, 7)
# Function call
color_tree(n)
# This code is contributed by mohit kumar 29
输出:
4
1 2 3 4 1 1 2