Codeforces 455c civilization (query set + DFS)

Source: Internet
Author: User

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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.