题意:中文题,略
思路:秦皇岛前就一直想学,后来没怎么想通,就算了。现在雅不是很懂,一直似懂非懂的,对于树分块的种类,有位聚聚已经整理好了(传送门),其他的就是直接搜索分块
代码:
#include <bits/stdc++.h> using namespace std;const int maxn=1e4+7; struct Edge {int v,nxt; }e[maxn<<1]; int top,stk[maxn]; int cnt,root[maxn]; int tot; int belong[maxn],head[maxn]; int n,block; void addedge(int x,int y) {e[++tot].v=y;e[tot].nxt=head[x];head[x]=tot; } void dfs(int x,int pre) {int bottom=top;for(int i=head[x];i;i=e[i].nxt){if(e[i].v==pre)continue;dfs(e[i].v,x);if(top-bottom>=block){root[++cnt]=x;while(top!=bottom)belong[stk[top--]]=cnt;}}stk[++top]=x; } int main() {scanf("%d%d",&n,&block);for(int i=1,x,y;i<n;i++){scanf("%d%d",&x,&y);addedge(x,y);addedge(y,x);}dfs(1,0);while(top)belong[stk[top--]]=cnt;printf("%d\n",cnt);for(int i=1;i<=n;i++)printf("%d ",belong[i]);puts("");for(int i=1;i<=cnt;i++)printf("%d ",root[i]);puts("");return 0; }