#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <utility> #include <vector> using namespace std; struct Rhine_Lab { Rhine_Lab() { freopen("grape.in", "r", stdin); freopen("grape.out", "w", stdout); } }; Rhine_Lab Ptilopsis_w;
typedef long long ll; const int N = 1e5+10;
struct edge { int to, col; };
int n, m, cnt; bool vis[N]; ll ans[N]; ll a[N], sum[N]; int belong[4][N]; vector<edge> ver[N]; map<pair<int,int>, ll> mp;
void bfs1(int s, int col); void bfs2(int s, int col);
int main() { cin >> n >> m; for(int i = 1; i <= n; i++) cin >> a[i]; for(int i = 1; i <= m; i++) { int x, y, z; cin >> x >> y >> z; ver[x].push_back({y, z}); ver[y].push_back({x, z}); } for(int c = 1; c <= 3; c++) { memset(vis, 0, sizeof(vis)); for(int i = 1; i <= n; i++) if(!vis[i]) bfs1(i, c); } for(int c = 1; c <= 3; c++) { memset(vis, 0, sizeof(vis)); for(int i = 1; i <= n; i++) if(!vis[i]) bfs2(i, c); } for(int i = 1; i <= n; i++) { int x = belong[1][i], y = belong[2][i], z = belong[3][i]; ans[x] = max(ans[x], sum[x] + max(sum[y]-mp[{x,y}], sum[z]-mp[{x,z}])); ans[y] = max(ans[y], sum[y] + max(sum[x]-mp[{x,y}], sum[z]-mp[{y,z}])); ans[z] = max(ans[z], sum[z] + max(sum[x]-mp[{x,z}], sum[y]-mp[{y,z}])); } int q, k; cin >> q; while(q --> 0) { cin >> k; cout << max({ ans[belong[1][k]], ans[belong[2][k]], ans[belong[3][k]] }) << "\n"; } }
void bfs1(int s, int col) { cnt++; queue<int> q; q.push(s), vis[s] = true; while(q.size()) { int x = q.front(); q.pop(); sum[cnt] += a[x]; belong[col][x] = cnt; for(auto i : ver[x]) if(!vis[i.to] and i.col != col) q.push(i.to), vis[i.to] = true; } }
void bfs2(int s, int col) { queue<int> q; q.push(s), vis[s] = true; ll sum = 0; while(q.size()) { int x = q.front(); q.pop(); sum += a[x]; for(auto i : ver[x]) if(!vis[i.to] and i.col == col) q.push(i.to), vis[i.to] = true; } int x = belong[1][s], y = belong[2][s], z = belong[3][s]; if(col == 1) mp[{y,z}] += sum; if(col == 2) mp[{x,z}] += sum; if(col == 3) mp[{x,y}] += sum; }
|