so approach is sort every node by increasing order of subtree sum( self included) , now whoever is less than -X , that can be removed as it will yeild positive profit, then i updated parents to accomodate that node removal and update children to have subtree sum 0
also this should be amortized linear , but i got TLE too , did i miss anything
Can someone provide a good test case for the problem, I tried solving it using dictionary and approach similar to given above but got WA. https://ideone.com/JsVXlp (python)
I think Haskell’s TLE (< 1.01) is too restrictive compared to other languages.
I tested Python3 and C++14 with the same algorithm (as the Editorial) and they pass.
In my attempt of the Haskell codes, I tried Array of List, Sequences, IntSet, and IntMap of Sequences to see all in vain.
Another point of view of unfair TLE for Haskell is that CodeChef configuration does not allow to use Data.Vector which is considered to be the fastest.
I just want to have you consider to either loosen up the TLE for Haskell or to allow Data.Vector library to be used.
Thank you,
while t!=0:
n,k=map(int,input().split())
lis1=list(map(int,input().split()))
dic=dict()
for i in range(1,n+1):
dic[i]=[]
for i in range(n-1):
a,b=map(int,input().split())
if a in dic:
dic[a].append(b)
else:
dic[a]=[]
dic[a].append(b)
print(maxi(1))
t=t-1
i have declared list,dictionary and (X) global.
is it wrong ?
Here is my easy to understand ac code,you have to sum the value of nodes of a given subtree and check whether its value is less than -x or not,if it is replace it with -x
//subtree removal
#include<bits/stdc++.h> #define ll long long int
using namespace std;
vectoradj[100005];
ll a[100005];
ll dp[100005];
ll visited[100005];
ll n,x;
One thing that is still confusing me is the number of operations ‘k’. Our objective is to minimize the value “X*k” and hence k should only take values 0 or 1 (depending on whether we remove any subtree or not). As per my solution, I found the maximum value of remaining nodes by subtracting each subtree sum from whole tree sum i.e. max(sum(root)- sum[i](for all subtrees)).
Why we need to add an edge from v to u if we have initialized an edge from u to v already?
If we don’t add an edge from v to u I got wrong answer why is it so? Please explain.
Oh thanks a lot.
I understand now. I used to feel that vectors, like arrays, are sent by reference but instead vectors are sent by value and a copy is generated. Due to recursive nature of function, sending by value leads to TLE. After sending by reference, it works correctly.
The question states that you can remove ANY number of subtrees, which are ‘k’ and your profit at the end is the (sum of values of the nodes left in tree - x * k).
You can remove more than one subtree also to maximize the profit. You have to take some test cases and understand that.
Now, max sum we get by trying removing every subtree, (sum[1]-sum[i], for each i<=N), is 1.
now, since k = 1, answer will be 1-5*1, i.e. -4 which is expected.
I’m not sure what kind of case seem to be missing.