#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ar array
#define fr first
#define sc second
#define vec vector
#define ret return
#define ins insert
#define mk make_pair
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define int long long
#define pii pair<int,int>
#define all(s) s.begin(), s.end()
#define allr(s) s.rbegin(), s.rend()
#define NeedForSpeed ios::sync_with_stdio(0),cin.tie(0);
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
const int N=1e6+12,INF=1e9,mod = 1e9+7;
bool iswowel(char n){ n = toupper(n); if(n == 'A' || n == 'O' || n == 'U' || n == 'E' || n == 'I') return true; else return false;}
int n,m,k,x,y,l,r,o,timer,ans,res,ok,mx,mn = INF;
int a[N], tin[N], tout[N], up[N][30], b, c;
vec<int> g[N];
void dfs(int v, int pred){
tin[v] = timer++;
up[v][0] = pred;
for(int i=1; i<=30; i++)
up[v][i] = up[up[v][i-1]][i-1];
for(int to : g[v]){
if(to != pred){
dfs(to, v);
}
}
tout[v] = timer++;
}
bool upper(int a,int b){
ret tin[a] <= tin[b] && tout[b] <= tout[a];
}
//int lca(int a, int b){
// if(upper(a, b))ret a;
// if(upper(b, a))ret b;
// for(int i=30; i>=0; i--){
// if(!upper(up[a][i], b)){
// a = up[a][i];
// }
// }
// ret up[a][0];
//}
main(){
NeedForSpeed
cin>>n>>k;
for(int i=1; i<=n; i++){
cin>>x>>a[i];
g[x].pb(i);
}
dfs(0, 0);
for(int mask=0; mask<(1<<n); mask++){
if(__builtin_popcount(mask) == k){
vec<int>v;
for(int i=0; i<n; i++){
if(((1<<i) & mask)){
v.pb(i+1);
}
}
ok = 1;
for(int i=0; i<k; i++){
for(int j=i+1; j<k; j++){
if(upper(v[i], v[j]) || upper(v[j], v[i])){
ok = 0;
break;
}
}
if(!ok)break;
}
if(ok){
ans = 0;
for(int i=0; i<k; i++){
ans += a[v[i]];
}
res = max(res, ans);
}
}
}
cout<<res<<endl;
return 0;
}
Compilation message
biochips.cpp:61:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
61 | main(){
| ^~~~
biochips.cpp: In function 'void dfs(long long int, long long int)':
biochips.cpp:39:12: warning: iteration 29 invokes undefined behavior [-Waggressive-loop-optimizations]
39 | up[v][i] = up[up[v][i-1]][i-1];
| ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
biochips.cpp:38:16: note: within this loop
38 | for(int i=1; i<=30; i++)
| ~^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
23756 KB |
Output is correct |
2 |
Correct |
12 ms |
23768 KB |
Output is correct |
3 |
Incorrect |
11 ms |
23756 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |