zoukankan      html  css  js  c++  java
  • AtCoder Beginner Contest 183 F-Confluence

    AtCoder Beginner Contest 183 F-Confluence

    Problem Statement

    (N) students are about to go to school. Student (i) belongs to Class (C_i).

    After leaving home, each student will head to school while repeatedly joining up with a group of other students. Once students join up with each other, they will not separate.

    You will be given (Q) queries that should be processed in order. There are two kinds of queries, in the following formats, that mean the following:

    • (1 a b): The group containing Student (a) and the group containing Student (b) merges. (If they are already in the same group, nothing happens.)
    • (2 x y): You are asked to find the number of students belonging to Class (y) who are already in the same group as Student (x) (including Student (x)) at the time of this query.
    Constraints
    • (1leq N leq 2 imes 10^5)
    • (1 leq Q leq 2 imes 10^5)
    • (1 leq C_i,a,b,x,y leq N)
    • In a query in the format (1 a b),(a eq b).
    • All values in input are integers.
    Input

    Input is given from Standard Input in the following format:

    (N Q)

    (C_1 ... C_N)

    (Query_1)

    (.)

    (.)

    (.)

    (Query_Q)

    Output

    Print the answers to the queries in the format (2 x y), each in its own line, in order.

    Example

    input1

    5 5
    1 2 3 2 1
    1 1 2
    1 2 5
    2 1 1
    1 3 4
    2 3 4
    

    output1

    2
    0
    
    Solution

    并查集处理学生的合并,用map记录每颗树上所包含对应班级人数。

    Code
    #include <iostream>
    #include <unordered_map>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    
    const int maxn = 211111;
    
    int fa[maxn], n, q;
    unordered_map<int, int> c[maxn];
    
    int find(int x)
    {
        if(x == fa[x]) return x;
        return fa[x] = find(fa[x]);
    }
    
    void merge(int x, int y)
    {
        int fx = find(x);
        int fy = find(y);
        if(fx == fy) return;
    
        if(c[fx].size() > c[fy].size()) {
            fa[fy] = fx;
            for(auto k : c[fy]) {
                c[fx][k.first] += k.second;
            }
        }
        else {
            fa[fx] = fy;
            for(auto k : c[fx]) {
                c[fy][k.first] += k.second;
            }
        }
        return;
    }
    
    int main()
    {
        //freopen("in.in", "r", stdin);
        ios_base::sync_with_stdio(false);
        cin.tie(0); cout.tie(0);
        cin >> n >> q;
        for(int i = 1; i <= n; ++i) {
            int x; cin >> x;
            c[i][x]++;
            fa[i] = i;
        }
        while(q--) {
            int d, a, b;
            cin >> d >> a >> b;
            if(d == 1) {
                merge(a, b);
            }
            else {
                int k = find(a);
                cout << c[k][b] << endl;
            }
        }
        return 0;
    }
    
  • 相关阅读:
    浏览器屏蔽百度搜索右侧热搜推荐脚本,收拾流氓头子
    js简单代码实现大banner轮播
    jquery回到顶部代码
    jquery实现隔行换色及移入移出效果
    利用:before和:after给段落添加效果的应用实例
    nginx配置文件应对网站攻击采集垃圾蜘蛛的方法总结
    Cygwin统计日志常用代码,欢迎各位大神补全
    原生js鼠标拖拽div左右滑动
    Day 82 VUE——基础
    Day 81 ES6
  • 原文地址:https://www.cnblogs.com/LeafLove/p/13984003.html
Copyright © 2011-2022 走看看