CF 260div2 E

游戏文明策略模拟
E. 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·1050 ≤ 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 ≠ bi; 1 ≤ ai, bi ≤ n). These numbers represent the road between citiesai 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:

  • 1 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).
  • 2 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
    

    用半个小时A完了abc,一个半小时写这题都没写出来,真是拙计。。先是想找重心,然后维护每棵子树的半径,直径,和重心的位置。不会树的重心呀。。。套模板改来改去比赛就这么tm结束了。。。赛后有人说只用维护树的直径,仔细一想,是的呀!我真是sb了。注意:两棵子树合并后的树的直径,不能只考虑用两棵子树的半径相加,有可能合并后还是原来某棵子树的直径。
    #include<cstdio>
    #include<map>
    #include<queue>
    #include<cstring>
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<vector>
    #include<cmath>
    using namespace std;
    const int maxn = 300000 + 5;
    const int INF = 1000000000;
    typedef long long LL;
    typedef pair<int, int> P;
    #define fi first
    #define se second
    
    vector<int> G[maxn];
    int n;
    int fa[maxn], diameter[maxn], vis[maxn];
    int Find(int x){return fa[x]==x?x:fa[x]=Find(fa[x]);}
    
    struct Node{
        int x, from, cnt;
        Node(int x, int from, int cnt){
            this -> x = x;
            this -> from = from;
            this -> cnt = cnt;
        }
    };
    queue<Node> q;
    
    P bfs(int x){
        while(!q.empty())
            q.pop();
        q.push(Node(x, -1, 0));
        int pos, from, cnt;
        while(!q.empty()){
            Node tem = q.front();
            q.pop();
            pos = tem.x;
            from = tem.from;
            cnt = tem.cnt;
            vis[pos] = 1;
            for(int i = 0;i < G[pos].size();i++){
                int to = G[pos][i];
                if(to != from){
                    q.push(Node(to, pos, cnt+1));
                }
            }
        }
    
        return P(cnt, pos);
    }
    
    void pre(){
        memset(vis, 0, sizeof vis);
        for(int i = 1;i <= n;i++){
            if(vis[i] == 0){
                int tem = bfs(i).se;
                int X = Find(i);
                diameter[X] = bfs(tem).fi;
            }
        }
    }
    
    int query(int x){
        return diameter[Find(x)];
    }
    
    void join(int x, int y){
        int X = Find(x);
        int Y = Find(y);
        if(X != Y){
            int radx = (diameter[X]+1)/2;
            int rady = (diameter[Y]+1)/2;
            fa[X] = Y;
            diameter[Y] = max(radx+rady+1, max(diameter[X],diameter[Y]));
        }
    }
    
    int main(){
        int m, q;
        while(scanf("%d%d%d", &n, &m, &q) != EOF){
            for(int i = 0;i < maxn;i++){
                G[i].clear();
                fa[i] = i;
            }
            while(m--){
                int x, y;
                scanf("%d%d", &x, &y);
                G[x].push_back(y);
                G[y].push_back(x);
                int X = Find(x);
                int Y = Find(y);
                fa[X] = Y;
            }
    
            pre();
    
            while(q--){
                int kind;
                scanf("%d", &kind);
                if(kind == 1){
                    int x;
                    scanf("%d", &x);
                    cout << query(x) << endl;
                }
                else{
                    int x, y;
                    scanf("%d%d", &x, &y);
                    join(x, y);
                }
            }
        }
        return 0;
    }
    


    基于数据驱动的 Koopman 算子的递归神经网络模型线性化,用于纳米定位系统的预测控制研究(Matlab代码实现)内容概要:本文围绕“基于数据驱动的Koopman算子的递归神经网络模型线性化”展开,旨在研究纳米定位系统的预测控制方法。通过结合数据驱动技术与Koopman算子理论,将非线性系统动态近似为高维线性系统,进而利用递归神经网络(RNN)建模并实现系统行为的精确预测。文中详细阐述了模型构建流程、线性化策略及在预测控制中的集成应用,并提供了完整的Matlab代码实现,便于科研人员复现实验、优化算法并拓展至其他精密控制系统。该方法有效提升了纳米级定位系统的控制精度与动态响应性能。; 适合人群:具备自动控制、机器学习或信号处理背景,熟悉Matlab编程,从事精密仪器控制、智能制造或先进控制算法研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①实现非线性动态系统的数据驱动线性化建模;②提升纳米定位平台的轨迹跟踪与预测控制性能;③为高精度控制系统提供可复现的Koopman-RNN融合解决方案; 阅读建议:建议结合Matlab代码逐段理解算法实现细节,重点关注Koopman观测矩阵构造、RNN训练流程与模型预测控制器(MPC)的集成方式,鼓励在实际硬件平台上验证并调整参数以适应具体应用场景。
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值