#include <bits/stdc++.h>
#define DIM 100010
using namespace std;
int v[DIM],cost[DIM],d[DIM],a[DIM],w[DIM];
long long dp[DIM][1010];
vector <int> L[DIM];
map <int,long long> s[DIM];
set <int> aux;
int n,m,k,i,j,x,y;
void dfs (int nod, int tata){
int ok = 0;
for (auto vecin : L[nod])
if (vecin != tata){
ok = 1;
dfs (vecin,nod);
}
for (auto vecin : L[nod]){
if (vecin == tata)
continue;
if (s[nod].size() < s[vecin].size())
swap(s[nod],s[vecin]);
for (auto it : s[vecin])
s[nod][it.first] += it.second;
}
if (v[nod]){
long long val = cost[nod];
s[nod][v[nod]] += val;
while (val > 0){
map <int,long long> :: iterator it = s[nod].upper_bound(v[nod]);
if (it == s[nod].end())
break;
val -= it->second;
if (val > 0)
s[nod].erase(it);
else s[nod][it->first] = -val;
}
}
}
int cautare_binara (int v[], int n, int val){
int st = 1, dr = n;
while (st <= dr){
int mid = (st+dr)>>1;
if (v[mid] == val)
return mid;
if (v[mid] < val)
st = mid+1;
else dr = mid-1;
}
}
int main (){
//ifstream cin ("date.in");
//ofstream cout ("date.out");
cin>>n>>m>>k;
int ok2 = 1; /// cazul cu lant
for (i=2;i<=n;i++){
cin>>x;
L[x].push_back(i);
L[i].push_back(x);
if (x != i-1)
ok2 = 0;
}
int ok = 1; long long sum = 0;
for (i=1;i<=m;i++){
cin>>x;
cin>>v[x]>>cost[x];
if (L[x].size() > 1)
ok = 0;
sum += cost[x];
if (cost[x] != 1)
ok2 = 0;
}
if (ok){
cout<<sum;
return 0;
}
if (ok2){
int el = 0;
for (i=1;i<=n;i++)
if (v[i])
a[++el] = v[i];
reverse (a+1,a+el+1);
int sz = 0;
for (i=1;i<=el;i++){
int st = 1, dr = sz;
while (st <= dr){
int mid = (st+dr)>>1;
if (a[d[mid]] <= a[i])
st = mid+1;
else dr = mid-1;
}
if (st == sz+1)
sz++;
d[st] = i;
}
cout<<sz;
return 0;
}
/// normalizez v urii
int el = 0;
for (i=1;i<=n;i++)
if (v[i])
w[++el] = v[i];
sort (w+1,w+el+1);
int el2 = 1;
for (i=2;i<=m;i++)
if (w[i] != w[i-1])
w[++el2] = w[i];
el = el2;
for (i=1;i<=n;i++){
if (!v[i])
continue;
int val = v[i];
v[i] = cautare_binara (w,el,val);
}
k = el;
dfs (1,0);
long long sol = 0;
for (auto it : s[1])
sol += it.second;
cout<<sol;
return 0;
}
Compilation message
magictree.cpp: In function 'void dfs(int, int)':
magictree.cpp:14:9: warning: variable 'ok' set but not used [-Wunused-but-set-variable]
14 | int ok = 0;
| ^~
magictree.cpp: In function 'int cautare_binara(int*, int, int)':
magictree.cpp:66:1: warning: control reaches end of non-void function [-Wreturn-type]
66 | }
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
7244 KB |
Output is correct |
2 |
Correct |
6 ms |
7372 KB |
Output is correct |
3 |
Correct |
6 ms |
7372 KB |
Output is correct |
4 |
Correct |
5 ms |
7344 KB |
Output is correct |
5 |
Correct |
5 ms |
7344 KB |
Output is correct |
6 |
Correct |
5 ms |
7372 KB |
Output is correct |
7 |
Correct |
6 ms |
7368 KB |
Output is correct |
8 |
Correct |
6 ms |
7372 KB |
Output is correct |
9 |
Correct |
5 ms |
7244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
90 ms |
12176 KB |
Output is correct |
2 |
Correct |
78 ms |
11328 KB |
Output is correct |
3 |
Correct |
154 ms |
12456 KB |
Output is correct |
4 |
Correct |
139 ms |
12432 KB |
Output is correct |
5 |
Correct |
170 ms |
12356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
7372 KB |
Output is correct |
2 |
Correct |
7 ms |
7372 KB |
Output is correct |
3 |
Correct |
6 ms |
7372 KB |
Output is correct |
4 |
Correct |
135 ms |
12324 KB |
Output is correct |
5 |
Correct |
145 ms |
12792 KB |
Output is correct |
6 |
Correct |
137 ms |
12376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
186 ms |
16160 KB |
Output is correct |
2 |
Correct |
179 ms |
14896 KB |
Output is correct |
3 |
Correct |
166 ms |
19292 KB |
Output is correct |
4 |
Correct |
135 ms |
17980 KB |
Output is correct |
5 |
Correct |
163 ms |
27608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
7244 KB |
Output is correct |
2 |
Correct |
6 ms |
7372 KB |
Output is correct |
3 |
Correct |
6 ms |
7372 KB |
Output is correct |
4 |
Correct |
5 ms |
7344 KB |
Output is correct |
5 |
Correct |
5 ms |
7344 KB |
Output is correct |
6 |
Correct |
5 ms |
7372 KB |
Output is correct |
7 |
Correct |
6 ms |
7368 KB |
Output is correct |
8 |
Correct |
6 ms |
7372 KB |
Output is correct |
9 |
Correct |
5 ms |
7244 KB |
Output is correct |
10 |
Correct |
175 ms |
20160 KB |
Output is correct |
11 |
Correct |
170 ms |
18932 KB |
Output is correct |
12 |
Correct |
149 ms |
19812 KB |
Output is correct |
13 |
Correct |
126 ms |
18420 KB |
Output is correct |
14 |
Correct |
118 ms |
12380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
8396 KB |
Output is correct |
2 |
Correct |
90 ms |
12100 KB |
Output is correct |
3 |
Correct |
81 ms |
12048 KB |
Output is correct |
4 |
Correct |
72 ms |
12072 KB |
Output is correct |
5 |
Correct |
34 ms |
11884 KB |
Output is correct |
6 |
Correct |
71 ms |
15072 KB |
Output is correct |
7 |
Correct |
68 ms |
18160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
7244 KB |
Output is correct |
2 |
Correct |
6 ms |
7372 KB |
Output is correct |
3 |
Correct |
6 ms |
7372 KB |
Output is correct |
4 |
Correct |
5 ms |
7344 KB |
Output is correct |
5 |
Correct |
5 ms |
7344 KB |
Output is correct |
6 |
Correct |
5 ms |
7372 KB |
Output is correct |
7 |
Correct |
6 ms |
7368 KB |
Output is correct |
8 |
Correct |
6 ms |
7372 KB |
Output is correct |
9 |
Correct |
5 ms |
7244 KB |
Output is correct |
10 |
Correct |
7 ms |
7372 KB |
Output is correct |
11 |
Correct |
7 ms |
7372 KB |
Output is correct |
12 |
Correct |
6 ms |
7372 KB |
Output is correct |
13 |
Correct |
135 ms |
12324 KB |
Output is correct |
14 |
Correct |
145 ms |
12792 KB |
Output is correct |
15 |
Correct |
137 ms |
12376 KB |
Output is correct |
16 |
Correct |
175 ms |
20160 KB |
Output is correct |
17 |
Correct |
170 ms |
18932 KB |
Output is correct |
18 |
Correct |
149 ms |
19812 KB |
Output is correct |
19 |
Correct |
126 ms |
18420 KB |
Output is correct |
20 |
Correct |
118 ms |
12380 KB |
Output is correct |
21 |
Correct |
53 ms |
11812 KB |
Output is correct |
22 |
Correct |
170 ms |
21760 KB |
Output is correct |
23 |
Correct |
175 ms |
25924 KB |
Output is correct |
24 |
Correct |
282 ms |
37244 KB |
Output is correct |
25 |
Correct |
173 ms |
22048 KB |
Output is correct |
26 |
Correct |
227 ms |
24900 KB |
Output is correct |
27 |
Correct |
221 ms |
23144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
7244 KB |
Output is correct |
2 |
Correct |
6 ms |
7372 KB |
Output is correct |
3 |
Correct |
6 ms |
7372 KB |
Output is correct |
4 |
Correct |
5 ms |
7344 KB |
Output is correct |
5 |
Correct |
5 ms |
7344 KB |
Output is correct |
6 |
Correct |
5 ms |
7372 KB |
Output is correct |
7 |
Correct |
6 ms |
7368 KB |
Output is correct |
8 |
Correct |
6 ms |
7372 KB |
Output is correct |
9 |
Correct |
5 ms |
7244 KB |
Output is correct |
10 |
Correct |
90 ms |
12176 KB |
Output is correct |
11 |
Correct |
78 ms |
11328 KB |
Output is correct |
12 |
Correct |
154 ms |
12456 KB |
Output is correct |
13 |
Correct |
139 ms |
12432 KB |
Output is correct |
14 |
Correct |
170 ms |
12356 KB |
Output is correct |
15 |
Correct |
7 ms |
7372 KB |
Output is correct |
16 |
Correct |
7 ms |
7372 KB |
Output is correct |
17 |
Correct |
6 ms |
7372 KB |
Output is correct |
18 |
Correct |
135 ms |
12324 KB |
Output is correct |
19 |
Correct |
145 ms |
12792 KB |
Output is correct |
20 |
Correct |
137 ms |
12376 KB |
Output is correct |
21 |
Correct |
186 ms |
16160 KB |
Output is correct |
22 |
Correct |
179 ms |
14896 KB |
Output is correct |
23 |
Correct |
166 ms |
19292 KB |
Output is correct |
24 |
Correct |
135 ms |
17980 KB |
Output is correct |
25 |
Correct |
163 ms |
27608 KB |
Output is correct |
26 |
Correct |
175 ms |
20160 KB |
Output is correct |
27 |
Correct |
170 ms |
18932 KB |
Output is correct |
28 |
Correct |
149 ms |
19812 KB |
Output is correct |
29 |
Correct |
126 ms |
18420 KB |
Output is correct |
30 |
Correct |
118 ms |
12380 KB |
Output is correct |
31 |
Correct |
17 ms |
8396 KB |
Output is correct |
32 |
Correct |
90 ms |
12100 KB |
Output is correct |
33 |
Correct |
81 ms |
12048 KB |
Output is correct |
34 |
Correct |
72 ms |
12072 KB |
Output is correct |
35 |
Correct |
34 ms |
11884 KB |
Output is correct |
36 |
Correct |
71 ms |
15072 KB |
Output is correct |
37 |
Correct |
68 ms |
18160 KB |
Output is correct |
38 |
Correct |
53 ms |
11812 KB |
Output is correct |
39 |
Correct |
170 ms |
21760 KB |
Output is correct |
40 |
Correct |
175 ms |
25924 KB |
Output is correct |
41 |
Correct |
282 ms |
37244 KB |
Output is correct |
42 |
Correct |
173 ms |
22048 KB |
Output is correct |
43 |
Correct |
227 ms |
24900 KB |
Output is correct |
44 |
Correct |
221 ms |
23144 KB |
Output is correct |
45 |
Correct |
46 ms |
11460 KB |
Output is correct |
46 |
Correct |
175 ms |
21700 KB |
Output is correct |
47 |
Correct |
229 ms |
25028 KB |
Output is correct |
48 |
Correct |
290 ms |
35232 KB |
Output is correct |
49 |
Correct |
202 ms |
23356 KB |
Output is correct |
50 |
Correct |
270 ms |
24968 KB |
Output is correct |
51 |
Correct |
232 ms |
23140 KB |
Output is correct |