bzoj1086[SCOI2005]王室联邦

来源:互联网 发布:php电商项目架构 编辑:程序博客网 时间:2024/04/28 00:57

这道题真恶心。。。
看起来是高大上的树结构,各种线段树、树链剖分甚至伸展树闪过,结果

Created with Raphaël 2.1.0yzhyzhxgcxgc怎么求最优解啊xgc愣了一下,说:不用求最优解啊,随便得一个解就好心中万般草泥马飞过。。。

(害我一个晚上想不出来)
言归正传,只要一个解就简单多了,将数存在栈里,只要达到B就当一个省,一个个分就行了,虽然清橙上全错(因为数据都是最优解),但在大视野就AC了。

#include<cstdio>#include<cstring>using namespace std;struct node{    int x,y,next;}a[2100];int len,last[1100];void ins(int x,int y){    len++;    a[len].x=x;a[len].y=y;    a[len].next=last[x];last[x]=len;}int sta[1100],top;int d[1100],g[1100],m,B;void dfs(int x,int fa){    int now=top;    for(int k=last[x];k;k=a[k].next)    {        int y=a[k].y;        if(y!=fa)        {            dfs(y,x);            if(top-now>=B)            {                g[++m]=x;                while(top!=now)d[sta[top--]]=m;            }        }    }    sta[++top]=x;}int main(){    freopen("Royal.in","r",stdin);    freopen("Royal.out","w",stdout);    int n,x,y;    scanf("%d%d",&n,&B);    if(n<B){printf("0\n");return 0;}    for(int i=1;i<n;i++)    {        scanf("%d%d",&x,&y);        ins(x,y);ins(y,x);    }    dfs(1,0);    while(top!=0)d[sta[top--]]=m;    printf("%d\n",m);    for(int i=1;i<n;i++)printf("%d ",d[i]);    printf("%d\n",d[n]);    for(int i=1;i<m;i++)printf("%d ",g[i]);    printf("%d\n",g[m]);    return 0;}

By_yzh

2 1
原创粉丝点击