zoukankan      html  css  js  c++  java
  • 并查集

    刷题是遇到一题:http://www.nowcoder.com/questionTerminal/b1303e827e7f4df4a816598d008bbe72

     

    这道题要用到并查集来解(Union-Find),这位网友总结的很详细:http://blog.csdn.net/dm_vincent/article/details/7655764

    并查集主要包括两部分,find和union函数。其中find函数找根结点,union函数合并结点。根据初始化值不同,find和union函数的写法

    也略有不同。

    下面是某网友写的答案,略有瑕疵,我修改了下,加上点注释:

    int find(vector<int>& id, int i)
    {
        //只有根结点或者未连接的点为负,其他点为其根结点的索引值
        //如果不是根结点,找到根结点
        while (id[i] >= 0)i = id[i];
    
        return i;
    }
    
    void unioned(vector<int>&id, int i, int j)
    {
    
        //将根值更小的点设为根结点
        if (id[i] < id[j])
        {
            id[i] += id[j];
            id[j] = i;//直接保存根结点的索引
        }
        else
        {
            id[j] += id[i];
            id[i] = j;
        }
    }
    int main()
    {
        string str;
        int count;//输入连通集个数
        while (cin >> str)
        {
            int len = str.length();
            //全部初始化为-1.
            vector<int> id(len, -1);
            cin >> count;
            for (int i = 0; i < count; i++)
            {
                int p, q;
                cin >> p >> q;
                int pRoot = find(id, p);
                int qRoot = find(id, q);
                if (pRoot != qRoot)
                    unioned(id, pRoot, qRoot);//注意:是根结点相连接
            }
            vector<vector<int>> mat(len);
            for (int i = 0; i < id.size(); i++)
            {
                if (id[i] == -1)continue;
    
                int root = find(id, i);
                mat[root].push_back(i);//保存各个连通集的位置索引(按根结点)
            }
            //
            for (int i = 0; i < mat.size(); i++)
            {
                vector<char> chs;//保存连通集上的字符
                //如果不是根结点,则直接跳过
                for (int j = 0; j < mat[i].size(); j++)
                {
                    chs.push_back(str[mat[i][j]]);
                }
                sort(chs.begin(), chs.end());//将同个连通集上的字符排序
                //写回字符串中
                for (int j = 0; j < mat[i].size(); j++)
                    str[mat[i][j]] = chs[j];
            }
            cout << str;
        }
    }
    View Code
  • 相关阅读:
    Jquery 下实现 图片大图预览效果
    PHP之图片上传类(加了缩略图)
    无限极分类
    inno steup 安装判断 进程是否运行
    mac os 10.15安装jdk 1.6
    c# 创建delphi的代码
    php 断点续传以及100% 后台zip解压
    多个编号重复,递归处理
    php映射echarts柱状图
    php数据映射到echarts中国地图
  • 原文地址:https://www.cnblogs.com/573177885qq/p/5845576.html
Copyright © 2011-2022 走看看