답안 #332217

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
332217 2020-12-01T17:37:24 Z caoash Mag (COCI16_mag) C++17
24 / 120
491 ms 171116 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]);
        if (dat[curr] > 1) {
            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;
        }
    }
    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);
      |
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 27756 KB Output is correct
2 Incorrect 20 ms 27756 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 27884 KB Output is correct
2 Correct 19 ms 27756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 384 ms 113644 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 27884 KB Output is correct
2 Incorrect 491 ms 171116 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Correct 466 ms 167420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 464 ms 93548 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 403 ms 98140 KB Output is correct
2 Incorrect 110 ms 34540 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 123 ms 33004 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 393 ms 91236 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 444 ms 91480 KB Output is correct
2 Incorrect 422 ms 61676 KB Output isn't correct