10.22-10.23考试爆炸记题目选

10.22 zrq大佬场
(3) 树
给以1为根的树,规定叶节点权值为该节点到根节点路径上最小点的编号。
求重新标号后叶节点乘积最大值

对于一个节点,当且仅当其子树的编号均已确定时,这个节点的标号是确定的

把树缩一缩然后暴力 220size 即可

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<ctime>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<iomanip>
#define mod 1000000007
using namespace std;
inline long long read(){
    long long i=0,f=1;
    char ch;
    for(ch=getchar();!isdigit(ch);ch=getchar())
        if(ch=='-') f=-1;
    for(;isdigit(ch);ch=getchar())
        i=(i<<3)+(i<<1)+(ch^48);
    return i*f;
}
int buf[1024];
inline void write(long long x){
    if(!x){putchar('0');return ;}
    if(x<0){putchar('-');x=-x;}
    while(x) buf[++buf[0]]=x%10,x/=10;
    while(buf[0]) putchar(buf[buf[0]--]+48);
    return ;
}
#define stan 111111
#define sten 222222
#define stin 111
#define ston 222
#define stun 1<<20
int tot,nxt[sten],first[stan],goal[sten];
int tota,nx[ston],fir[stin],disti[ston];
int sze[stan],dep[stan],num[stan],leaf[stin],cnt;
int n,a,b,maxn;
long long sum,ans,f[ston];
double tmp,per[ston];
bool tag[stan];
void addedge(int a,int b){
    nxt[++tot]=first[a];first[a]=tot;goal[tot]=b;
    nxt[++tot]=first[b];first[b]=tot;goal[tot]=a;
    return ;
}
void newedge(int a,int b){
    nx[++tota]=fir[a];fir[a]=tota;disti[tota]=b;
    return ;
}
void dfs(int x,int fa){
    int tmp_cnt=0;
    sze[x]=1;
    for(int p=first[x];p;p=nxt[p])
        if(goal[p]!=fa){
            dep[goal[p]]=dep[x]+1;
            dfs(goal[p],x);
            sze[x]+=sze[goal[p]];
            ++tmp_cnt;
        }
    if(tmp_cnt!=1) 
        tag[x]=true;
    if(tmp_cnt==0) 
        leaf[cnt++]=x;
    return ;
}
void dismember(int x,int fa,int ori){
    if(tag[x]&&ori) newedge(ori,x);
    if(tag[x]) ori=x;
    for(int p=first[x];p;p=nxt[p])
        if(goal[p]!=fa)
            dismember(goal[p],x,ori);
    return ;
}
void dfs2(int x){
    if(!fir[x]){
        sum+=num[x];
        return ;
    }
    num[x]=0;
    for(int p=fir[x];p;p=nx[p]){
        dfs2(disti[p]);
        if(num[disti[p]]==sze[disti[p]]){
            num[x]+=sze[disti[p]]+dep[disti[p]]-dep[x]-1;
            sum+=dep[disti[p]]-dep[x]-1;
        }
    }
    if(num[x]==sze[x]-1){
        ++num[x];
        ++sum;
    }
    return ;
}
signed main(){
    n=read();
    for(int i=1;i<n;++i){
        a=read();b=read();
        addedge(a,b);
    }
    dep[1]=1;
    dfs(1,0);
    tag[1]=true;
    dismember(1,0,0);
    maxn=1<<cnt;
    for(int i=0;i<cnt;++i){
        f[1<<i]=1;
        per[1<<i]=1.0;
    }
    for(int i=1;i<maxn;++i){
        for(int j=0;j<cnt;++j)
            if(i&(1<<j)) num[leaf[j]]=1;
            else num[leaf[j]]=0;
        sum=0;dfs2(1);
        tmp=per[i]*(sum+1);
        ans=f[i]*(sum+1)%mod;
        for(int j=0;j<cnt;++j)
            if(!(i&(1<<j))&&per[i|(1<<j)]<tmp){
                per[i|(1<<j)]=tmp;
                f[i|(1<<j)]=ans;
            }
    }
    write(f[maxn-1]);
    return 0;
}

10.23 金牌爷wkl第8还是第9场
(3)拆网线
在一棵有 n <script type="math/tex" id="MathJax-Element-115">n</script>个点的树上,求一种覆盖k个点的最少选边数

叶节点拓扑

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<ctime>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<iomanip>
using namespace std;
inline int read(){
    int i=0,f=1;
    char ch;
    for(ch=getchar();!isdigit(ch);ch=getchar())
        if(ch=='-') f=-1;
    for(;isdigit(ch);ch=getchar())
        i=(i<<3)+(i<<1)+(ch^48);
    return i*f;
}
int buf[1024];
inline void write(int x){
    if(!x){putchar('0');return ;}
    if(x<0){putchar('-');x=-x;}
    while(x) buf[++buf[0]]=x%10,x/=10;
    while(buf[0]) putchar(buf[buf[0]--]+48);
    return ;
}
#define stan 111111
#define sten 222222
int tot,nxt[sten],first[stan],goal[sten];
int d[stan],n,k,a,b,cnt2,T;
bool vis[stan],tag[stan];
void addedge(int a,int b){
    nxt[++tot]=first[a];first[a]=tot;goal[tot]=b;
    nxt[++tot]=first[b];first[b]=tot;goal[tot]=a;
    ++d[a];++d[b];
    return ;
}
void dfs(int u,int fa){
    if(d[u]==1&&u!=1){
        vis[u]=true;
        return ;
    }
    for(int p=first[u];p;p=nxt[p])
        if(goal[p]!=fa){
            dfs(goal[p],u);
            if(vis[goal[p]]&&(!vis[u])&&(!tag[goal[p]])){
                vis[u]=true;
                tag[u]=true;
                ++cnt2;
            }
        }
    if(!vis[u]) vis[u]=true;
    return ;
}
signed main(){
    T=read();
    while(T--){
        tot=0;cnt2=0;
        memset(first,0,sizeof(first));
        memset(d,0,sizeof(d));
        memset(tag,false,sizeof(tag));
        memset(vis,false,sizeof(vis));
        n=read();k=read();
        for(int i=2;i<=n;++i){
            a=read();
            addedge(i,a);
        }
        dfs(1,0);
        if(k<=2*cnt2){
            write(k/2+(k%2));puts("");
            continue;
        }else{
            write(min(k-2*cnt2+cnt2,n-1));puts("");
            continue;
        }
    }
    return 0;
}

喧豗接天声声爆,炸燹连垄处处哀。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值