簡單的樹形DPphp
$dp[i][j]$表示節點$i$染顏色$j$時的最小ios
隨便轉移一下...本身一開始認爲就是一層1一層2可是發現好像不對,不可能這麼naive,可是又實在不知道極限是多少...spa
PS求路過的人教我如何證實最大爲3...code
#include<iostream> #include<cstring> #include<cstdio> #include<cmath> #include<algorithm> using namespace std; int read() { int x=0,f=1; char ch=getchar(); while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();} while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();} return x*f; } #define maxn 10010 int N,ans; struct EdgeNode{int next,to;}edge[maxn<<1]; int head[maxn],cnt; void add(int u,int v) { cnt++; edge[cnt].next=head[u]; head[u]=cnt; edge[cnt].to=v; } void insert(int u,int v) {add(u,v); add(v,u);} int dp[maxn][5]; void DFS(int now,int fa) { for (int i=1; i<=3; i++) dp[now][i]=i; for (int i=head[now]; i; i=edge[i].next) if (edge[i].to!=fa) DFS(edge[i].to,now); for (int i=1; i<=3; i++) for (int j=head[now]; j; j=edge[j].next) if (edge[j].to!=fa) { int nowc=0; for (int k=1; k<=3; k++) if (k!=i) nowc=nowc==0?dp[edge[j].to][k]:min(nowc,dp[edge[j].to][k]); dp[now][i]+=nowc; } } int main() { N=read(); for (int u,v,i=1; i<=N-1; i++) u=read(),v=read(),insert(u,v); DFS(1,0); for (int i=1; i<=3; i++) ans=ans==0?dp[1][i]:min(ans,dp[1][i]); printf("%d\n",ans); return 0; }