1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <climits>
#include <vector>

using namespace std;

int main(){
    int n,q;
    scanf("%d %d",&n,&q);
    long long int t[n];
    for(int i=0;i<n;i++) scanf("%lld",&t[i]);
    long long int dist[n][n];
    bool neighbour[n][n];
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            dist[i][j]=0;
            neighbour[i][j]=false;
        }
    }
    for(int i=0;i<(n-1);i++){
        int a,b;
        long long int l;
        scanf("%d %d %lld",&a,&b,&l);
        dist[a-1][b-1] =l;
        dist[b-1][a-1] =l;
        neighbour[a-1][b-1]=true;
        neighbour[b-1][a-1]=true;
    }

    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(neighbour[i][j]){
                for(int k=0;k<n;k++){
                    if(neighbour[i][k] && k!=j){
                        dist[k][j] += dist[i][j];
                        dist[j][k] += dist[i][j];
                    }
                }
            }
        }
    }

    vector<int> kolejnosc;
    kolejnosc.push_back(0);
    for(int i=0;i<q;i++){
        int w;
        scanf("%d",&w);
        int acc = kolejnosc.back();
        if(w==1){
            int v;
            long long int d;
            scanf("%d %lld",&v,&d);
            t[v-1] = d;
        }
        else{
            int a,b;
            long long int d;
            scanf("%d %d %lld",&a,&b,&d);
            int change = d-dist[a-1][b-1];

            dist[a-1][b-1] = d;
            dist[b-1][a-1] = d;
            for(int i=0;i<n;i++){
                if(i!=(a-1) && i!=(b-1)){
                    if(!neighbour[a-1][i]){
                        if(dist[i][a-1]!=0) dist[i][a-1]+=change;
                        if(dist[a-1][i]!=0) dist[a-1][i]+=change;
                    }
                    if(!neighbour[b-1][i]){
                        if(dist[b-1][i]!=0) dist[b-1][i]+=change;
                        if(dist[i][b-1]!=0) dist[i][b-1]+=change;
                    }
                }
            }

        }

        int bestI = 0;
        long long int best = LLONG_MIN;
        for(int j=0;j<n;j++){
                if(acc!=j && (t[j]-dist[acc][j])>best){
                    bestI=j;
                    best = (t[j]-dist[acc][j]);
                }
        }

        kolejnosc.push_back(bestI);
    }
    for(int i=1;i<(q+1);i++) printf("%d ",kolejnosc[i]+1);
    printf("\n");
}
//Author: Helena Borak