• BZOJ 1086 [SCOI2005]王室联邦(树分块)


    题意:中文题,略

    思路:秦皇岛前就一直想学,后来没怎么想通,就算了。现在雅不是很懂,一直似懂非懂的,对于树分块的种类,有位聚聚已经整理好了(传送门),其他的就是直接搜索分块

    代码:

    #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
    ",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;
    }
  • 相关阅读:
    吉文斯旋转
    MinHash
    MinHash 原理
    Mahout SlopOne
    svd++
    openwrt定制管理
    苹果新的编程语言 Swift 语言进阶(九)--方法和下标
    2014年百度之星程序设计大赛
    unixbench安装及使用
    数据库连接-ADO.NET
  • 原文地址:https://www.cnblogs.com/lalalatianlalu/p/9758661.html
Copyright © 2020-2023  润新知