structEdge { int to, nxt; inlineEdge(){} inline Edge(int to, int nxt) :to(to), nxt(nxt) {} }edge[maxn << 1];
int head[maxn], tot;
voidInit() { tot = 0; memset(head, -1, sizeof head); }
voidaddedge(int u, int v) { edge[tot] = Edge(v, head[u]); head[u] = tot++; edge[tot] = Edge(u, head[v]); head[v] = tot++; }
int fa[maxn][DEG]; int deg[maxn];
voidBFS(int root) { queue<int>q; deg[root] = 0; fa[root][0] = root; q.push(root); while (!q.empty()) { int tmp = q.front(); q.pop(); for (int i = 1; i < DEG; ++i) { fa[tmp][i] = fa[fa[tmp][i - 1]][i - 1]; } for (int i = head[tmp]; i != -1; i = edge[i].nxt) { int v = edge[i].to; if (v == fa[tmp][0]) continue; deg[v] = deg[tmp] + 1; fa[v][0] = tmp; q.push(v); } } }
intLCA(int u, int v) { if (deg[u] > deg[v]) swap(u, v); int hu = deg[u]; int hv = deg[v]; int tu = u, tv = v; for (int det = hv - hu, i = 0; det; det >>= 1, ++i) { if (det & 1) { tv = fa[tv][i]; } } if (tu == tv) return tu; for (int i = DEG - 1; i >= 0; --i) { if (fa[tu][i] == fa[tv][i]) continue; tu = fa[tu][i]; tv = fa[tv][i]; } return fa[tu][0]; }
int n; bool flag[maxn];
voidRUN() { int t; scanf("%d", &t); while (t--) { scanf("%d", &n); Init(); memset(flag, false, sizeof flag); for (int i = 1; i < n; ++i) { int u, v; ll w; scanf("%d %d", &u, &v); addedge(u, v); flag[v] = true; } int root = 0; for (int i = 1; i <= n; ++i) { if (!flag[i]) { root = i; break; } } BFS(root); int u, v; scanf("%d %d", &u, &v); printf("%d\n", LCA(u, v)); } }
structEdge { int to, nxt; Edge() {} Edge(int to, int nxt) :to(to), nxt(nxt) {} }edge[maxn << 1];
int head[maxn], tot;
int dis[maxn];
voidaddedge(int u, int v) { edge[tot] = Edge(v, head[u]); head[u] = tot++; }
voidInit() { tot = 0; memset(head, -1, sizeof head); }
int fa[maxn][DEG]; int deg[maxn];
voidBFS(int root) { queue<int>q; deg[root] = 0; fa[root][0] = root; q.push(root); while (!q.empty()) { int tmp = q.front(); q.pop(); for (int i = 1; i < DEG; ++i) { fa[tmp][i] = fa[fa[tmp][i - 1]][i - 1]; } for (int i = head[tmp]; i != -1; i = edge[i].nxt) { int v = edge[i].to; if (v == fa[tmp][0]) continue; deg[v] = deg[tmp] + 1; fa[v][0] = tmp; q.push(v); } } }
intLCA(int u, int v) { if (deg[u] > deg[v]) swap(u, v); int hu = deg[u]; int hv = deg[v]; int tu = u, tv = v; for (int det = hv - hu, i = 0; det; det >>= 1, ++i) { if (det & 1) { tv = fa[tv][i]; } } if (tu == tv) return tu; for (int i = DEG - 1; i >= 0; --i) { if (fa[tu][i] == fa[tv][i]) continue; tu = fa[tu][i]; tv = fa[tv][i]; } return fa[tu][0]; }
int n, m; char str[maxn]; bool flag[maxn]; int arr[maxn];
voidRUN() { while (~scanf("%d", &n)) { Init(); memset(flag, false, sizeof flag); memset(arr, 0, sizeof arr); for (int i = 1; i <= n; ++i) { int id, m; scanf("%d:(%d)", &id, &m); for (int i = 0; i < m; ++i) { int id2; scanf("%d", &id2); addedge(id, id2); flag[id2] = true; } } int root = 0; for (int i = 1; i <= n; ++i) { if (!flag[i]) { root = i; break; } } BFS(root); int q; scanf("%d", &q); while (q--) { int u, v; scanf(" (%d %d)", &u, &v); int root = LCA(u, v); arr[root]++; } for (int i = 1; i <= n; ++i) { if (arr[i]) { printf("%d:%d\n", i, arr[i]); } } } }
voidBFS(int root) { queue<int>q; deg[root] = 0; fa[root][0] = root; q.push(root); dis[root] = 0; while (!q.empty()) { int tmp = q.front(); q.pop(); for (int i = 1; i < DEG; ++i) { fa[tmp][i] = fa[fa[tmp][i - 1]][i - 1]; } for (int i = head[tmp]; i != -1; i = edge[i].next) { int v = edge[i].to; if (v == fa[tmp][0]) continue; dis[v] = dis[tmp] + edge[i].w; deg[v] = deg[tmp] + 1; fa[v][0] = tmp; q.push(v); } } }
intLCA(int u, int v) { if (deg[u] > deg[v]) swap(u, v); int hu = deg[u], hv = deg[v]; int tu = u; int tv = v; for (int det = hv - hu, i = 0; det; det >>= 1, ++i) { if (det & 1) { tv = fa[tv][i]; } } if (tu == tv) return tu; for (int i = DEG - 1; i >= 0; --i) { if (fa[tu][i] == fa[tv][i]) continue; tu = fa[tu][i]; tv = fa[tv][i]; } return fa[tu][0]; }
voidRUN() { int t; scanf("%d", &t); while (t--) { init(); int n, q; scanf("%d %d", &n, &q); for (int i = 1; i < n; ++i) { int u, v, w; scanf("%d %d %d", &u, &v, &w); addedge(u, v, w); addedge(v, u, w); } BFS(1); while (q--) { int u, v; scanf("%d %d", &u, &v); int root = LCA(u, v); int ans = dis[u] + dis[v] - 2 * dis[root]; printf("%d\n", ans); } } }
voidmix(int x, int y) { x = find(x); y = find(y); if (x != y) { Fa[x] = y; } }
structEdge { int to; int next; int w; Edge() {} Edge(int to, int next, int w) :to(to), next(next), w(w) {} }edge[maxn << 1];
int head[maxn]; int tot;
voidaddedge(int u, int v, int w) { edge[tot] = Edge(v, head[u], w); head[u] = tot++; }
voidinit() { tot = 0; memset(head, -1, sizeof head); memset(dis, 0, sizeof dis); for (int i = 1; i <= n; ++i) { Fa[i] = i; } }
int fa[maxn][DEG]; int deg[maxn];
voidBFS() { queue<int>q; for (int i = 1; i <= n; ++i) { if (find(i) == i) { deg[i] = 0; fa[i][0] = i; dis[i] = 0; q.push(i); } } while (!q.empty()) { int tmp = q.front(); q.pop(); for (int i = 1; i < DEG; ++i) { fa[tmp][i] = fa[fa[tmp][i - 1]][i - 1]; } for (int i = head[tmp]; i != -1; i = edge[i].next) { int v = edge[i].to; if (v == fa[tmp][0]) continue; dis[v] = dis[tmp] + edge[i].w; deg[v] = deg[tmp] + 1; fa[v][0] = tmp; q.push(v); } } }
intLCA(int u, int v) { if (deg[u] > deg[v]) swap(u, v); int hu = deg[u], hv = deg[v]; int tu = u; int tv = v; for (int det = hv - hu, i = 0; det; det >>= 1, ++i) { if (det & 1) { tv = fa[tv][i]; } } if (tu == tv) return tu; for (int i = DEG - 1; i >= 0; --i) { if (fa[tu][i] == fa[tv][i]) continue; tu = fa[tu][i]; tv = fa[tv][i]; } return fa[tu][0]; }
voidsolve() { int u, v; scanf("%d %d", &u, &v); int rootu = find(u); int rootv = find(v); if (rootu != rootv) { puts("Not connected"); return; } int root = LCA(u, v); int ans = dis[u] + dis[v] - 2 * dis[root]; printf("%d\n", ans); }
voidRUN() { while (~scanf("%d %d %d", &n, &m, &q)) { init(); for (int i = 0; i < m; ++i) { int u, v, w; scanf("%d %d %d", &u, &v, &w); addedge(u, v, w); addedge(v, u, w); mix(u, v); } BFS(); while (q--) { solve(); } } }
structEdge { int to, nxt; Edge() {} Edge(int to, int nxt) :to(to), nxt(nxt) {} }edge[maxn << 1];
int head[maxn], tot;
int dis[maxn];
voidaddedge(int u, int v) { edge[tot] = Edge(v, head[u]); head[u] = tot++; }
voidinit() { memset(head, -1, sizeof head); tot = 0; }
int fa[maxn][DEG]; int deg[maxn];
voidBFS(int root) { queue<int>q; deg[root] = 0; fa[root][0] = root; q.push(root); while (!q.empty()) { int tmp = q.front(); q.pop(); for (int i = 1; i < DEG; ++i) { fa[tmp][i] = fa[fa[tmp][i - 1]][i - 1]; } for (int i = head[tmp]; i != -1; i = edge[i].nxt) { int v = edge[i].to; if (v == fa[tmp][0]) continue; deg[v] = deg[tmp] + 1; fa[v][0] = tmp; q.push(v); } } }
intLCA(int u, int v) { if (deg[u] > deg[v]) swap(u, v); int hu = deg[u]; int hv = deg[v]; int tu = u, tv = v; for (int det = hv - hu, i = 0; det; det >>= 1, ++i) { if (det & 1) { tv = fa[tv][i]; } } if (tu == tv) return tu; for (int i = DEG - 1; i >= 0; --i) { if (fa[tu][i] == fa[tv][i]) continue; tu = fa[tu][i]; tv = fa[tv][i]; } return fa[tu][0]; }
int n, m; ll arr[maxn];
voidDFS(int u, int pre) { for (int i = head[u]; ~i; i = edge[i].nxt) { int v = edge[i].to; if (v == pre) continue; DFS(v, u); arr[u] += arr[v]; } }
voidRUN() { while (~scanf("%d %d", &n, &m)) { init(); memset(arr, 0, sizeof arr); for (int i = 1; i < n; ++i) { int u, v; scanf("%d %d", &u, &v); addedge(u, v); addedge(v, u); } BFS(1); for (int i = 1; i <= m; ++i) { int u, v; scanf("%d %d", &u, &v); arr[u]++; arr[v]++; arr[LCA(u, v)] -= 2; } DFS(1, -1); ll ans = 0; for (int i = 2; i <= n; ++i) { if (!arr[i]) { ans += m; } elseif (arr[i] == 1) { ans++; } } printf("%lld\n", ans); } }
voidmix(int x, int y){ x = find(x); y = find(y); if (x != y) { father[x] = y; } }
boolsame(int x, int y){ return find(x) == find(y); }
ll MST(){ mp.clear();
ll res = 0;
sort(G.begin(), G.end()); memset(inMST,false, sizeof inMST); for (auto it: G) { int u = it.u; int v = it.v; mp[make_pair(v, u)] =cnt; mp[make_pair(u, v)] = cnt++; } for (auto it: G) { int u = it.u; int v = it.v; if (same(u, v)) continue; mix(u, v); inMST[mp[make_pair(u, v)]] = true; addedge(u, v, it.w); res += it.w; } return res; }
int fa[maxn][DEG]; int deg[maxn];
voidBFS(int root){ queue<int> q; deg[root] = 0; fa[root][0] =root; q.push(root); while (!q.empty()) { int tmp = q.front(); q.pop(); for (int i = 1;i < DEG;++i) { dis[tmp][i] =max(dis[tmp][i - 1], dis[fa[tmp][i - 1]][i - 1]); fa[tmp][i] = fa[fa[tmp][i - 1]][i - 1]; } for (int i = head[tmp];~i;i = edge[i].nxt) { int v = edge[i].to; if (v == fa[tmp][0]) continue; deg[v] = deg[tmp] + 1; fa[v][0] =tmp; int id = mp[make_pair(tmp, v)]; dis[v][0] = G[id].w; q.push(v); } } }
intLCA(int u, int v){ int res = 0; if (deg[u] > deg[v])swap(u, v); int hu = deg[u], hv = deg[v]; int tu = u, tv = v; for (int det = hv - hu, i = 0;det;det >>= 1, ++i) { if (det & 1) { res = max(res, dis[tv][i]); tv = fa[tv][i]; } } if (tu == tv)return res; for (int i = DEG - 1;i >= 0; --i) { if (fa[tu][i] == fa[tv][i]) continue; res = max(res, max(dis[tu][i], dis[tv][i])); tu = fa[tu][i]; tv = fa[tv][i]; } res = max(res, max(dis[tu][0], dis[tv][0])); return res; }
voidRUN(){ while (~scanf("%d %d", &n, &m)) { Init(); for (int i = 1;i <=m;++i) { int u, v; ll w; scanf("%d %d %lld", &u, &v, &w); G.push_back(node(u, v, w)); } ll ans = MST(); BFS(1); int q; scanf("%d", &q); while (q--) { int u, v; scanf("%d %d", &u, &v); int id = mp[make_pair(u, v)]; ll w = G[id].w; if (inMST[id]) { printf("%lld\n", ans); } else { ll res = LCA(u, v); printf("%lld\n", ans + w - res); } } } }
structEdge { int to, nxt; inlineEdge(){} inline Edge(int to, int nxt) :to(to), nxt(nxt) {} }edge[maxn << 1];
int head[maxn], tot;
map<string, int>mp; int dis[maxn];
inlinevoidaddedge(int u, int v) { edge[tot] = Edge(v, head[u]); head[u] = tot++; }
inlinevoidinit() { memset(head, -1, sizeof head); tot = 0; }
int fa[maxn][DEG]; int deg[maxn];
inlinevoidBFS(int root) { queue<int>q; deg[root] = 0; dis[root] = 0; fa[root][0] = root; q.push(root); while (!q.empty()) { int tmp = q.front(); q.pop(); for (int i = 1; i < DEG; ++i) { fa[tmp][i] = fa[fa[tmp][i - 1]][i - 1]; } for (int i = head[tmp]; i != -1; i = edge[i].nxt) { int v = edge[i].to; if (v == fa[tmp][0]) continue; deg[v] = deg[tmp] + 1; dis[v] = dis[tmp] + 1; fa[v][0] = tmp; q.push(v); } } }
inlineintLCA(int u, int v) { if (deg[u] > deg[v]) swap(u, v); int hu = deg[u]; int hv = deg[v]; int tu = u, tv = v; for (int det = hv - hu, i = 0; det; det >>= 1, ++i) { if (det & 1) { tv = fa[tv][i]; } } if (tu == tv) return tu; for (int i = DEG - 1; i >= 0; --i) { if (fa[tu][i] == fa[tv][i]) continue; tu = fa[tu][i]; tv = fa[tv][i]; } return fa[tu][0]; }
int n, m; bool flag[maxn];
inlinevoidRUN() { int t; cin >> t; while (t--) { cin >> n >> m; init(); mp.clear(); int cnt = 1; memset(flag, false, sizeof flag); for (int i = 1; i < n; ++i) { string s1, s2; cin >> s1 >> s2; if (mp[s1] == 0) { mp[s1] = cnt++; } if (mp[s2] == 0) { mp[s2] = cnt++; } addedge(mp[s2], mp[s1]); addedge(mp[s1], mp[s2]); flag[mp[s1]] = true; } int root = 0; for (int i = 1; i < cnt; ++i) { if (!flag[i]) { root = i; break; } } BFS(root); while (m--) { string s1, s2; cin >> s1 >> s2; int u = mp[s1]; int v = mp[s2]; int tmp = LCA(u, v); int ans = dis[u] - dis[tmp]; if (tmp != v) { ans++; } cout << ans << endl; } } }
structEdge { int to, nxt; ll w; Edge() {} Edge(int to, int nxt, ll w) :to(to), nxt(nxt), w(w) {} }edge[maxn << 1];
int head[maxn], tot; int red[maxn];
voidaddedge(int u, int v, ll w) { edge[tot] = Edge(v, head[u], w); head[u] = tot++; }
voidInit(int n) { for (int i = 1; i <= n; ++i) { red[i] = 0; head[i] = -1; } tot = 0; }
ll dis[maxn]; int fa[maxn][DEG]; int deg[maxn]; int pre[maxn];
voidBFS(int root) { queue<int>q; dis[root] = 0; deg[root] = 0; fa[root][0] = root; pre[root] = root; q.push(root); while (!q.empty()) { int tmp = q.front(); q.pop(); for (int i = 1; i < DEG; ++i) { fa[tmp][i] = fa[fa[tmp][i - 1]][i - 1]; } for (int i = head[tmp]; ~i; i = edge[i].nxt) { int v = edge[i].to; ll w = edge[i].w; if (v == fa[tmp][0]) continue; deg[v] = deg[tmp] + 1; fa[v][0] = tmp; if (red[v]) { pre[v] = v; } else { pre[v] = pre[tmp]; } if (red[v]) { dis[v] = 0; } else { dis[v] = dis[tmp] + w; } q.push(v); } } }
intLCA(int u, int v) { if (deg[u] > deg[v]) swap(u, v); int hu = deg[u], hv = deg[v]; int tu = u, tv = v; for (int det = hv - hu, i = 0; det; det >>= 1, ++i) { if (det & 1) { tv = fa[tv][i]; } } if (tu == tv) return tu; for (int i = DEG - 1; i >= 0; --i) { if (fa[tu][i] == fa[tv][i]) continue; tu = fa[tu][i], tv = fa[tv][i]; } return fa[tu][0]; }
int n, m, q, k; int arr[maxn];
boolcmp(int a, int b) { return dis[a] > dis[b]; }
boolcheck(ll mid) { int root = arr[1]; int cnt = 0; for (int i = 1; i <= k; ++i) { if (dis[arr[i]] > mid) { if (pre[root] != pre[arr[i]]) returnfalse; root = LCA(root, arr[i]); cnt++; } } if (cnt == 1 || cnt == 0) returntrue; for (int i = 1; i <= k; ++i) { if (dis[arr[i]] > mid) { if (dis[arr[i]] - dis[root] > mid) returnfalse; } } returntrue; }
voidRUN() { int t; scanf("%d", &t); while (t--) { scanf("%d %d %d", &n, &m, &q); Init(n); for (int i = 1; i <= m; ++i) { int u; scanf("%d", &u); red[u] = 1; } for (int i = 1; i < n; ++i) { int u, v; ll w; scanf("%d %d %lld", &u, &v, &w); addedge(u, v, w); addedge(v, u, w); } BFS(1); while (q--) { scanf("%d", &k); for (int i = 1; i <= k; ++i) { scanf("%d", arr + i); } if (k == 1) { puts("0"); continue; } sort(arr + 1, arr + 1 + k, cmp); ll l = 0; ll r = INFLL; ll ans = 0; while (r - l >= 0) { ll mid = (r + l) >> 1; if (check(mid)) { r = mid - 1; ans = mid; } else { l = mid + 1; } } printf("%lld\n", ans); } } }