Submission #73858

# Submission time Handle Problem Language Result Execution time Memory
73858 2018-08-29T06:59:16 Z funcsr Friend (IOI14_friend) C++17
0 / 100
16 ms 10052 KB
#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];
bool both[200000];

P dfs(int x, int p) {
  int dp0 = 0, dp1 = W[x];
  for (int t : Gchildren[x]) if (t != p) {
    int dp0t, dp1t;
    tie(dp0t, dp1t) = dfs(t, x);
    dp0 += dp1t;
    dp1 += dp0t;
  }
  if (!Gclone[x].empty()) {
    assert(Gclone[x].size() == 2);
    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;
    if (both[x]) 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);

      both[root] = protocol[i] == 1;
    }
  }

  return dfs(0, -1).first;
}
# Verdict Execution time Memory Grader output
1 Correct 12 ms 9720 KB Output is correct
2 Incorrect 11 ms 9816 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 12 ms 9816 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 15 ms 9816 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 13 ms 9924 KB Output is correct
2 Incorrect 15 ms 9924 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 16 ms 10008 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 13 ms 10008 KB Output is correct
2 Incorrect 14 ms 10052 KB Output isn't correct
3 Halted 0 ms 0 KB -