Submission #332220

# Submission time Handle Problem Language Result Execution time Memory
332220 2020-12-01T17:50:21 Z caoash Mag (COCI16_mag) C++17
120 / 120
531 ms 174316 KB
#include <bits/stdc++.h> 
using namespace std;

using ll = long long;

using vi = vector<int>;
using vl = vector<ll>;
#define pb push_back
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define lb lower_bound
#define ub upper_bound

using pi = pair<int,int>;
#define f first
#define s second
#define mp make_pair

namespace output {
  void pr(int x) {
    cout << x;
  }
  void pr(long x) {
    cout << x;
  }
  void pr(ll x) {
    cout << x;
  }
  void pr(unsigned x) {
    cout << x;
  }
  void pr(unsigned long x) {
    cout << x;
  }
  void pr(unsigned long long x) {
    cout << x;
  }
  void pr(float x) {
    cout << x;
  }
  void pr(double x) {
    cout << x;
  }
  void pr(long double x) {
    cout << x;
  }
  void pr(char x) {
    cout << x;
  }
  void pr(const char * x) {
    cout << x;
  }
  void pr(const string & x) {
    cout << x;
  }
  void pr(bool x) {
    pr(x ? "true" : "false");
  }

  template < class T1, class T2 > void pr(const pair < T1, T2 > & x);
  template < class T > void pr(const T & x);

  template < class T, class...Ts > void pr(const T & t,
    const Ts & ...ts) {
    pr(t);
    pr(ts...);
  }
  template < class T1, class T2 > void pr(const pair < T1, T2 > & x) {
    pr("{", x.f, ", ", x.s, "}");
  }
  template < class T > void pr(const T & x) {
    pr("{"); // const iterator needed for vector<bool>
    bool fst = 1;
    for (const auto & a: x) pr(!fst ? ", " : "", a), fst = 0;
    pr("}");
  }

  void ps() {
    pr("\n");
  } // print w/ spaces
  template < class T, class...Ts > void ps(const T & t,
    const Ts & ...ts) {
    pr(t);
    if (sizeof...(ts)) pr(" ");
    ps(ts...);
  }

  void pc() {
    cout << "]" << endl;
  } // debug w/ commas
  template < class T, class...Ts > void pc(const T & t,
    const Ts & ...ts) {
    pr(t);
    if (sizeof...(ts)) pr(", ");
    pc(ts...);
  }
  #define dbg(x...) pr("[", #x, "] = ["), pc(x);
}

#ifdef mikey 
using namespace output;
#else
using namespace output;
#define dbg(x...)
#endif

const int MX = 1000005;
const int MOD = (int) (1e9 + 7);
const ll INF = (ll) 1e18;

struct fr{
    ll x, y;

    fr(ll X = 0, ll Y = 1){
        x = X, y = Y;
        ll g = __gcd(x, y);
        x /= g, y /= g;
    }

    friend fr operator+(fr a, fr b){
        return fr(a.x * b.y + b.x * a.y, a.y * b.y);
    }

    friend fr operator-(fr a, fr b){
        return fr(a.x * b.y - b.x * a.y, a.y * b.y);
    }

    friend fr operator*(fr a, fr b){
        return fr(a.x * b.x, a.y * b.y);
    }

    friend fr operator/(fr a, fr b){
        return fr(a.x * b.y, a.y * b.x);
    }

    friend bool operator<(fr a, fr b){
        return a.x * b.y < b.x * a.y;
    }
};

int dat[MX];
vi adj[MX];
ll dp[MX], dp2[MX];
int anc[MX];

void dfs(int v, int p) {
    if (dat[v] == 1) dp[v] = 1;
    for (int to : adj[v]) {
        if (to == p) continue;
        anc[to] = v;
        dfs(to, v);
        if (dp[to] + (dat[v] == 1) > dp[v]) {
            dp[v] = dp[to] + (dat[v] == 1);
        }
    }
    if (dat[v] > 1) dp[v] = 0;
}

int main(){
#ifdef mikey 
    freopen("a.in", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n; cin >> n;
    for (int i = 0; i < n - 1; i++) {
        int u, v; cin >> u >> v;
        u--, v--;
        adj[u].pb(v), adj[v].pb(u);
    }
    memset(anc, -1, sizeof(anc));
    for (int i = 0; i < n; i++) {
        cin >> dat[i];
    }
    dfs(0, -1);
    queue<int> bfs;
    fr ans = fr(INF);
    bfs.push(0);
    while (!bfs.empty()) {
        int curr = bfs.front();
        bfs.pop();
        for (int to : adj[curr]) {
            if (to != anc[curr]) bfs.push(to);
        }
        if (anc[curr] != -1) {
            if (dat[anc[curr]] == 1) {
                dp2[curr] = dp2[anc[curr]] + 1;
                for (int to : adj[anc[curr]]) {
                    if (to != curr && to != anc[anc[curr]]) {
                        dp2[curr] = max(dp2[curr], dp[to] + 1);
                    }
                }
            } else {
                dp2[curr] = 0;
            }
        }
        // dbg(curr, dp[curr], dp2[curr]);
        ll best = dp2[curr], sbest = 0;
        for (int to : adj[curr]) {
            if (to != anc[curr]) {
                if (dp[to] > best) {
                    sbest = best, best = dp[to];
                } else if (dp[to] > sbest) {
                    sbest = dp[to];
                }
            }
        }
        fr ret = {dat[curr], best + sbest + 1};
        if (ret < ans) ans = ret;
        ret = {dat[curr]};
        if (ret < ans) ans = ret;
    }
    cout << ans.x << "/" << ans.y << '\n';
}

Compilation message

mag.cpp:104: warning: "dbg" redefined
  104 | #define dbg(x...)
      | 
mag.cpp:97: note: this is the location of the previous definition
   97 |   #define dbg(x...) pr("[", #x, "] = ["), pc(x);
      |
# Verdict Execution time Memory Grader output
1 Correct 19 ms 27756 KB Output is correct
2 Correct 21 ms 27884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 27756 KB Output is correct
2 Correct 19 ms 27756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 379 ms 100096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 27756 KB Output is correct
2 Correct 469 ms 156012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 475 ms 154588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 496 ms 78812 KB Output is correct
2 Correct 378 ms 65516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 402 ms 80988 KB Output is correct
2 Correct 100 ms 33260 KB Output is correct
3 Correct 479 ms 174316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 97 ms 33388 KB Output is correct
2 Correct 432 ms 94188 KB Output is correct
3 Correct 531 ms 61548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 381 ms 76004 KB Output is correct
2 Correct 434 ms 94700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 446 ms 79852 KB Output is correct
2 Correct 447 ms 54452 KB Output is correct