Cod sursa(job #2408598)

Utilizator razviii237Uzum Razvan razviii237 Data 18 aprilie 2019 10:21:13
Problema A+B Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.93 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

ifstream f("arbore4.in");
ofstream g("arbore4.out");

const int maxn = 1e5+5, mod = 666013;

vector <int> v[maxn];
int n, i, x, y;
int h[maxn];

int heights(int x) {
    int sum = 1;
    for(auto u : v[x]) {
        sum += heights(u);
    }
    return h[x] = sum;
}

int solve(int x, int lft) {
    if(h[x] == 1) { return lft; }
    int ans = 0, now = 1, i;

    for(lft = lft - 1; lft >= h[x] - 1; lft --) {
        now = 1; i = lft;
        for(auto u : v[x]) {
            now *= solve(u, i); now %= mod;
            i -= h[u];
        }
        ans += now; ans %= mod;
    }
    return ans;
}

int main()
{
    f >> n;
    for(i = 1; i <= n - 1; i ++) {
        f >> x >> y; if(x > y) { swap(x, y); }
        v[x].push_back(y);
    }
    heights(1);

    g << solve(1, n);

    f.close(); g.close();
    return 0;
}