2020杭电多校第九场题解A、C

Tree

题目传送门

Tree

题目大意

给你一个具有n个结点的树,以1为根结点,输入n-1个数分别为父节点
让你创建一条边,使得 ( x , y ) (x,y) (x,y)对(指x能到y)尽可能的多

思路

显然添加的这条边必然是添加在叶结点到根结点的,这样可以使得根结点到该叶结点的所有结点的对数全变为n
所有,首先dfs求出所有的点的子结点的个数,然后dfs找取叶结点连接根结点取最大值即可

AC Code

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
#define endl '\n'
#define INF 0x3f3f3f3f
#define int long long
// #define TDS_ACM_LOCAL
const int N=5e5 +9;
int n, x, ans[N];
int sum, res, cnt;
vector<int> t[N];

int dfs1(int u){
    int tep=1;
    for(auto v:t[u]){
        tep+=dfs1(v);
    }
    ans[u]=tep;     //分别记录每个结点的对数
    sum+=tep;
    return tep;
}

void dfs2(int tep, int u){      //tep为经过的结点的个数,u为当前结点
    res+=ans[u];
    if(t[u].empty()){           //为叶结点时
        int k=n*tep+sum-res;    //n*tep为该路径的对数,sum-res为除该路径的对数
        cnt=max(cnt, k);
        res-=1;                 //叶结点的回溯
        return ;
    }
    for(auto v:t[u])    dfs2(tep+1, v);
    res-=ans[u];
    return ;
}

void solve(){
    cin>>n;
    for(int i=0; i<=n; i++) t[i].clear();
    for(int i=2; i<=n; i++){
        cin>>x;
        t[x].emplace_back(i);
    }
    sum=0;      //(x.y)的对的总个数
    dfs1(1);
    res=cnt=0;  //res为dfs时当前到达的点的(x,y)的总和,cnt为最终答案
    dfs2(1,1);
    cout<<cnt<<endl;
    return ;
}

signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
#ifdef TDS_ACM_LOCAL
    freopen("D:\\VS code\\.vscode\\testall\\in.txt", "r", stdin);
    freopen("D:\\VS code\\.vscode\\testall\\out.txt", "w", stdout);
#endif
    int T;
    cin>>T;
    while(T--)  solve();
    return 0;
}

Slime and Stones

题目传送门

Slime and Stones

题目大意

给你两堆石子,每次可以单独拿一堆中的任意值,或者两堆都拿,但是两堆拿的值需满足 ∣ x − y ∣ ≤ k |x−y|≤k xyk

思路

显然的威佐夫博弈的扩展
威佐夫博弈中如果想拿两堆的话,拿的石子必须相同,即 x = y x=y x=y
也就是从原来的 y = x + k y=x+k y=x+k变成了 y = x + d ∗ k y=x+d*k y=x+dk,所以根据Betty定理也可以得到第k个奇异局势为

( [ 2 − d + d 2 + 4 2 ] , [ 2 + d + d 2 + 4 2 ] ) ([\frac{2-d+\sqrt{d^2+4}}{2}],[\frac{2+d+\sqrt{d^2+4}}{2}]) ([22d+d2+4 ],[22+d+d2+4 ])

题目中的k+1(起始k=0时,d为1)即为d

AC Code

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
#define endl '\n'
#define INF 0x3f3f3f3f
#define int long long
// #define TDS_ACM_LOCAL
const int N=2e5 +9;
int a, b;
double k;
void solve(){
    cin>>a>>b>>k;
    k++;
    if(a<b) swap(a,b);
    int c=(a-b)/k;
    int ans1=c*((sqrt(4.0+k*k)+k+2.0)/2.0);
    int ans2=c*((sqrt(4.0+k*k)-k+2.0)/2.0);
    if(ans1==a&&ans2==b)    cout<<"0"<<endl;
    else                    cout<<"1"<<endl;
    return ;
}

signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
#ifdef TDS_ACM_LOCAL
    freopen("D:\\VS code\\.vscode\\testall\\in.txt", "r", stdin);
    freopen("D:\\VS code\\.vscode\\testall\\out.txt", "w", stdout);
#endif
    int T;
    cin>>T;
    while(T--)  solve();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值