zoukankan      html  css  js  c++  java
  • quailty's Contest #1 A1 道路修建 Small

    暴力。每次合并两个点之后,把新产生的连通关系都记录下来。

    #include<cstdio>
    #include<algorithm>
    #include<vector>
    #include<cstring>
    using namespace std;
    
    int T, n, m, p, u, v, G;
    int lastans, thisans;
    int fa[1000 + 20];
    int d[1000 + 20][1000 + 20];
    vector<int>c1;
    vector<int>c2;
    
    int get(int x){
        while (fa[x] != x)
            x = fa[x] = fa[fa[x]];
        return x;
    }
    
    int main()
    {
        scanf("%d", &T);
        while (T--)
        {
            scanf("%d%d", &n, &m);
    
            G = n;
            memset(d, -1, sizeof d);
            lastans = 0;
            for (int i = 0; i <= n; i++) fa[i] = i;
    
            for (int i = 1; i <= m; i++)
            {
                scanf("%d%d%d", &p, &u, &v);
                u = (u^lastans);
                v = (v^lastans);
    
                if (p == 0)
                {
                    int fu = get(u);
                    int fv = get(v);
    
                    if (fu != fv)
                    {
                        G--;
                        c1.clear();
                        c2.clear();
    
                        for (int j = 1; j <= n; j++)
                        {
                            int fi = get(j);
                            if (fi == fu) c1.push_back(j);
                            if (fi == fv) c2.push_back(j);
                        }
                        for (int a = 0; a < c1.size(); a++)
                        for (int b = 0; b < c2.size(); b++)
                        if (d[c1[a]][c2[b]] == -1){
                            d[c1[a]][c2[b]] = i;
                            d[c2[b]][c1[a]] = i;
                        }
                        thisans = G;
                        fa[fu] = fv;
                    }
                    else
                    {
                        thisans = G;
                    }
                }
                else
                {
                    if (d[u][v] == -1) thisans = 0;
                    else thisans = d[u][v];
                }
                printf("%d
    ", thisans);
                lastans = thisans;
            }
        }
        return 0;
    }
  • 相关阅读:
    欧拉回路一个定理的证明
    NOIP2018 初赛数学第二题解析
    linux 减少Terminal路径的方法
    网络挖坑
    linux 记录
    河南游记 Day0

    NOI2018 Day 1 你的名字
    大佬的几行fastIO
    Codeforces 781B. Innokenty and a Football League
  • 原文地址:https://www.cnblogs.com/zufezzt/p/5180565.html
Copyright © 2011-2022 走看看