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
题目传送门
题目大意
给你两堆石子,每次可以单独拿一堆中的任意值,或者两堆都拿,但是两堆拿的值需满足 ∣ x − y ∣ ≤ k |x−y|≤k ∣x−y∣≤k
思路
显然的威佐夫博弈的扩展
威佐夫博弈中如果想拿两堆的话,拿的石子必须相同,即
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+d∗k,所以根据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}]) ([22−d+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;
}