답안 #404251

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
404251 2021-05-14T02:52:39 Z jeqcho Fireworks (APIO16_fireworks) C++17
0 / 100
2000 ms 308 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pair<int,int>> vpi;

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)

#define pb push_back
#define rsz resize
#define sz(x) int(x.size())
#define all(x) begin(x), end(x)
#define fi first
#define se second

int const NpM=3e2+3;
bitset<NpM> bomb;
vi v[NpM];
vpi adj[NpM];

void dfs1(int now, int w)
{
	if(bomb[now])
	{
		v[now] = {w};
		return;
	}
	v[now].clear();
	trav(chi,adj[now])
	{
		dfs1(chi.fi, w+chi.se);
		trav(e,v[chi.fi])
		{
			v[now].pb(e);
		}
	}
}

int dfs2(int now, int x, int w, int chn)
{
	int ans=0;
	int half = sz(v[now])/2;
	int neg=0;
	trav(e,v[now])
	{
		if(e+chn<x)++neg;
	}
	while(neg>half)
	{
		++ans;
		++w;
		++chn;
		neg=0;
		trav(e,v[now])
		{
			if(e+chn<x)++neg;
		}
	}
	
	int pos=0;
	trav(e,v[now])
	{
		if(e+chn>x)++pos;
	}
	while(pos>half && w>0)
	{
		++ans;
		--w;
		--chn;
		pos=0;
		trav(e,v[now])
		{
			if(e+chn>x)++pos;
		}
	}
	if(bomb[now] && v[now][0]+chn!=x)
	{
		return 1e6;
	}
	trav(chi,adj[now])
	{
		ans += dfs2(chi.fi,x,chi.se,chn);
	}
	return ans;
}

int to(int x)
{
	return dfs2(0,x,0,0);
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	int n,m;
	cin>>n>>m;
	FOR(i,1,n+m)
	{
		int p,c;
		cin>>p>>c;
		--p;
		adj[p].pb({i,c});
		if(i>=n)
		{
			bomb[i]=1;
		}
	}
	dfs1(0,0);
	int mn=1e9;
	F0R(i,3000+3)
	{
		mn=min(to(i),mn);
	}
	cout<<mn<<'\n';
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2084 ms 204 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 204 KB Output is correct
2 Incorrect 91 ms 308 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2084 ms 204 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2084 ms 204 KB Time limit exceeded
2 Halted 0 ms 0 KB -