int g[N][N]; int n, m; int dx[] = {1, -1, 0, 0}; int dy[] = {0, 0, 1, -1}; bool st[N][N];
boolcheck(int val, int x, int y){ st[x][y] = true; bool res = false; for (int i = 0; i < 4; i ++ ) { int nx = x + dx[i], ny = y + dy[i]; if (nx < 1 || nx > n || ny < 1 || ny > m) continue; if (st[nx][ny]) continue; res |= check(val, nx, ny); } return res; }
voidsolve(){ int x, y; cin >> n >> m >> x >> y; int max_val = 0; for (int i = 1; i <= n; i ++ ) { for (int j = 1; j <= m; j ++ ) { cin >> g[i][j]; max_val = max(max_val, g[i][j]); } }
int l = 0, r = max_val; while (l < r) { int mid = l + r + 1 >> 1; if (check(mid, x, y)) { l = mid; } else { r = mid - 1; } }
cout << l << endl; }
intmain(){ std::ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); int T; cin >> T; while (T --) { solve(); } return0; }
constint N = 1e5 + 10; int n, k; vector<int> g[N]; int a[N], ans;
voiddfs(int u, int fa){ for (auto v : g[u]) { if (v == fa) continue; if (a[u] - a[v] == k || a[v] - a[u] == k) ans ++; dfs(v, u); } }
voidsolve(){ cin >> n >> k; for (int i = 1; i <= n; i ++ ) g[i].clear(); for (int i = 1; i < n; i ++ ) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); }
for (int i = 1; i <= n; i ++ ) cin >> a[i];
ans = 0; dfs(1, -1); cout << ans << endl; }
intmain(){ std::ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); int T; cin >> T; while (T --) { solve(); } return0; }
structEdge { int to, w; }; vector<Edge> g[2][N]; int dist[2][N]; bool st[N];
voiddijkstra(vector<Edge> g[], int dist[], int n, int s){ priority_queue<PII, vector<PII>, greater<PII> > q; q.push({0, s}); memset(dist, 0x3f, N * sizeof(int)); memset(st, 0, sizeof(st)); dist[s] = 0; while (q.size()) { PII t = q.top(); q.pop(); int u = t.second; if (st[u]) continue; st[u] = true; for (auto e : g[u]) { int v = e.to, w = e.w; if (dist[v] > dist[u] + w) { dist[v] = dist[u] + w; q.push({dist[v], v}); } } } }
voidsolve(){ int n, m, s, t; cin >> n >> m >> s >> t; for (int i = 1; i <= n; i ++ ) g[0][i].clear(), g[1][i].clear(); while (m --) { int a, b, c, d; cin >> a >>b >> c >> d; g[0][a].push_back({b, c}); g[0][b].push_back({a, c}); g[1][a].push_back({b, d}); g[1][b].push_back({a, d}); }
dijkstra(g[0], dist[0], n, s); dijkstra(g[1], dist[1], n, t);
int ans = 0x3f3f3f3f; for (int i = 1; i <= n; i ++ ) { ans = min(ans, max(dist[0][i], dist[1][i])); }
cout << ans << endl; }
intmain(){ std::ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); int T; // freopen("cloth.in", "r", stdin); cin >> T; while (T --) { solve(); } return0; }
voidsolve(){ int n; cin >> n; memset(d, 0, sizeof(int) * (n + 1)); for (int i = 1; i < n; i ++ ) { int a, b; cin >> a >> b; d[a] ++; d[b] ++; } longlong ans = 0; for (int i = 1; i <= n; i ++ ) { if (d[i] >= 2) ans += (LL) d[i] * (d[i] - 1) / 2; } cout << ans << endl; }
intmain(){ std::ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); int T; cin >> T; while (T --) { solve(); } return0; }