답안 #597224

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
597224 2022-07-15T18:00:10 Z ThegeekKnight16 Arboras (RMI20_arboras) C++14
11 / 100
5000 ms 12680 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int MAXN = 1e5 + 10;
vector<int> grafo[MAXN];
vector<ll> pesos[MAXN]; 
int indices[MAXN];
int pai[MAXN];
ll dp[MAXN][2];
ll modulo = 1e9 + 7;

void dfs(int v)
{
    dp[v][0] = 0LL; dp[v][1] = 0LL;
    
    for (int i = 0; i < grafo[v].size(); i++)
    {
        int viz = grafo[v][i];
        ll p = pesos[v][i];
        dfs(viz);
        if (dp[viz][0] + p >= dp[v][0])
        {
            dp[v][1] = dp[v][0];
            dp[v][0] = (dp[viz][0] + p);
        }
        else if (dp[viz][0] + p >= dp[v][1]) dp[v][1] = (dp[viz][0] + p);
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int N;
    cin >> N;
    for (int i = 1; i < N; i++)
    {
        cin >> pai[i];
        grafo[pai[i]].push_back(i);
    }
    for (int i = 1; i < N; i++)
    {
        ll p;
        cin >> p;
        pesos[pai[i]].push_back(p);
        indices[i] = pesos[pai[i]].size()-1;
    }
    int Q;
    cin >> Q;
    dfs(0);
    ll resp = 0;
    for (int i = 0; i < N; i++) {resp += (dp[i][0] + dp[i][1]) % (modulo); resp = resp % modulo;}
    cout << resp;
    for (int q = 1; q <= Q; q++)
    {
        int v;
        ll add;
        cin >> v >> add;
        pesos[pai[v]][indices[v]] += add;
        dfs(0);
        resp = 0LL;
        for (int i = 0; i < N; i++) {resp += (dp[i][0] + dp[i][1]) % (modulo); resp = resp % modulo;}
        cout << '\n' << resp;
    }
}

Compilation message

arboras.cpp: In function 'void dfs(int)':
arboras.cpp:16:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   16 |     for (int i = 0; i < grafo[v].size(); i++)
      |                     ~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 5076 KB Output is correct
2 Correct 25 ms 5148 KB Output is correct
3 Correct 26 ms 5040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5059 ms 10844 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5065 ms 12680 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 5076 KB Output is correct
2 Correct 25 ms 5148 KB Output is correct
3 Correct 26 ms 5040 KB Output is correct
4 Execution timed out 5059 ms 10844 KB Time limit exceeded
5 Halted 0 ms 0 KB -