zoukankan      html  css  js  c++  java
  • bzoj 1086: [SCOI2005]王室联邦

    神题,树分块的基础的233,,,反正就是神奇的乱搜,不会

     1 #include<bits/stdc++.h>
     2 #define N 100005
     3 #define LL long long
     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 struct node{int to,next;}e[N];
    14 int n,b,top,s[N],belong[N],root[N],head[N],cnt1,cnt;
    15 void insert(int x, int y){e[++cnt1].next=head[x]; e[cnt1].to=y; head[x]=cnt1;}
    16 void dfs(int x, int fa)
    17 {
    18     /*int size=0;
    19     for (int i=head[x];i;i=e[i].next)
    20     {
    21         if (e[i].to==fa) continue;
    22         size+=dfs(e[i].to,x);
    23         if (size>=b)
    24         {
    25             ++cnt;
    26             while (size--) belong[s[top--]]=cnt;
    27             root[cnt]=x;
    28         } 
    29     }
    30     s[++top]=x;
    31     return size+1;*/
    32     int bottom=top;
    33     for (int i=head[x];i;i=e[i].next)
    34     {
    35         if (e[i].to==fa) continue;
    36         dfs(e[i].to,x);
    37         if (top-bottom>=b)
    38         {
    39             root[++cnt]=x;
    40             while (top!=bottom) belong[s[top--]]=cnt;
    41         }
    42     }
    43     s[++top]=x;
    44 }
    45 int main()
    46 {
    47     n=ra(); b=ra(); 
    48     if (n<b)
    49     {
    50         printf("0");
    51         return 0;
    52     }
    53     for (int i=1; i<n; i++)
    54     {
    55         int x=ra(),y=ra();
    56         insert(x,y); insert(y,x);
    57     }
    58     dfs(1,0);
    59     while (top) belong[s[top--]]=cnt;
    60     cout<<cnt<<endl;
    61     for (int i=1; i<=n; i++)
    62         printf("%d ",belong[i]); cout<<endl;
    63     for (int i=1; i<=cnt; i++)
    64         printf("%d ",root[i]);
    65     return 0;
    66 }
  • 相关阅读:
    js人工智能对话框
    html 实现相册
    thinkphp5 三种重定向(跳转)
    thinkphp5 分页实现
    常用的Mysql数据库操作语句大全
    FormData之file图片上传
    FormData对象
    input file 上传图片时限制格式
    form 中Enctype=multipart/form-data 的作用
    thinkphp5 不刷新退出
  • 原文地址:https://www.cnblogs.com/ccd2333/p/6435558.html
Copyright © 2011-2022 走看看