#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=5e3+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,300+3)
{
mn=min(to(i),mn);
}
cout<<mn<<'\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2093 ms |
460 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Incorrect |
2 ms |
460 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2093 ms |
460 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2093 ms |
460 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |