确实是神2333333333,一开始以为是01染色sb题,然而被打脸。。。
(蒟蒻不乱说,网上各种神犇的题解,还有图!!)
1 #include <bits/stdc++.h> 2 #define LL long long 3 #define lowbit(x) x&(-x) 4 #define inf 0x3f3f3f3f 5 using namespace std; 6 inline int ra() 7 { 8 int x=0,f=1; char ch=getchar(); 9 while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();} 10 while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();} 11 return x*f; 12 } 13 int n,cnt; 14 int head[10005],f[10005][25]; 15 struct edge{ 16 int to,next; 17 }e[20005]; 18 void insert(int x, int y) 19 { 20 e[++cnt].next=head[x]; e[cnt].to=y; head[x]=cnt; 21 } 22 void dfs(int x, int fa) 23 { 24 for (int i=1; i<=20; i++) 25 f[x][i]=i; 26 for (int i=head[x];i;i=e[i].next) 27 if (e[i].to!=fa) dfs(e[i].to,x); 28 for (int j=1; j<=20; j++) 29 { 30 for (int i=head[x];i;i=e[i].next) 31 if (e[i].to!=fa) 32 { 33 int mn=inf; 34 for (int k=1; k<=20; k++) 35 if (j!=k) mn=min(mn,f[e[i].to][k]); 36 f[x][j]+=mn; 37 } 38 } 39 } 40 int main(int argc, char const *argv[]) 41 { 42 n=ra(); 43 for (int i=1; i<n; i++) 44 { 45 int x=ra(),y=ra(); 46 insert(x,y); insert(y,x); 47 } 48 dfs(1,0); 49 int ans=inf; 50 for (int j=1; j<=20; j++) 51 ans=min(ans,f[1][j]); 52 cout<<ans; 53 return 0; 54 }