Ciel the Commander CodeForces - 321C (樹, 思維)
阿新 • • 發佈:2019-02-19
() 存在 force nbsp push_back pri highlight 分治 main
鏈接
大意: 給定n結點樹, 求構造一種染色方案, 使得每個點顏色在[A,Z], 且端點同色的鏈中至少存在一點顏色大於端點 (A為最大顏色)
直接點分治即可, 因為最壞可以塗$2^{26}-1$個節點, 所以方案一定存在
#include <iostream> #include <algorithm> #include <cstdio> #include <vector> #define REP(i,a,n) for(int i=a;i<=n;++i) #define pb push_back using namespace std; typedef long long ll; const int N = 4e5+10, INF = 0x3f3f3f3f; int n, sum, rt; int mx[N], sz[N], vis[N]; vector<int> g[N]; char buf[N]; void getrt(int x, int fa) { mx[x]=0, sz[x]=1; for (int y:g[x]) if (!vis[y]&&y!=fa) { getrt(y,x),sz[x]+=sz[y]; mx[x]=max(mx[x],sz[y]); } mx[x]=max(mx[x],sum-sz[x]); if (mx[x]<mx[rt]) rt=x; } void solve(int x, char c) { vis[x]=1, buf[x]=c; for (int y:g[x]) if (!vis[y]) { mx[rt=0]=n,sum=sz[y]; getrt(y,0),solve(rt,c+1); } } int main() { scanf("%d", &n); REP(i,2,n) { int u, v; scanf("%d%d", &u, &v); g[u].pb(v), g[v].pb(u); } sum=mx[0]=n,getrt(1,0),solve(rt,‘A‘); REP(i,1,n) printf("%c ", buf[i]); puts(""); }
Ciel the Commander CodeForces - 321C (樹, 思維)