Codeforces 🎨 CF2183D1. Tree Coloring (Easy Version)
題目的難度顏色使用 Luogu 上的分級,由簡單到困難分別為 🔴🟠🟡🟢🔵🟣⚫。
🔗 CF2183D1 Tree Coloring (Easy Version)
rating: 1532
Problem Statement
題目簡述
給定一棵 個節點、以 為根的有根樹。每次操作可以選擇一個白色節點集合 ,將其染黑。集合 需滿足:
- 集合內任意兩點之間沒有邊相連(即 為獨立集)。
- 集合內任意兩點到根節點的距離不同。
求將整棵樹染黑所需的最少操作次數。
Constraints
約束條件
思路:分析下界
問題轉化:染色問題
題目要求最小化操作次數,我們可以將每次操作視為分配一種顏色。題意等同於:將樹上所有節點染色,使得相同顏色的節點之間「深度皆不同」且「不存在父子關係」,求最小的顏色數量 。
在轉化後的問題下,我們可以分析對顏色數量的下界限制:
1. 每層顏色互異
考慮樹的每一層(相同深度的節點集合)。
根據限制,相同顏色的節點深度必須不同,這意味著同一層的所有節點必須染上不同的顏色。
若第 層有 個節點,則顏色數量至少需要 。
2. 父子及其兄弟互斥
考慮任意一個節點 及其所有子節點 。
- 子節點間互斥:所有子節點 都在同一層(深度均為 ),根據深度限制,它們必須顏色互異。
- 父子互斥:父節點 與每個子節點 之間存在父子關係(邊),根據限制,它們不能同色。
綜合上述兩點,集合 中的任意兩個節點,要麼深度相同,要麼是父子關係,因此這個集合內的所有節點都必須染上不同的顏色。
這個互斥集合的大小為 (1 個父節點 + 個子節點)。
複雜度分析
- 時間複雜度:,需要計算出每個節點的深度和度數。
- 空間複雜度:。
Code
1 | from collections import deque, defaultdict |
寫在最後
PROMPT
masterpiece, best quality, high quality, good quality, 32K UHD, colorful, official art, illustration, in the style of fashion photography, dynamic, dynamic force picture, (Visual impact:1.2), impactful picture, extreme aesthetic, A shot with tension, sharp focus, The Ninth Art, depth of field, cinematic lighting, light particles, lens flare, movie perspective, (Tyndall Effect:1.4), light particles, light, shadow, scenery, temperate atmosphere, (artist:pigeon666:0.83), (Yomu:0.4), (remsrar:0.45), (quasarcake:0.3),
1girl, solo, Nijika Ijichi (ijichi nijika, bocchi the rock), brown eyes, blonde hair, long hair, very long hair, bangs, side ponytail, school uniform, white shirt, pleated skirt, long sleeves, bow, ahoge, sidelocks, outdoors, collared shirt, bowtie, black skirt, bag, red bow, red bowtie,
holding umbrella, transparent umbrella, rain, raining, night, wet street, puddle, reflection, glowing vending machine, backlighting, neon lights, urban scenery, wide shot, landscape, horizontal, looking at viewer, blush








