#include <cstdio>
#include <vector>
#include <stack>
#include <utility>
#define MAX 100001
#define MIN_VALUE -9223372036854775807
using namespace std;
vector< pair < int, long long int > > graf[MAX];
int subtree[MAX], enterTime[MAX], deep[MAX], p[MAX][22], time = 0, LOG = 1, n, q;
long long int totalcost[MAX], banany[MAX];
void dfs(int u, long long int cost){
subtree[u] = 1;
totalcost[u] = cost;
enterTime[u] = time;
time++;
for(int i = 0; i < graf[u].size(); ++i){
if(subtree[ graf[u][i].first ] == 0){
p[ graf[u][i].first ][0] = u;
dfs(graf[u][i].first, graf[u][i].second + totalcost[u]);
subtree[u] += subtree[graf[u][i].first];
}
}
}
void dfsUpdate(int u, long long int cost){
totalcost[u] = cost;
for(int i = 0; i < graf[u].size(); ++i){
if(enterTime[ graf[u][i].first ] > enterTime[u]){
dfsUpdate(graf[u][i].first, graf[u][i].second + totalcost[u]);
}
}
}
inline bool isAncestor(int u, int v){
if (enterTime[v] < enterTime[u] || enterTime[v] >= enterTime[u]+subtree[u]){
return false;
}
return true;
}
inline int lca(int A, int B){
if (isAncestor(A, B)){ return A; }
if (isAncestor(B, A)){ return B; }
int l, u;
l = LOG;
u = A;
while (l >= 0){
if(!isAncestor(p[u][l], B))
u = p[u][l];
l--;
}
return p[u][0];
}
inline int nextCity(int u){
long long int max = MIN_VALUE, tempValue = 0;
int index = u;
int father;
for(int i = 1; i <= n; ++i){
if(u == i) { continue; }
father = lca(u, i);
tempValue = banany[i] + totalcost[i] - totalcost[father] + totalcost[u] - totalcost[father];
if(tempValue > max){
max = tempValue;
index = i;
}
}
return index;
}
int main(){
int a, b, t, actual = 1;
long long int c;
scanf("%d %d", &n, &q);
for(int i = 1; i <= n; ++i){
scanf("%lld ", &banany[i]);
}
for(int i = 0; i < n-1; ++i){
scanf("%d %d %lld", &a, &b, &c);
graf[a].push_back(make_pair(b, -c));
graf[b].push_back(make_pair(a, -c));
}
while ((1<<LOG) < n) LOG++;
p[1][0] = 1;
dfs(1, 0);
for (int i = 1; i <= LOG; ++i){
for (int j = 1; j <= n; ++j){
p[j][i] = p[p[j][i-1]][i-1];
}
}
for(int i = 0; i < q; ++i){
scanf("%d", &t);
if(t == 1){
scanf("%d %lld", &a, &c);
banany[a] = c;
} else{
scanf("%d %d %lld", &a, &b, &c);
for (int i = 0; i < graf[a].size(); ++i){
if(graf[a][i].first == b){
graf[a][i].second = -c;
break;
}
}
for (int i = 0; i < graf[b].size(); ++i){
if(graf[b][i].first == a){
graf[b][i].second = -c;
break;
}
}
if(isAncestor(a, b)){
dfsUpdate(b, totalcost[a]-c);
}else{
dfsUpdate(a, totalcost[b]-c);
}
}
actual = nextCity(actual);
printf("%d ", actual);
}
return 0;
}
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | #include <cstdio> #include <vector> #include <stack> #include <utility> #define MAX 100001 #define MIN_VALUE -9223372036854775807 using namespace std; vector< pair < int, long long int > > graf[MAX]; int subtree[MAX], enterTime[MAX], deep[MAX], p[MAX][22], time = 0, LOG = 1, n, q; long long int totalcost[MAX], banany[MAX]; void dfs(int u, long long int cost){ subtree[u] = 1; totalcost[u] = cost; enterTime[u] = time; time++; for(int i = 0; i < graf[u].size(); ++i){ if(subtree[ graf[u][i].first ] == 0){ p[ graf[u][i].first ][0] = u; dfs(graf[u][i].first, graf[u][i].second + totalcost[u]); subtree[u] += subtree[graf[u][i].first]; } } } void dfsUpdate(int u, long long int cost){ totalcost[u] = cost; for(int i = 0; i < graf[u].size(); ++i){ if(enterTime[ graf[u][i].first ] > enterTime[u]){ dfsUpdate(graf[u][i].first, graf[u][i].second + totalcost[u]); } } } inline bool isAncestor(int u, int v){ if (enterTime[v] < enterTime[u] || enterTime[v] >= enterTime[u]+subtree[u]){ return false; } return true; } inline int lca(int A, int B){ if (isAncestor(A, B)){ return A; } if (isAncestor(B, A)){ return B; } int l, u; l = LOG; u = A; while (l >= 0){ if(!isAncestor(p[u][l], B)) u = p[u][l]; l--; } return p[u][0]; } inline int nextCity(int u){ long long int max = MIN_VALUE, tempValue = 0; int index = u; int father; for(int i = 1; i <= n; ++i){ if(u == i) { continue; } father = lca(u, i); tempValue = banany[i] + totalcost[i] - totalcost[father] + totalcost[u] - totalcost[father]; if(tempValue > max){ max = tempValue; index = i; } } return index; } int main(){ int a, b, t, actual = 1; long long int c; scanf("%d %d", &n, &q); for(int i = 1; i <= n; ++i){ scanf("%lld ", &banany[i]); } for(int i = 0; i < n-1; ++i){ scanf("%d %d %lld", &a, &b, &c); graf[a].push_back(make_pair(b, -c)); graf[b].push_back(make_pair(a, -c)); } while ((1<<LOG) < n) LOG++; p[1][0] = 1; dfs(1, 0); for (int i = 1; i <= LOG; ++i){ for (int j = 1; j <= n; ++j){ p[j][i] = p[p[j][i-1]][i-1]; } } for(int i = 0; i < q; ++i){ scanf("%d", &t); if(t == 1){ scanf("%d %lld", &a, &c); banany[a] = c; } else{ scanf("%d %d %lld", &a, &b, &c); for (int i = 0; i < graf[a].size(); ++i){ if(graf[a][i].first == b){ graf[a][i].second = -c; break; } } for (int i = 0; i < graf[b].size(); ++i){ if(graf[b][i].first == a){ graf[b][i].second = -c; break; } } if(isAncestor(a, b)){ dfsUpdate(b, totalcost[a]-c); }else{ dfsUpdate(a, totalcost[b]-c); } } actual = nextCity(actual); printf("%d ", actual); } return 0; } |
English