Cod sursa(job #2777940)

Utilizator Irina.comanIrina Coman Irina.coman Data 26 septembrie 2021 14:50:10
Problema Asmax Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.05 kb
#include <fstream>
#include <vector>
using namespace std;
vector<int> graph[16005];
int vis[16005], v[16005], s[16005];

void dfs(int node)
{
    vis[node] = true;
    s[node] += v[node];
    for (int i = 0; i < graph[node].size(); i++)
    {
        int next = graph[node][i];
        if (!vis[next])
        {
            dfs(next);
            s[node] += s[next];
        }
    }
}

int main()
{
    ifstream fin("asmax.in");
    ofstream fout("asmax.out");
    int n, x, rad, maxx = -1005;
    fin >> n;
    for (int i = 1; i <= n; i++)
    {
        fin >> v[i];
    }
    for (int i = 1; i <= n; i++)
    {
        fin >> x;
        graph[i].push_back(x);
        graph[x].push_back(i);
    }
    for (int i = 1; i <= n; i++)
    {

    }
    dfs(rad);
    for (int i = 1; i <= n; i++)
        if (s[i] >= maxx and i != rad)
            maxx = s[i];
    for (int i = 1; i <= n; i++)
    {
        if (s[i] == maxx)
           {
               fout << i;
               break;
           }
    }
    return 0;
}