• POJ 2378 Tree Cutting 3140 Contestants Division (简单树形dp)


    POJ 2378 Tree Cutting:题意 求删除哪些单点后产生的森林中的每一棵树的大小都小于等于原树大小的一半

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<iostream>
     4 #include<algorithm>
     5 #include<cmath>
     6 using namespace std;
     7 const int maxn=10010;
     8 const int maxx=1<<30;
     9 int t,n;
    10 int f[maxn]={};
    11 int vis[maxn]={};
    12 struct nod{
    13     int y;
    14     int next;
    15 }e[2*maxn]={};
    16 int head[maxn]={},tot=0;
    17 int ans[maxn]={},size=0,sum=0;
    18 void init(int x,int y){
    19     e[++tot].y=y;
    20     e[tot].next=head[x];
    21     head[x]=tot;
    22 }
    23 void dfs(int x){
    24     int y;
    25     vis[x]=1;
    26     int tmp=0;
    27     for(int i=head[x];i;i=e[i].next){
    28         y=e[i].y;
    29         if(!vis[y]){
    30             dfs(y);
    31             f[x]+=f[y]+1;
    32             tmp=max(f[y]+1,tmp);
    33         }
    34     }
    35     tmp=max(tmp,n-f[x]-1);
    36     if(tmp<size){
    37         ans[++sum]=x;
    38     }
    39 }
    40 int main(){
    41     scanf("%d",&n);
    42     size=n/2+1;
    43     int x,y;
    44     for(int i=1;i<n;i++){
    45         scanf("%d%d",&x,&y);
    46         init(x,y);
    47         init(y,x);
    48     }
    49     dfs(1);
    50     sort(ans+1,ans+1+sum);
    51     for(int i=1;i<=sum;i++){
    52         printf("%d
    ",ans[i]);
    53     }
    54     return 0;
    55 }
    View Code

    POJ 3140 Contestants Division:题意 删除某边后剩余两个联通块节点数的差最小,输出这个差,

    要用longlong,记得把上限设大...
     1 #include<cstdio>
     2 #include<cstring>
     3 #include<iostream>
     4 #include<algorithm>
     5 #include<cmath>
     6 using namespace std;
     7 const int maxn=100010;
     8 long long maxx=2000000000000000000LL;
     9 int t,n,m;
    10 long long f[maxn]={},v[maxn]={};
    11 int vis[maxn]={};
    12 struct nod{
    13     int y;
    14     int next;
    15 }e[2*maxn]={};
    16 int head[maxn]={},tot;
    17 long long ans,sum,bign;
    18 void init(int x,int y){
    19     e[++tot].y=y;
    20     e[tot].next=head[x];
    21     head[x]=tot;
    22 }
    23 inline long long mabs(long long x){
    24     if(x<0){
    25         return -x;
    26     }
    27     return x;
    28 }
    29 void dfs(int x){
    30     int y;
    31     long long tmp=0;
    32     vis[x]=1;
    33     for(int i=head[x];i;i=e[i].next){
    34         y=e[i].y;
    35         if(!vis[y]){
    36             dfs(y);
    37             f[x]+=f[y];
    38             tmp=mabs(bign-f[y]-f[y]);
    39             ans=min(ans,tmp);
    40         }
    41     }
    42     f[x]+=v[x];
    43 }
    44 void yu(){
    45     bign=sum=0;
    46     tot=0;
    47     ans=maxx;
    48     memset(f,0,sizeof(f));
    49     memset(vis,0,sizeof(vis));
    50     memset(head,0,sizeof(head));
    51     memset(v,0,sizeof(v));
    52 }
    53 int main(){
    54     for(int zz=1;;zz++){
    55         yu();
    56         scanf("%d%d",&n,&m);
    57         if(n==0&&m==0){
    58             break;
    59         }
    60         int x,y;
    61         for(int i=1;i<=n;i++){
    62             scanf("%lld",&v[i]);
    63             bign+=v[i];
    64         }
    65         for(int i=1;i<=m;i++){
    66             scanf("%d%d",&x,&y);
    67             init(x,y);
    68             init(y,x);
    69         }
    70         dfs(1);
    71         printf("Case %d: %lld
    ",zz,ans);
    72     }
    73     return 0;
    74 }
    View Code
  • 相关阅读:
    ubuntu16.04配置网卡
    如何让虚拟机的Ubuntu上网?
    sqlite错误 The database disk image is malformed database disk image is malformed 可解决
    Linux系统安装bcompare步骤及注意事项Linux系统安装bcompare步骤及注意事项
    用python做科学计算(一)C语言读取python生成的二进制文件
    ubuntu下的RapidSVN
    matplotlib常见问题总结
    MATLAB中的矩阵索引
    4.0 Lab1-CRC Generation(1)
    项目管理-人员配置
  • 原文地址:https://www.cnblogs.com/137shoebills/p/7783896.html
Copyright © 2020-2023  润新知