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

树型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. java如何调用webservice接口

    java调用WebService可以直接使用Apache提供的axis.jar自己编写代码,或者利用Eclipse自动生成WebService Client代码,利用其中的Proxy类进行调用.理论上 ...

  2. 隐藏index.php - ThinkPHP完全开发手册 - 3.1

      为了更好的实现SEO优化,我们需要隐藏URL地址中的index.php,由于不同的服务器环境配置方法区别较大,apache环境下面的配置我们可以参考5.9 URL重写来实现,就不再多说了,这里大概 ...

  3. 微信公众号-开发者-自定义菜单-CLICK事件处理

    想点击菜单,推送消息.功能很简单,坑了我一天时间.在此笔记. 菜单代码: { "button":[ { "type":"click", &q ...

  4. scrapy-redis使用详解

    描述: 1.使用两台机器,一台是win10,一台是centos7,分别在两台机器上部署scrapy来进行分布式抓取一个网站 2.centos7的ip地址为192.168.1.112,用来作为redis ...

  5. Pox启动及事件产生、监听分析

        ./pox/pox.py , Pox       实例化core=pox.core.initialize(),即为实例化POXCore类(该类是所有组件的交接点,提供组件注册功能),监听cor ...

  6. 特定用户QQ群聊天记录导出的实现

    一.把QQ群的聊天记录txt格式导出 消息管理器 -> 选择要导出的群 -> 右击.导出   这里要注意 : 导出之后的 文本是 unicode 编码的,需要转换 ==|| 之前不知道,搞 ...

  7. Android中获取应用程序(包)的大小-----PackageManager的使用(二)

    通过第一部分<<Android中获取应用程序(包)的信息-----PackageManager的使用(一)>>的介绍,对PackageManager以及 AndroidMani ...

  8. 六、mysql字段类型选择

    .char类型字段会自动删除字符串后面所带的空格,而varchar不会 .char比varchar快..... .myisam 建议 char类型,memory char跟varchar一样,都作为c ...

  9. 【多路复用】I/O多路复用

    http://www.tuicool.com/articles/RBvqUz C#下用select方法实现socket服务端

  10. iOS:横向使用iPhone默认的翻页效果

    大致思路使用两层辅助UIView的旋转来实现添加后的View的横向翻页效果 CATransform3D transformA = CATransform3DRotate(CATransform3DId ...