Submission #23054

# Submission time Handle Problem Language Result Execution time Memory
23054 2017-05-02T14:15:15 Z model_code Mag (COCI16_mag) C++11
84 / 120
1066 ms 262144 KB
// zadatak Mag, 1. kolo
// Domagoj Bradac

#include <bits/stdc++.h>
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

typedef pair<int, int> P;
#define X first
#define Y second
#define TRACE(x) cerr << #x << " " << x << endl;

const int MAX = 1<<20;

bool manji(const P &a, const P &b)
{
  return (long long) a.X * b.Y < (long long) a.Y * b.X;
}

int p[MAX];
int dpup[MAX], dpdown[MAX];
vector <int> L[MAX], R[MAX];
int pozl[MAX], pozr[MAX];
vector <int> V[MAX];
P rje = P(1e9, 1);

void dfsdown(int node, int par)
{
  L[node].push_back(0);
  for (auto it : V[node]) {
    if (it != par) {
      dfsdown(it, node);
      pozl[it] = (int) L[node].size() - 1;
      L[node].push_back(max(L[node].back(), p[it] == 1 ? dpdown[it] + 1 : 0));
    }
  }
  
  R[node].push_back(0);
  for (int i=(int) V[node].size()-1; i>=0; i--) {
    int it = V[node][i];
    if (it != par) {
      pozr[it] = (int) R[node].size() - 1;
      R[node].push_back(max(R[node].back(), p[it] == 1 ? dpdown[it] + 1 : 0));
    }
  }

  dpdown[node] = L[node].back();
}

void dfsup(int node, int par)
{
  if (node) {
    dpup[node] = (p[par] == 1 ? (1 + max(dpup[par], max(L[par][pozl[node]], R[par][pozr[node]]))) : 0);
  }
  
  P a = P(p[node], 1);
  if (manji(a, rje))
    rje = a;
  int mx = dpup[node];
   
  for (auto it : V[node]) {
    if (it == par)
      continue;
    
    int val = (p[it] == 1 ? dpdown[it] + 1 : 0);
    P tmp = P(p[node], 1 + mx + val);
    if (manji(tmp, rje))
      rje = tmp;
    mx = max(mx, val);
    dfsup(it, node);
  }
}

int main()
{
  int n;
  scanf("%d", &n);

  for (int i=0; i<n-1; i++) {
    int a, b;
    scanf("%d%d", &a, &b); a--; b--;
    V[a].push_back(b);
    V[b].push_back(a);
  }

  for (int i=0; i<n; i++)
    scanf("%d", &p[i]);

  dfsdown(0, -1);
  dfsup(0, -1);

  int gc = __gcd(rje.X, rje.Y);
  // TRACE(rje.X); TRACE(rje.Y);
  printf("%d/%d\n", rje.X / gc, rje.Y / gc);

  return 0;
}

Compilation message

mag.cpp: In function 'int main()':
mag.cpp:80:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &n);
                  ^
mag.cpp:84:26: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d", &a, &b); a--; b--;
                          ^
mag.cpp:90:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &p[i]);
                       ^
# Verdict Execution time Memory Grader output
1 Correct 26 ms 96360 KB Output is correct
2 Correct 19 ms 96228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 96360 KB Output is correct
2 Correct 23 ms 96360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 633 ms 214832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 96228 KB Output is correct
2 Memory limit exceeded 549 ms 262144 KB Memory limit exceeded
# Verdict Execution time Memory Grader output
1 Memory limit exceeded 496 ms 262144 KB Memory limit exceeded
# Verdict Execution time Memory Grader output
1 Correct 733 ms 189288 KB Output is correct
2 Correct 519 ms 164820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 683 ms 197320 KB Output is correct
2 Correct 229 ms 105996 KB Output is correct
3 Memory limit exceeded 546 ms 262144 KB Memory limit exceeded
# Verdict Execution time Memory Grader output
1 Correct 229 ms 105996 KB Output is correct
2 Correct 783 ms 189992 KB Output is correct
3 Correct 986 ms 144804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 613 ms 184444 KB Output is correct
2 Correct 793 ms 188356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 759 ms 190476 KB Output is correct
2 Correct 1066 ms 144804 KB Output is correct