AtCoder Beginner Contest 309 E题
AtCoder Beginner Contest 309 E题
E - Family and Insurance
Problem Statement
There is a family consisting of person 1 1 1, person 2 2 2, … … … , and person N N N. For i ≥ 2 i ≥ 2 i≥2 , person i i i’s parent is person p i p_i pi.
They bought insurance M M M times. For i i i = 1 , 2 , … , M 1,2,…,M 1,2,…,M, person x i x_i xi bought the
i-th insurance, which covers that person and their descendants in the next y i y_i yi generations.
How many people are covered by at least one insurance?
Constraints
2 ≤ N ≤ 3 × 1 0 5 2≤N≤3×10^5 2≤N≤3×105
1 ≤ M ≤ 3 × 1 0 5 1≤M≤3×10^5 1≤M≤3×105
1 ≤ p i ≤ i − 1 1≤p_i≤i−1 1≤pi≤i−1
1 ≤ x i ≤ N 1≤x_i≤N 1≤xi≤N
1 ≤ y i ≤ 3 × 1 0 5 1≤y_i≤3×10^5 1≤yi≤3×105
All input values are integers.
Input
The input is given from Standard Input in the following format:
N N N M M M
p 2 … p N p_2…p_N p2…pN
x 1 x_1 x1 y 1 y_1 y1
⋮
x M x_M xM y M y_M yM
Output
Print the answer.
Sample Input 1
7 3
1 2 1 3 3 3
1 1
1 2
4 3
Sample Output 1
4
The 1-st insurance covers people 1, 2, and 4, because person 1’s 1-st generation descendants are people
2 and 4.The 2-nd insurance covers people 1, 2, 3, and 4, because person 1’s 1-st generation descendants are people 2 and 4, and person 1’s 2-nd generation descendant is person 3.The 3-rd insurance covers person 4, because person 4 has no 1-st, 2-nd, or 3-rd descendants.
Therefore, four people, people 1, 2, 3, and 4, are covered by at least one insurance.
Sample Input 2
10 10
1 1 3 1 2 3 3 5 7
2 1
5 1
4 3
6 3
2 1
7 3
9 2
1 2
6 2
8 1
Sample Output 2
10
一个家庭,除了第一个人,每人都有一个祖先(且祖先编号必定小于自己编号)。有 M M M 份保险,不仅可以对第 x i x_i xi 个人生效,还可以向后代传 y i y_i yi 代。问最后有多少人有保险。
这是一棵树(有唯一的前驱与多个后记)。我选择用vector定义a[]二维数组,a[i]里存放第
i
i
i 个人的后记。对于每一份保险,用ans数组标记购买人,再向下搜索
y
i
y_i
yi 代。
加上剪枝,用vh一维数组记录对于标号为
i
i
i 的人,他的保险最多能向下生效的代数。如果当前保险生效代数小于记录的,则无需搜索,直接return。
交过以后,超时,53个点过了43个,除去剪枝时间复杂度为
O
(
m
∗
e
i
,
j
)
O(m*e_{i,j})
O(m∗ei,j)(
e
i
,
j
e_{i,j}
ei,j 指第
i
i
i 个人的
j
j
j 代及以内后代数量)。
#include <bits/stdc++.h>
#define int long long
using namespace std;
vector<int>a[300010];
int n,m,pi,vh[300010],x,y,ans,dp[300010];
void dfs(int x,int step){
if(dp[x]>=step)return;
dp[x]=step;
for(int i=0;i<a[x].size();++i){
vh[a[x][i]]=1;
dfs(a[x][i],step-1);
}
}
signed main(){
cin>>n>>m;
for(int i=2;i<=n;++i){
cin>>pi;
a[pi].push_back(i);
}
for(int i=1;i<=m;++i){
cin>>x>>y;
vh[x]=1;
dfs(x,y);
}
for(int i=1;i<=n;++i)
if(vh[i])++ans;
cout<<ans<<endl;
return 0;
}
超时,优化关键就在于重复的搜索。有一个条件一直没用上,那就是祖先标号小于自身。这有什么用呢?这意味着我们从前向后的枚举顺序也是祖先先枚举,后代后枚举的顺序,换句话说,祖先的保险有后效性,而枚举顺序正好从前向后。
对于第
i
i
i 个人,他保险还可以向下传递的代数是祖先保险可传递代数-1与自身保险可传递代数取最大值。
注意一个人可能购买多份保险,除去递归消耗,理想的时间复杂度
O
(
n
+
m
)
O(n+m)
O(n+m)。
#include <bits/stdc++.h>
#define int long long
using namespace std;
vector<int>a[300010];
int n,m,p,vh[300010],ans[300010],x,y,cnt;
void dfs(int step,int pos){
if(pos>0)ans[step]=1;
for(int i=0;i<a[step].size();++i)
dfs(a[step][i],max(pos-1,vh[a[step][i]]));
}//从上到下挨个遍历家族
signed main(){
scanf("%lld%lld",&n,&m);
for(int i=2;i<=n;++i){
scanf("%lld",&p);
a[p].push_back(i);
}
for(int i=1;i<=m;++i){
scanf("%lld%lld",&x,&y);
vh[x]=max(y+1,vh[x]);
}
dfs(1,vh[1]);
for(int i=1;i<=n;++i)
cnt+=ans[i];
printf("%lld\n",cnt);
return 0;
}
其实,可以用一个数组标记第 i i i 个人的前驱,再开一个数组记录第 i i i 个人的保险传递代数,就可以对于第 i i i 个人访问其前驱保险传递代数,再比较自身保险代数,一层for循环解决问题,这样做不仅效率高(没有递归消耗),而且省空间(记前驱比记后记要少一维,也没有递归消耗),最终时间复杂度 O ( n + m ) O(n+m) O(n+m)。
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n,m,p,vh[300010],ans[300010],x,y,cnt,last[300010];
signed main(){
scanf("%lld%lld",&n,&m);
for(int i=2;i<=n;++i){
scanf("%lld",&p);
last[i]=p;
}
for(int i=1;i<=m;++i){
scanf("%lld%lld",&x,&y);
vh[x]=max(y+1,vh[x]);
}
for(int i=1;i<=n;++i){
vh[i]=max(vh[last[i]]-1,vh[i]);
if(vh[i]>0)ans[i]=1;
}
for(int i=1;i<=n;++i){
cnt+=ans[i];
}
printf("%lld\n",cnt);
return 0;
}

浙公网安备 33010602011771号