• P1196 [NOI2002]银河英雄传说


    算法

    并查集+带权

    思路

    链也是树。接到后面等同于合并集。

    find

    int get(int x) {
    	if (x == fa[x]) return x;
    	int root = get(fa[x]);
    	d[x] += d[fa[x]];
    	return fa[x] = root;
    }
    

      

    修改

    int m = get(x), n = get(y);
    		if (c == 'M') {
    			fa[m] = n;
    			d[m] = s[n];
    			s[n] += s[m];
    		}
    

      

    代码

    //Author:XuHt
    #include <cmath>
    #include <iostream>
    using namespace std;
    const int N = 30001;
    int fa[N], d[N], s[N];
    
    int get(int x) {
    	if (x == fa[x]) return x;
    	int root = get(fa[x]);
    	d[x] += d[fa[x]];
    	return fa[x] = root;
    }
    
    int main() {
    	int t;
    	cin >> t;
    	for (int i = 1; i < N; i++) {
    		fa[i] = i;
    		s[i] = 1;
    		d[i] = 0;
    	}
    	while (t--) {
    		char c;
    		cin >> c;
    		int x, y;
    		cin >> x >> y;
    		int m = get(x), n = get(y);
    		if (c == 'M') {
    			fa[m] = n;
    			d[m] = s[n];
    			s[n] += s[m];
    		} else {
    			if (m == n) cout << abs(d[x] - d[y]) - 1 << endl;
    			else cout << "-1" << endl;
    		}
    	}
    	return 0;
    }
    

      

      

  • 相关阅读:
    VS Code设置同步
    ASP.NET Core 发布
    CMD命令
    通过注册表为文件(夹)添加右键菜单
    win+r 以管理员身份运行
    .NET Core部署Windows服务
    .NET Core的打包到一个exe程序
    C#枚举
    C#生成Guid
    使用Visual Studio的单元测试
  • 原文地址:https://www.cnblogs.com/ruanmowen/p/12726710.html
Copyright © 2020-2023  润新知