解題:CQOI 2017 小Q的棋盤
阿新 • • 發佈:2018-10-11
int problem min org splay const algo ans 最長
題面
由樹的結構我們可以知道,最終要麽是連一條(最長的)鏈都沒走完,要麽是走了一些點最後走了最長的鏈。為什麽總是說最長的鏈呢,因為在樹上這樣走的過程中(最後不要求返回的話)除了一條鏈都會被走兩次,顯然我們貪心地把最長鏈走一次即可。
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int N=105; 6 int n,m,t1,t2,cnt,dps,ans; 7 int p[N],noww[2*N],goal[2View Code*N]; 8 void link(int f,int t) 9 { 10 noww[++cnt]=p[f]; 11 goal[cnt]=t,p[f]=cnt; 12 } 13 void DFS(int nde,int fth,int dth) 14 { 15 dps=max(dps,dth); 16 for(int i=p[nde];i;i=noww[i]) 17 if(goal[i]!=fth) DFS(goal[i],nde,dth+1); 18 } 19 int main () 20 { 21 scanf("%d%d",&n,&m);22 for(int i=1;i<n;i++) 23 { 24 scanf("%d%d",&t1,&t2),t1++,t2++; 25 link(t1,t2),link(t2,t1); 26 } 27 DFS(1,0,1),ans=min(dps,m+1),n-=dps,m-=dps-1; 28 printf("%d",ans+min(n,(m+1)/2)); 29 return 0; 30 }
解題:CQOI 2017 小Q的棋盤