vector<vector<int>> a(n, vector<int>(n)); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> a[i][j]; } }
ll sum = 0; for (int k = -n + 1; k < n; k++) { int mx = 0; for (int i = max(-k, 0); i + k < n && i < n; i++) { if (a[i][i + k] < 0) { mx = max(mx, -a[i][i + k]); } } sum += mx; } cout << sum << "\n"; }
map<ll, int> mp; mp[0] = 1; ll sum = 0, ans = 0; for (int i = 0; i < n; i++) { int x; cin >> x; sum += x; if (mp.count(sum)) { ans++; mp.clear(); } mp[sum]++; }
vector<int> p(n); for (int i = 0; i < n; i++) { cin >> p[i]; p[i]--; }
vector<vector<int>> cycs; vector<int> vis(n); for (int i = 0; i < n; i++) { if (vis[i]) continue; int u = i; vector<int> cyc; do { cyc.push_back(u); vis[u] = true; u = p[u]; } while (u != i); cycs.push_back(cyc); }
int res = 0; for (auto& cyc : cycs) { res += (cyc.size() - 1) / 2; } cout << res << "\n"; }
vector<vector<int>> E(n); for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; u--, v--; E[u].push_back(v); E[v].push_back(u); }
vector p(n, vector<int>(20, -1)); vector<int> dep(n); vector<int> dis(n), dis2(n); // 到叶子的最长距离和次长距离 auto dfs1 = [&](thisauto&& self, int u) -> void { for (auto v : E[u]) { if (v == p[u][0]) continue; dep[v] = dep[u] + 1; p[v][0] = u; for (int bit = 1; bit <= __lg(dep[v]); bit++) { p[v][bit] = p[p[v][bit - 1]][bit - 1]; } self(v); int tmp = dis[v] + 1; if (tmp >= dis[u]) { dis2[u] = dis[u]; dis[u] = tmp; } elseif (dis[v] + 1 >= dis2[u]) { dis2[u] = tmp; } } }; dfs1(0); vector<array<int, 20>> dp(n); auto dfs2 = [&](thisauto&& self, int u) -> void { for (auto v : E[u]) { if (v == p[u][0]) continue; dp[v][0] = ((dis[u] == dis[v] + 1) ? dis2[u] : dis[u]) + 1; for (int bit = 1; bit <= __lg(dep[v]); bit++) { dp[v][bit] = max(dp[v][bit - 1], dp[p[v][bit - 1]][bit - 1] + (1 << bit - 1)); } self(v); } }; dp[0][0] = dis[0]; dfs2(0);
int q; cin >> q;
while (q--) { int u, x; cin >> u >> x; u--; x = min(x, dep[u]);
int res = dis[u], sum = 0; for (int bit = 19; bit >= 0; bit--) { if ((1 << bit) <= x) { res = max(res, dp[u][bit] + sum); u = p[u][bit]; x -= (1 << bit); sum += (1 << bit); } }