#include <bits/stdc++.h>
#if !defined(LOL)
#include "friend.h"
#endif
using namespace std;
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
const int maxn = (int)2e5+10;
vector<int> adj[maxn];
int used[maxn];
void add_edge(int a, int b){
adj[a].pb(b); adj[b].pb(a);
}
void add_friends(int a, int b){
for(auto u : adj[a]) add_edge(u,b);
}
bool chk(int s){
for(auto u : adj[s])
if(used[u]) return false;
return true;
}
int C[maxn], dp[maxn][3];
void dfs(int s, int p=-1){
dp[s][0]=dp[s][1]=0; dp[s][1]=C[s];
for(auto u : adj[s]){
if(u==p) continue; dfs(u,s);
dp[s][1]+=dp[u][0];
dp[s][0]+=max(dp[u][0],dp[u][1]);
}
}
int findSample(int n, int c[], int h[], int pro[])
{
int ans = 0;
for(int i = 0; i < n; i++) C[i]=c[i];
for(int i = 1; i < n; i++){
if(pro[i]!=1) add_edge(h[i],i);
if(pro[i]) add_friends(h[i],i);
}
dfs(0,-1); return max(dp[0][0], dp[0][1]);
}
#if defined(LOL)
int main(void)
{
#define __MAXSIZE__ 100002
int confidence[__MAXSIZE__];
int host[__MAXSIZE__];
int protocol[__MAXSIZE__];
int n,i;
// Number of people
assert(scanf("%d",&n)==1);
// Confidence
for(i=0;i<n;i++)
assert(scanf("%d",&confidence[i])==1);
// Host and Protocol
for(i=1;i<n;i++)
assert(scanf("%d %d",&host[i],&protocol[i])==2);
// Answer
printf("%d\n",findSample(n,confidence,host,protocol));
return 0;
}
#endif
Compilation message
friend.cpp: In function 'void dfs(int, int)':
friend.cpp:32:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
32 | if(u==p) continue; dfs(u,s);
| ^~
friend.cpp:32:28: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
32 | if(u==p) continue; dfs(u,s);
| ^~~
friend.cpp: In function 'int findSample(int, int*, int*, int*)':
friend.cpp:40:9: warning: unused variable 'ans' [-Wunused-variable]
40 | int ans = 0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
4 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Runtime error |
42 ms |
65536 KB |
Execution killed with signal 9 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4992 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
41 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
4 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
4 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
5 ms |
5076 KB |
Output is correct |
9 |
Correct |
4 ms |
4948 KB |
Output is correct |
10 |
Correct |
4 ms |
4948 KB |
Output is correct |
11 |
Correct |
4 ms |
5096 KB |
Output is correct |
12 |
Correct |
5 ms |
5024 KB |
Output is correct |
13 |
Correct |
4 ms |
5076 KB |
Output is correct |
14 |
Correct |
4 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
5020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Runtime error |
37 ms |
65536 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |