Cod sursa(job #1391535)

Utilizator AdrianaMAdriana Moisil AdrianaM Data 18 martie 2015 00:11:05
Problema Guvern Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.3 kb
#include <fstream>
#include <vector>
#define INF 0x3f3f3f3f
using namespace std;

ifstream is("guvern.in");
ofstream os("guvern.out");

using VB = vector<bool>;
using VI = vector<int>;
using VVI = vector<VI>;

int n, answ;
VI b;
VVI g, c;

void Read();
void DFS(int nod);

int main()
{
    Read();
    c = VVI(n + 1, VI(n + 1));
    DFS(1);
    os << answ;
    is.close();
    os.close();
    return 0;
}

void DFS(int nod)
{
    c[nod] = VI(n + 1, INF);
    c[nod][1] = b[nod];
    for ( const auto& nodv : g[nod] )
        if ( !c[nodv][1] )
        {
            DFS(nodv);
            //os << nod << "\n";
            for ( int i = n; i; --i )
            {
                for ( int k = 1; k <= i; ++k )
                    if ( c[nodv][k] <= c[nod][i - k] )
                    c[nod][i] = min(c[nod][i], max(c[nodv][k], c[nod][i - k]));
                if ( c[nod][i] != INF )
                    answ = max(answ, i);
                //os << c[nod][i] << " ";
            }
        }
}

void Read()
{
    is >> n;
    g = VVI(n + 1);
    int n1, n2;
    for ( int i = 1; i < n; ++i )
    {
        is >> n1 >> n2;
        g[n1].push_back(n2);
        g[n2].push_back(n1);
    }
    b = VI(n + 1);
    for ( int i = 1; i <= n; ++i )
        is >> b[i];
}