Accepted
/in/foo.cc: In function 'int main()': /in/foo.cc:14:9: warning: unused variable 'root' [-Wunused-variable] int root=1; ^~~~ /in/foo.cc:29:13: warning: 'temp' may be used uninitialized in this function [-Wmaybe-uninitialized] int temp; ^~~~
代码
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
using namespace std;
int main()
{
int N,Q;
cin >> N >> Q;
int num1[N-1];
int num2[N-1];
int fruit[N-1];
int leaf[N+1]={0};
int root=1;
for (int i=0;i<N-1;i++)
{
cin >> num1[i] >> num2[i] >> fruit[i];
leaf[num1[i]]++;
leaf[num2[i]]++;
}
if (Q==0)
{
cout << "0";
return 0;
}
for (int i=0;i<N-1-Q;i++)
{
bool flag=true;
int temp;
int tempmin;
for (int j=0;j<N-1;j++)
{
if (num1[j]<0||num2[j]<0)
continue;
if ((num1[j]!=1&&leaf[num1[j]]==1)||(num2[j]!=1&&leaf[num2[j]]==1))
{
if (flag)
{
temp=j;
tempmin=fruit[j];
flag=false;
continue;
}
if (fruit[j]<tempmin)
{
temp=j;
tempmin=fruit[j];
}
}
}
leaf[num1[temp]]--;
num1[temp]=-1;
leaf[num2[temp]]--;
num2[temp]=-1;
fruit[temp]=0;
}
int output=0;
for (int i=0;i<N-1;i++)
output+=fruit[i];
cout << output;
return 0;
}
信息
- 递交者
- 类型
- 自测
- 题目
- 二叉苹果树
- 语言
- C++
- 递交时间
- 2019-10-22 20:55:15
- 评测时间
- 2020-08-10 10:48:13
- 评测机
- 分数
- 10
- 总耗时
- 1ms
- 峰值内存
- 220.0 KiB