답안 #827514

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
827514 2023-08-16T14:15:37 Z MasterTaster 바이오칩 (IZhO12_biochips) C++14
50 / 100
210 ms 524288 KB
#include <bits/stdc++.h>
 
#define ll long long
#define pii pair<int, int>
#define xx first
#define yy second
#define pb push_back
#define MAXN 200010
#define MAXM 510
#define MAXX 100000010
 
using namespace std;
 
ll n, m, p[MAXN], w[MAXN], gde[MAXN], r[MAXN], dp[MAXN][MAXM];
bool bio[MAXN];
vector<int> g[MAXN], svi;
void dfs(int u)
{
	bio[u]=true;
 
	svi.pb(u);
	gde[u]=svi.size()-1;
	dp[gde[u]][1]=w[u];
 
	for (auto v:g[u])
	{
		if (!bio[v])
			dfs(v);
	}
 
	r[u]=svi.size();
	///cout<<u<<":"<<endl;
	///for (int i=0; i<=m; i++) cout<<dp[u][i]<<" ";
	///cout<<endl;
}
 
int main(){
	cin>>n>>m;
 
	int koren=1;
	for (int i=1; i<=n; i++)
	{
		cin>>p[i]>>w[i];
		if (p[i]==0) { koren=i; continue; }
		g[p[i]].pb(i); g[i].pb(p[i]);
	}
 
	dfs(koren);
 
	//for (int i=0; i<svi.size(); i++) cout<<svi[i]<<" ";
	//cout<<endl;
 
	for (int i=svi.size()-1; i>=0; i--)
	{
		for (int j=0; j<=m; j++)
		{
			if (j==0) { dp[i][j]=0; continue; }
			if (i!=svi.size()-1) dp[i][j]=max(dp[i][j], dp[i+1][j]);
			//else dp[i][1]=w[svi[i]];
			if (r[svi[i]]<svi.size()) dp[i][j]=max(dp[i][j], dp[r[svi[i]]][j-1]+w[svi[i]]);
 
			//cout<<i<<" "<<j<<": "<<dp[i][j]<<endl;
		}
	}
	cout<<dp[0][m];
}

Compilation message

biochips.cpp: In function 'int main()':
biochips.cpp:58:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |    if (i!=svi.size()-1) dp[i][j]=max(dp[i][j], dp[i+1][j]);
      |        ~^~~~~~~~~~~~~~
biochips.cpp:60:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |    if (r[svi[i]]<svi.size()) dp[i][j]=max(dp[i][j], dp[r[svi[i]]][j-1]+w[svi[i]]);
      |        ~~~~~~~~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Incorrect 2 ms 4948 KB Output isn't correct
3 Correct 2 ms 5332 KB Output is correct
4 Correct 21 ms 41044 KB Output is correct
5 Correct 24 ms 45648 KB Output is correct
6 Correct 25 ms 45652 KB Output is correct
7 Runtime error 205 ms 524288 KB Execution killed with signal 9
8 Runtime error 210 ms 524288 KB Execution killed with signal 9
9 Runtime error 208 ms 524288 KB Execution killed with signal 9
10 Runtime error 209 ms 524288 KB Execution killed with signal 9