Submission #73860

#TimeUsernameProblemLanguageResultExecution timeMemory
73860funcsr친구 (IOI14_friend)C++17
100 / 100
114 ms15988 KiB
#include "friend.h"
#include <vector>
#include <cassert>
#include <tuple>
#define rep(i, n) for (int i=0; i<(n); i++)
#define pb push_back
using namespace std;
typedef pair<int, int> P;

vector<int> Gchildren[200000];
vector<int> Gclone[200000];
int repr[100000];
int W[200000];

P dfs(int x, int p) {
  int dp0 = 0, dp1 = W[x]>=0?W[x]:0;
  for (int t : Gchildren[x]) {
    int dp0t, dp1t;
    tie(dp0t, dp1t) = dfs(t, x);
    dp0 += dp1t;
    dp1 += dp0t;
  }
  if (!Gclone[x].empty()) {
    int t1 = Gclone[x][0], t2 = Gclone[x][1];
    int dp0t1, dp1t1;
    tie(dp0t1, dp1t1) = dfs(t1, x);
    int dp0t2, dp1t2;
    tie(dp0t2, dp1t2) = dfs(t2, x);
    dp0 += dp0t1+dp0t2;
    // both[i]
    if (W[x]==-1) dp1 += dp1t1+dp1t2;
    else dp1 += max(dp1t1+dp0t2, dp0t1+dp1t2);
  }
  dp1 = max(dp1, dp0);
  return P(dp0, dp1);
}

int findSample(int N, int A[], int host[], int protocol[]) {
  W[0] = A[0];
  repr[0] = 0;
  int V = 1;
  for (int i=1; i<N; i++) {
    int p = host[i];
    if (protocol[i] == 0) {
      Gchildren[repr[p]].pb(V);
      W[V] = A[i];
      repr[i] = V;
      V++;
    }
    else {
      // copy
      int root = repr[p], p_new = V++, x = V++;
      repr[p] = p_new;
      repr[i] = x;

      W[p_new] = W[root];
      W[x] = A[i];
      W[root] = 0;

      Gclone[root].pb(p_new);
      Gclone[root].pb(x);

      if (protocol[i] == 1) W[root] = -1; // both[i]
    }
  }

  return dfs(0, -1).second;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...