Consider a rooted tree. A rooted tree has one special vertex called the root. All edges are directed from the root. Vertex u is called a child of vertex v and vertex v is called a parent of vertex u if there exists a directed edge from v to u. A vertex is called a leaf if it doesn't have children and has a parent.
Let's call a rooted tree a spruce if its every non-leaf vertex has at least 3 leaf children. You are given a rooted tree, check whether it's a spruce.
The definition of a rooted tree can be found here.
The first line contains one integer n — the number of vertices in the tree (3 ≤ n ≤ 1 000). Each of the next n - 1 lines contains one integer pi (1 ≤ i ≤ n - 1) — the index of the parent of the i + 1-th vertex (1 ≤ pi ≤ i).
Vertex 1 is the root. It's guaranteed that the root has at least 2 children.
Print "Yes" if the tree is a spruce and "No" otherwise.
4 1 1 1
Yes
7 1 1 1 2 2 2
No
8 1 1 1 1 3 3 3
Yes
The first example:
The second example:
It is not a spruce, because the non-leaf vertex 1 has only 2 leaf children.
The third example:
代码:
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cstdio>
#include<cmath>
#include<string>
#define ll long long
using namespace std;
bool vis[1005];
vector<int>v[1005];
int main(){
int n;
int i;
int x;
cin>>n;
memset(vis,0,sizeof(vis));
bool flag=true;
for(i=2;i<=n;i++){
cin>>x;
v[x].push_back(i);
vis[x]=1;
}
for(i=1;i<1005;i++){
if(vis[i]==1){
int len=v[i].size();
for(int k=0;k<v[i].size();k++){
if(vis[v[i][k]]==1) len--;
}
if(len<3) {
flag=false;
break;
}
}
}
if(flag) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
return 0;
}
题意:
有n家店铺,每个店铺有一种商品(无限个),每种商品有体积(2的i-1次方),每种商品有价格c[i],问至少买l体积的物品最少需要花费多少钱?
思路:
看到题目第一反应是完全背包,可是仔细想想之后,发现背包公式并不适用。后来想到了贪心,时间太晚,逻辑混乱陆,没AC。今早上又补了补,大概就是:按照单位毫升的价格(c[i]/2i−1)降序来排列,首先第一家一定是最划算的,然后看在第一家购买剩余多少,继而判断是继续在当前家买便宜还是到另一家去买便宜,进行一个判断即可
代码:
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cstdio>
#include<cmath>
#include<string>
#define ll long long
#define maxn 30
using namespace std;
//结构体,并重载<运算符
struct Node{
ll vol; //体积
ll cost;//花费
bool operator<(Node a){//重载<运算符
return (a.cost*vol>cost*a.vol);
}
}s[maxn+5];
ll n,l;
//left为剩余的体积,cost为当前的花费,num为到了第几家
ll dfs(ll left,ll cost,ll num){
ll count=left/s[num].vol; //判断当前家需要购买几瓶
cost+=count*s[num].cost;//加上花费
left-=count*s[num].vol;//计算剩余体积
if(left==0) return cost; //如果不剩余了,则方案最优,直接返回
//if(num==n) return cost+s[num].cost;
return min(cost+s[num].cost,dfs(left,cost,num+1));//处理剩余部分,再买一杯超出需要的量或者去下一家店,两者去最少花费的那种。
}
int main(){
s[1].vol=1;
cin>>n>>l;
cin>>s[1].cost;
for(int i=2;i<=n;i++){
cin>>s[i].cost;
s[i].vol=2*s[i-1].vol;
}
sort(s+1,s+1+n);
// ll tempm=0;
// ll left=1;
// ll temp;
cout<<dfs(l,0,1)<<endl;
return 0;
}
考虑二进制,因为每个物品的体积都是2的多少次方,且前面的体积一定是后面的体积的1/2,然后如果当前的可以被前面的替代,那么就考虑替换下来,然后判断每一位i是否与l符合
代码:
#include<bits/stdc++.h> using namespace std; typedef long long ll; ll n,l,ans; ll a[34]; int main() { ans=0; scanf("%lld%lld",&n,&l); for(int i=0;i<n;i++) { scanf("%lld",&a[i]); if(i!=0&&a[i]>2*a[i-1]) a[i]=2*a[i-1]; } for(int i=n;i<32;i++) a[i]=2*a[i-1]; for(int i=0;i<32;i++) { if(ans>a[i]) ans=a[i]; if(l&(1<<i)) ans+=a[i]; } printf("%lld\n",ans); return 0; }