Codeforces 455C Civilization (query set + dfs), codeforces455c
Link: Codeforces 455C Civilization
Given N, M, Q, N indicates that there are N cities and M repaired roads. The repaired roads cannot be changed, and then Q operations are performed, there are two types of operations: one is to query the connection set where city x is located, and the longest path is multi-length. The second is to connect two Unicom sets and adopt the shortest path after Unicom.
Solution: Because the diagram at the beginning cannot be changed, the dfs is used to process each Unicom set at the beginning, and the maximum value is recorded. Then, Q operations are performed, use and query set maintenance. Note that the longest path is the shortest solution when China Unicom is used, so the transfer equation of s is changed to s = max (s, (s + 1) /2 + (s0 + 1)/2 + 1)
#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace std;const int maxn = 3 * 1e5 + 5;int N, M, Q, f[maxn], s[maxn];int root, ans, rec;vector<int> g[maxn];int getfar(int x) { return f[x] == x ? x : f[x] = getfar(f[x]);}void link (int u, int v) { int x = getfar(u); int y = getfar(v); if (x == y) return; if (s[x] < s[y]) swap(s[x], s[y]); f[y] = x; s[x] = max(s[x], (s[x] + 1) / 2 + (s[y] + 1) / 2 + 1);}void dfs (int u, int p, int d) { f[u] = root; if (d > ans) { ans = d; rec = u; } for (int i = 0; i < g[u].size(); i++) { if (g[u][i] != p) dfs(g[u][i], u, d+1); }}int main () { int type, u, v; scanf("%d%d%d", &N, &M, &Q); for (int i = 1; i <= N; i++) { f[i] = i; g[i].clear(); } for (int i = 0; i < M; i++) { scanf("%d%d", &u, &v); g[u].push_back(v); g[v].push_back(u); } for (int i = 1; i <= N; i++) { if (f[i] == i) { root = rec = i; ans = -1; dfs(i, 0, 0); ans = -1; dfs(rec, 0, 0); s[i] = ans; } } for (int i = 0; i < Q; i++) { scanf("%d", &type); if (type == 1) { scanf("%d", &u); v = getfar(u); printf("%d\n", s[v]); } else { scanf("%d%d", &u, &v); link(u, v); } } return 0;}