#include "friend.h"
#include <bits/stdc++.h>
#define pb push_back
using namespace std ;
// Find out best sample
const int N = 100010 ;
int confi[N] ;
int dp [N][2];
vector <int> G[N][3] ;
int go ( int u, int flag ) {
auto &rf = dp[u][flag];
if ( rf!=-1 ) return rf ;
if ( flag ) {
///take
int x = confi[u] ;
for ( auto v:G[u][0] ) x += go( v, 0 ) ;
for ( auto v:G[u][2] ) x += go( v, 0 ) ;
for ( auto v:G[u][1] ) x += go( v, 1 ) ;
rf = x ;
///dont take
x = 0 ;
int it1 = 0, it2 = 0, it3 = 0, rep3 = 0, ini= 0, rep = 0, repp = 0,repp2= 0,rep2 =0, itLast = 0 ;
for ( auto v:G[u][0] ) rep += go(v, 1), repp += go(v,0) ;
for ( auto v:G[u][1] ) rep2 += go(v, 0) ;
for ( auto v:G[u][2] ) ini += go(v,0);
repp2 = rep2 ;
G[u][0].push_back ( 100002 ) ;
while ( it1 < G[u][0].size() ) {
while ( it2 < G[u][1].size() && G[u][1][it2] < G[u][0][it1] ) {
rep2 -= go(G[u][1][it2], 0) ;
rep2 += go(G[u][1][it2], 1) ;
it2++ ;
}
while ( it3 < G[u][2].size() && G[u][2][it3] < G[u][0][it1] ) {
while ( itLast < G[u][1].size() && G[u][1][itLast] < G[u][2][it3] ) repp2 += -go(G[u][1][itLast],0) + go(G[u][1][itLast],1),itLast++ ;
rep3 = max ( rep3, ini - go(G[u][2][it3],0) + go(G[u][2][it3],1) + repp2 ) ;
it3++ ;
}
x = max ( x, max(rep2+ini, rep3)+rep ) ;
rep -= go(G[u][0][it1],1) ;
rep += go(G[u][0][it1],0) ;
it1++ ;
}
rf = max ( rf, x ) ;
}
else {
int x = 0 ;
for ( auto v:G[u][1] ) x += go ( v, 0 ) ;
for ( auto v:G[u][2] ) x += go(v,0) ;
for ( auto v:G[u][0] ) x += go ( v, 1 ) ;
rf = x ;
}
return rf ;
}
int findSample(int n,int confidence[],int host[],int protocol[]){
for ( int i = 0 ; i < n ; ++i ) confi[i] = confidence[i] ;
for ( int i = 1 ; i < n ; ++i ) G[host[i]][protocol[i]].push_back ( i ) ;
memset ( dp,-1,sizeof dp ) ;
return go(0,1) ;
}
Compilation message
friend.cpp: In function 'int go(int, int)':
friend.cpp:34:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while ( it1 < G[u][0].size() ) {
~~~~^~~~~~~~~~~~~~~~
friend.cpp:35:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while ( it2 < G[u][1].size() && G[u][1][it2] < G[u][0][it1] ) {
~~~~^~~~~~~~~~~~~~~~
friend.cpp:40:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while ( it3 < G[u][2].size() && G[u][2][it3] < G[u][0][it1] ) {
~~~~^~~~~~~~~~~~~~~~
friend.cpp:41:32: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while ( itLast < G[u][1].size() && G[u][1][itLast] < G[u][2][it3] ) repp2 += -go(G[u][1][itLast],0) + go(G[u][1][itLast],1),itLast++ ;
~~~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
8192 KB |
Output is correct |
2 |
Correct |
9 ms |
8064 KB |
Output is correct |
3 |
Correct |
9 ms |
8192 KB |
Output is correct |
4 |
Correct |
9 ms |
8192 KB |
Output is correct |
5 |
Correct |
9 ms |
8192 KB |
Output is correct |
6 |
Correct |
9 ms |
8192 KB |
Output is correct |
7 |
Correct |
9 ms |
8192 KB |
Output is correct |
8 |
Correct |
9 ms |
8192 KB |
Output is correct |
9 |
Correct |
9 ms |
8192 KB |
Output is correct |
10 |
Correct |
9 ms |
8192 KB |
Output is correct |
11 |
Correct |
9 ms |
8192 KB |
Output is correct |
12 |
Correct |
10 ms |
8192 KB |
Output is correct |
13 |
Correct |
9 ms |
8192 KB |
Output is correct |
14 |
Correct |
9 ms |
8192 KB |
Output is correct |
15 |
Correct |
9 ms |
8192 KB |
Output is correct |
16 |
Correct |
10 ms |
8192 KB |
Output is correct |
17 |
Correct |
9 ms |
8192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
8192 KB |
Output is correct |
2 |
Correct |
9 ms |
8192 KB |
Output is correct |
3 |
Correct |
9 ms |
8192 KB |
Output is correct |
4 |
Correct |
9 ms |
8192 KB |
Output is correct |
5 |
Correct |
9 ms |
8192 KB |
Output is correct |
6 |
Correct |
9 ms |
8192 KB |
Output is correct |
7 |
Correct |
10 ms |
8192 KB |
Output is correct |
8 |
Correct |
9 ms |
8192 KB |
Output is correct |
9 |
Correct |
10 ms |
8192 KB |
Output is correct |
10 |
Correct |
10 ms |
8320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
8192 KB |
Output is correct |
2 |
Correct |
9 ms |
8192 KB |
Output is correct |
3 |
Correct |
9 ms |
8192 KB |
Output is correct |
4 |
Correct |
10 ms |
8192 KB |
Output is correct |
5 |
Correct |
10 ms |
8192 KB |
Output is correct |
6 |
Correct |
10 ms |
8192 KB |
Output is correct |
7 |
Correct |
9 ms |
8192 KB |
Output is correct |
8 |
Correct |
10 ms |
8192 KB |
Output is correct |
9 |
Correct |
9 ms |
8192 KB |
Output is correct |
10 |
Correct |
9 ms |
8192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
8192 KB |
Output is correct |
2 |
Correct |
10 ms |
8192 KB |
Output is correct |
3 |
Correct |
9 ms |
8192 KB |
Output is correct |
4 |
Correct |
9 ms |
8192 KB |
Output is correct |
5 |
Correct |
10 ms |
8356 KB |
Output is correct |
6 |
Correct |
10 ms |
8192 KB |
Output is correct |
7 |
Correct |
9 ms |
8192 KB |
Output is correct |
8 |
Correct |
10 ms |
8192 KB |
Output is correct |
9 |
Correct |
9 ms |
8192 KB |
Output is correct |
10 |
Correct |
10 ms |
8192 KB |
Output is correct |
11 |
Correct |
10 ms |
8192 KB |
Output is correct |
12 |
Correct |
11 ms |
8192 KB |
Output is correct |
13 |
Correct |
11 ms |
8192 KB |
Output is correct |
14 |
Correct |
9 ms |
8192 KB |
Output is correct |
15 |
Correct |
10 ms |
8192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
8192 KB |
Output is correct |
2 |
Correct |
10 ms |
8192 KB |
Output is correct |
3 |
Correct |
9 ms |
8192 KB |
Output is correct |
4 |
Correct |
9 ms |
8192 KB |
Output is correct |
5 |
Correct |
9 ms |
8192 KB |
Output is correct |
6 |
Correct |
9 ms |
8192 KB |
Output is correct |
7 |
Correct |
9 ms |
8192 KB |
Output is correct |
8 |
Correct |
9 ms |
8192 KB |
Output is correct |
9 |
Correct |
9 ms |
8320 KB |
Output is correct |
10 |
Correct |
10 ms |
8448 KB |
Output is correct |
11 |
Correct |
10 ms |
8192 KB |
Output is correct |
12 |
Correct |
10 ms |
8192 KB |
Output is correct |
13 |
Correct |
10 ms |
8192 KB |
Output is correct |
14 |
Correct |
10 ms |
8192 KB |
Output is correct |
15 |
Correct |
10 ms |
8192 KB |
Output is correct |
16 |
Correct |
10 ms |
8192 KB |
Output is correct |
17 |
Correct |
9 ms |
8192 KB |
Output is correct |
18 |
Correct |
11 ms |
8192 KB |
Output is correct |
19 |
Correct |
9 ms |
8192 KB |
Output is correct |
20 |
Correct |
10 ms |
8192 KB |
Output is correct |
21 |
Correct |
12 ms |
8192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
8192 KB |
Output is correct |
2 |
Correct |
12 ms |
8192 KB |
Output is correct |
3 |
Correct |
9 ms |
8192 KB |
Output is correct |
4 |
Correct |
9 ms |
8192 KB |
Output is correct |
5 |
Correct |
9 ms |
8192 KB |
Output is correct |
6 |
Correct |
10 ms |
8192 KB |
Output is correct |
7 |
Correct |
9 ms |
8192 KB |
Output is correct |
8 |
Correct |
11 ms |
8192 KB |
Output is correct |
9 |
Correct |
9 ms |
8192 KB |
Output is correct |
10 |
Correct |
9 ms |
8192 KB |
Output is correct |
11 |
Correct |
9 ms |
8192 KB |
Output is correct |
12 |
Correct |
90 ms |
14456 KB |
Output is correct |
13 |
Correct |
44 ms |
11640 KB |
Output is correct |
14 |
Correct |
79 ms |
14200 KB |
Output is correct |
15 |
Correct |
86 ms |
14308 KB |
Output is correct |