zoukankan      html  css  js  c++  java
  • Codeforces 455C

    题目链接

    C. Civilization
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Andrew plays a game called "Civilization". Dima helps him.

    The game has n cities and m bidirectional roads. The cities are numbered from 1 to n. Between any pair of cities there either is a single (unique) path, or there is no path at all. A path is such a sequence of distinct cities v1, v2, ..., vk, that there is a road between any contiguous cities vi and vi + 1 (1 ≤ i < k). The length of the described path equals to (k - 1). We assume that two cities lie in the same region if and only if, there is a path connecting these two cities.

    During the game events of two types take place:

    1. Andrew asks Dima about the length of the longest path in the region where city x lies.
    2. Andrew asks Dima to merge the region where city x lies with the region where city y lies. If the cities lie in the same region, then no merging is needed. Otherwise, you need to merge the regions as follows: choose a city from the first region, a city from the second region and connect them by a road so as to minimize the length of the longest path in the resulting region. If there are multiple ways to do so, you are allowed to choose any of them.

    Dima finds it hard to execute Andrew's queries, so he asks you to help him. Help Dima.

    Input

    The first line contains three integers nmq (1 ≤ n ≤ 3·105; 0 ≤ m < n1 ≤ q ≤ 3·105) — the number of cities, the number of the roads we already have and the number of queries, correspondingly.

    Each of the following m lines contains two integers, ai and bi (ai ≠ bi1 ≤ ai, bi ≤ n). These numbers represent the road between cities ai and bi. There can be at most one road between two cities.

    Each of the following q lines contains one of the two events in the following format:

    • xi. It is the request Andrew gives to Dima to find the length of the maximum path in the region that contains city xi (1 ≤ xi ≤ n).
    • xi yi. It is the request Andrew gives to Dima to merge the region that contains city xi and the region that contains city yi (1 ≤ xi, yi ≤ n). Note, that xi can be equal to yi.
    Output

    For each event of the first type print the answer on a separate line.

    Sample test(s)
    input
    6 0 6
    2 1 2
    2 3 4
    2 5 6
    2 3 2
    2 5 3
    1 1
    output
    4
    思路:先计算出各个连通分量的最长的路径(两次dfs), 合并的时候,按秩合并,同时更新最长路。
    Accepted Code:
     1 /*************************************************************************
     2     > File Name: E.cpp
     3     > Author: Stomach_ache
     4     > Mail: sudaweitong@gmail.com
     5     > Created Time: 2014年08月09日 星期六 08时16分23秒
     6     > Propose: 
     7  ************************************************************************/
     8 
     9 #include <cmath>
    10 #include <string>
    11 #include <vector>
    12 #include <cstdio>
    13 #include <fstream>
    14 #include <cstring>
    15 #include <iostream>
    16 #include <algorithm>
    17 using namespace std;
    18 
    19 const int maxn = 300002;
    20 int n, m, q;
    21 int d[maxn], p[maxn], rank[maxn];
    22 int x, w, best;
    23 vector<int> g[maxn];
    24 
    25 int getFa(int x) {
    26       return x != p[x] ? p[x] = getFa(p[x]) : x;
    27 }
    28 
    29 void dfs(int u, int fa, int high) {
    30     p[u] = x;
    31     if (high > best) best = high, w = u;
    32     for (int i = 0; i < (int)g[u].size(); i++) if (g[u][i] != fa) 
    33           dfs(g[u][i], u, high + 1);
    34 }
    35 
    36 void unite(int x, int y) {
    37     if (x == y) return ;
    38     if (rank[x] >= rank[y]) {
    39           p[y] = x;
    40         d[x] = max(max(d[x], d[y]), (d[x]+1)/2+(d[y]+1)/2+1);
    41         if (rank[x] == rank[y]) rank[x]++;
    42     } else {
    43           p[x] = y;
    44         d[y] = max(max(d[x], d[y]), (d[x]+1)/2+(d[y]+1)/2+1);
    45     } 
    46 }
    47 
    48 int main(void) {
    49       scanf("%d %d %d", &n, &m, &q);
    50     for (int i = 0; i < m; i++) {
    51           int from, to;
    52         scanf("%d %d", &from, &to);
    53         g[from].push_back(to);
    54         g[to].push_back(from);
    55     }
    56     for (x = 1; x <= n; x++) if (!p[x]) {
    57           best = -1; dfs(x, 0, 0);
    58         best = -1, dfs(w, 0, 0);
    59         d[x] = best;
    60     }
    61     while (q--) {
    62           int t;
    63         scanf("%d %d", &t, &x);
    64         if (t == 1) {
    65             printf("%d
    ", d[getFa(x)]);
    66         } else {
    67               int y;
    68             scanf("%d", &y);
    69             unite(getFa(x), getFa(y));
    70         }
    71     }
    72     return 0;
    73 }
     
    
    
    
    
    
    
    
    
    
    
  • 相关阅读:
    Xamarin.ios UIView Frame vs. Bounds (转载)
    Xamarin.iOS常用控件总结
    Xamarin.iOS学习:使用Object Sharpie自动绑定Objective-C库
    Xamarin.iOS学习一:Hello.iOS 入门学习
    Entity Framework学习六:一些常用查询技术
    Entity Framework学习五:一个实体类映射到多个表
    Entity Framework学习四:查询和建模进阶
    Entity Framework学习三:查询、插入、更新和删除操作
    使用Code First模式开发如何更新数据库(转载)
    让Entity Framework不再私闯sys.databases 不自动创建数据库(转载)
  • 原文地址:https://www.cnblogs.com/Stomach-ache/p/3900808.html
Copyright © 2011-2022 走看看