답안 #42247

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
42247 2018-02-23T22:45:05 Z wasyl Mag (COCI16_mag) C++11
120 / 120
540 ms 252828 KB
#include <bits/stdc++.h>
#ifndef dbg
#define dbg(...)
#endif
#define all(x) begin(x), end(x)
#define rsz(...) resize(__VA_ARGS__)
#define psh(...) push_back(__VA_ARGS__)
#define emp(...) emplace_back(__VA_ARGS__)
#define prt(...) print(cout, __VA_ARGS__)
#define dprt(...) dbg(print(cerr,__VA_ARGS__))
#define dmp(...) dprt(#__VA_ARGS__, '=', __VA_ARGS__)
using namespace std;using ll=long long;
template<typename t>using V=vector<t>;
template<typename t>void print(ostream& os, const t& a){os<<a<<'\n';}
template<typename t, typename... A>void print
(ostream& os, const t& a, A&&... b){os<<a<<' ';print(os, b...);}

struct Node : V< int >
{
    int val;
};

int n, res1, res2;
V< Node > gr;
V< V< int > > dp;

inline void dfs (int v, int oj = 1)
{
    dmp(v, oj);
    for (int s : gr[v])
        if (s != oj)
            dfs(s, v);
    
    if (gr[v].val > 2)
        return;
    
    int tres1 = 0, tres2 = 0;
    int naj = 0, tmp = 0;
    for (int s : gr[v])
        if (s != oj)
        {
            tres1 = max(tres1, naj + dp[0][s] + 1);
            tres2 = max(tres2, naj + dp[1][s] + 1);
            tres2 = max(tres2, tmp + dp[0][s] + 1);
            naj = max(naj, dp[0][s]);
            tmp = max(tmp, dp[1][s]);
        }
        
    if (gr[v].val == 2)
    {
        dp[1][v] = naj + 1;
        res2 = max(res2, tres1);
    }
    else
    {
        res1 = max(res1, tres1);
        res2 = max(res2, tres2);
        dp[0][v] = naj + 1;
        dp[1][v] = tmp + 1;
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n;
    gr.rsz(n + 1);
    dp.rsz(2, V< int >(n + 1));
        
    for (int i = 0, p, q; i < n - 1; ++i)
    {
        cin >> p >> q;
        gr[p].psh(q);
        gr[q].psh(p);        
    }

    for (int i = 1; i <= n; ++i)
        cin >> gr[i].val;
    
    dfs(1); 
    
    if (res1)
    {
        dmp(res1, res2);
        if (2 * res1 > res2)
            cout << 1 << '/' << res1 << '\n';
        else
        {
            if (res2 & 1)
                cout << 2 << '/' << res2 << '\n';
            else
                cout << 1 << '/' << res2 / 2 << '\n';
        }
    }
    else
    {
        int mini = INT_MAX;
        for (int i = 1; i <= n; ++i)
            mini = min(mini, gr[i].val);
        cout << mini << '/' << 1 << '\n';
    }    
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 492 KB Output is correct
2 Correct 2 ms 648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 392 ms 91172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 91172 KB Output is correct
2 Correct 509 ms 150108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 502 ms 165072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 435 ms 165072 KB Output is correct
2 Correct 358 ms 165072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 416 ms 165072 KB Output is correct
2 Correct 57 ms 165072 KB Output is correct
3 Correct 540 ms 230908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 70 ms 230908 KB Output is correct
2 Correct 440 ms 230908 KB Output is correct
3 Correct 365 ms 230908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 413 ms 230908 KB Output is correct
2 Correct 452 ms 235764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 467 ms 252828 KB Output is correct
2 Correct 366 ms 252828 KB Output is correct