题意:给你一棵树,让你求一点,使该点到其余各点的距离之和最小。如果这样的点有多个,则按升序依次输出。

树型dp

#include <cstdio>
#include <cstring>
#include <vector>
#include <set>
using namespace std;
const int maxn=50010;
typedef __int64 LL;
vector<int>tree[maxn];// to save the relation
LL f[maxn],g[maxn],dp[maxn];//f[u] u as root to all his sons' distance g[u]the number of u'sons
set<int> myqueue;
void dfs(int u,int pa){
/* 以1为根,所有的子树到他们的子节点的和 */
    if(tree[u].size() == 1 && u != 1){
        g[u]=1;
        f[u]=0;
        return;
    }
    for(int v=0;v < tree[u].size();v++){
        if(tree[u][v]!=pa){
            dfs(tree[u][v],u);
            g[u]+=g[tree[u][v]];//the son's sons' number
            f[u]+=f[tree[u][v]]+g[tree[u][v]];
        }
    }
    g[u]++;// himself
}
void dfs2(int u,int pa){// to sum the way from his father
//再考虑从父节点来的
    if(tree[u].size()==1 && u!=1){
        dp[u]=dp[pa]+g[1]-(g[u]<<1);
        return;
    }
    for(int v=0;v<tree[u].size();v++){
        if(tree[u][v]!= pa){
//到某节点的距离的和=该节点子树的距离和(在dfs1中获得)+从父亲那一支子树获得的和(此时,父节点那一支看成子树)。dp[儿子]=dp[父节点]-dp[儿子]-g[儿子](儿子到父节点这条路被减了子树的子节点数的次数)    + dp[儿子]+(g[1]-g[儿子])(父树上的所有节点)
            dp[tree[u][v]]=dp[u]+g[1]-(g[tree[u][v]]<<1);
            dfs2(tree[u][v],u);//先算了之后再跑子树
        }
    }
}
int main(){
    int t;
    int n,I,R,a,b;
    scanf("%d",&t);
    LL mmin;
    while(t--){
        scanf("%d%d%d",&n,&I,&R);

        for(int i=0;i<=n;i++){
            tree[i].clear();
        }
        for(int i=2;i<=n;i++){
            scanf("%d%d",&a,&b);
            tree[a].push_back(b);
            tree[b].push_back(a);
        }
//        for(int i=0;i<=n;i++){
//            for(int j=0;j<tree[i].size();j++){
//                printf("%d ",tree[i][j]);
//            }
//            printf("\n");
//        }
        memset(f,0,sizeof(f));
        memset(g,0,sizeof(g));
        dfs(1,-1);
        dp[1]=f[1];
        dfs2(1,-1);
        mmin=dp[1];
        myqueue.clear();
        myqueue.insert(1);
        for(int i=2;i<=n;i++){
            if(dp[i]<mmin){
                mmin=dp[i];
                myqueue.clear();
                myqueue.insert(i);
            }
            if(dp[i]==mmin) myqueue.insert(i);
        }
        //warning : output long long should be I64d
        printf("%I64d\n",I*I*R*mmin);//use set not num but the op
            for(set<int>::iterator it=myqueue.begin();it!=myqueue.end();++it){
                printf("%d ",*it);
            }
        printf("\n\n");
    }
}

  

Power Station POJ 4045的更多相关文章

  1. Power Network (poj 1459 网络流)

    Language: Default Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 23407   ...

  2. Power Network - poj 1459 (最大流 Edmonds-Karp算法)

      Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 24788   Accepted: 12922 Description A ...

  3. Building a Space Station POJ - 2031

    Building a Space Station POJ - 2031 You are a member of the space station engineering team, and are ...

  4. Power Strings POJ - 2406

    Power Strings POJ - 2406 时限: 3000MS   内存: 65536KB   64位IO格式: %I64d & %I64u 提交 状态 已开启划词翻译 问题描述 Gi ...

  5. poj - 4045 - Power Station

    题意:一棵有n个结点的树,要取其中的一个结点,使得该结点到其他所有结点的距离和dis最小,即损耗I * I * R * dis最小,输出最小损耗和该结点(有多个的话按结点编号从小到大输出)(3 < ...

  6. ( KMP 求循环节的个数)Power Strings -- poj -- 2406

    链接: http://poj.org/problem?id=2406 Power Strings Time Limit:3000MS     Memory Limit:65536KB     64bi ...

  7. (最小生成树) Building a Space Station -- POJ -- 2031

    链接: http://poj.org/problem?id=2031 Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6011 ...

  8. Building a Space Station POJ 2031 【最小生成树 prim】

    http://poj.org/problem?id=2031 Description You are a member of the space station engineering team, a ...

  9. Power Strings (poj 2406 KMP)

    Language: Default Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 33205   ...

随机推荐

  1. 关于IE8以上 不引人css 症状

    不知道各位有没有体验过 这样的状况  在同一个文件夹中 <!DOCTYPE html> <html> <head> <meta charset="u ...

  2. 【原】GO 语言常见错误

    1. Scan error on column index 4: converting string "" to a int: strconv.ParseInt: parsing ...

  3. 多项分布(multinominal distribution)

    简介 更一般性的问题会问:“点数1~6的出现次数分别为(x1,x2,x3,x4,x5,x6)时的概率是多少?其中sum(x1~x6)= n”.这就是一个多项式分布. 定义 把二项分布推广至多个(大于2 ...

  4. 利用LRUMap 设计缓存

    上下文缓存,即将一些常用的数据至于一个缓存集合中,当需要获取这些数据的时候,直接从缓存中读取,而不必每次都从数据库读取,以此提高效率. 其原理类似Apache Commons Collections  ...

  5. 如何查看Oracle的用户权限

    ORACLE数据字典视图的种类分别为:USER,ALL 和 DBA. USER_*:有关用户所拥有的对象信息,即用户自己创建的对象信息 ALL_*:有关用户可以访问的对象的信息,即用户自己创建的对象的 ...

  6. Sublime字体设置

    {"font_face": "Courier New","font_options":["subpixel_antialias&q ...

  7. php判断服务器是否支持Gzip压缩功能

    Gzip可以压缩网页大小从而达到加速打开网页的速度,目前主流的浏览器几乎都支持这个功能,但开启Gzip是需要服务器支持的,在这里我们简单的使用php来判断服务器是否支持Gzip功能. 新建一个php类 ...

  8. centos 安装ecshop出现date错误

    centos 安装ecshop 错误提示 Warning: date(): It is not safe to rely on the system's timezone settings. You ...

  9. javascript获取ckeditor编辑器的值(实现代码)

    CKeditor编辑器是FCKeditor的升级版本想对于FCK来说,确实比较好用,加载速度也比较快以下是如果通过JS获取CKeditor编辑器的值,用于表单验证 if(CKEDITOR.instan ...

  10. Delphi Variant oleVariant

    The OleVariant type exists on both the Windows and Linux platforms. The main difference between Vari ...