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\n", thisans);
            lastans = thisans;
        }
    }
    return 0;
}

 

posted @ 2016-02-03 21:43  Fighting_Heart  阅读(239)  评论(0编辑  收藏  举报