【2000*】【Codeforces Round #518 (Div. 1) [Thanks, Mail.Ru!] B】Multihedgehog
阿新 • • 發佈:2018-11-10
【連結】 我是連結,點我呀:)
【題意】
【題解】
找到度數為1的點。
他們顯然是葉子節點。
然後每個葉子節點。
往上進行bfs.
累計他們的父親節點的兒子的個數。
如果都滿足要求那麼就繼續往上走。
直到不能走。或已經走了k步。
且要求走了k步之後。他們都到了同一個節點。(根節點
這道題。
n=1的時候,認為是無解的。
(因為題目中說"some vertices of degree 1",也就是說必須>= 1.......
【程式碼】
/* find each vertex i where du[i]==1 bfs(i) one depth and get y,cnt[y]++; after that count the number if different cnt[y] ->CNT (only consider cnt[y]>0 if (CNT!=1) return 0; set the only cnt value as kk we can now know that the root has kk children find the root,(du[root]==kk) (if there are more than one i such that du[i]==kk,no solution (if there are no such i,such that du[i]==k,no solution dfs from root. if (dep!=k && du[x]==1) return 0;//must have k depth check if every vertex except the bottom vertext all has kk children */ #include <bits/stdc++.h> #define ll long long #define lson l,mid,rt<<1 #define rson mid+1,r,rt<<1|1 #define rep1(i,a,b) for (int i = a;i <= b;i++) #define rep2(i,a,b) for (int i = a;i >= b;i--) using namespace std; const int N = 1e5; int n,k; vector<int> g[N+10]; bool flag[N+10]; vector<int> v,tempv; int cnt[N+10]; void wujie(){ puts("No"); exit(0); } int main(){ #ifdef ccy freopen("rush.txt","r",stdin); #endif scanf("%d%d",&n,&k); if (n==1){ puts("No"); return 0; } rep1(i,1,n-1){ int x,y; scanf("%d%d",&x,&y); g[x].push_back(y); g[y].push_back(x); } rep1(i,1,n) if ((int)g[i].size()==1){ v.push_back(i); } rep1(dep,1,k){ /* for (int x:v){ printf("%d ",x); } puts("");*/ rep1(j,0,(int)v.size()-1){ int x = v[j]; int cntup = 0; rep1(l,0,(int)g[x].size()-1){ int y = g[x][l]; // printf("%d ",y); if (flag[y]) continue; cnt[y]++; cntup++; } if (cntup!=1) wujie(); flag[x] = true; } // puts(""); v.clear(); rep1(i,1,n){ if (cnt[i]!=0 && cnt[i]<3) wujie(); if (cnt[i]==0) continue; v.push_back(i); cnt[i] = 0; } } rep1(i,0,(int)v.size()-1){ if (v[i]!=v[0]) wujie(); } puts("Yes"); return 0; }