poj 1703 Find them,Catch them 并查集

http://poj.org/problem?id=1703

f[] 先清为-1  则 set_find(x]=x;

一开始: n=5;

x:  1 2 3 4 5 6 7 8 9 10

f:  1 2 3 4 5 6 7 8 9 10

  f[](包含的元素)
d:1 2后: 3{3};  4{4}; 5{5}; 6{2,6} 7{1,7} ; 8{8}; 9{9}; 10{10};

f[1]!=f[2] 在不同集合

d: 2 4 后: 3{3} ..5{5}..7{1,4,7} 8{2,6,8}; 9{9}; 10{10}

f[1]=f[4]=7 在同一集合

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<set>
#include<queue>
#include<map>
#include<vector>
using namespace std;
const int size=100010;
int f[size*2];
int set_find(int x)
{
   if(f[x]<0) return x;
   else f[x]=set_find(f[x]);
}

int  main()
{
//    freopen("in.in","r",stdin);
   int T;
   scanf("%d",&T);
   while(T--)
   {
     int n,m;
     scanf("%d%d",&n,&m);
     memset(f,-1,sizeof(f));
     char c;
     int a,b;
     while(m--)
     {
        scanf("%s%d%d",&c,&a,&b);
        int fa=set_find(a);
        int fb=set_find(b);
//        printf("fd==%d  fb=%d\n",fa,fb);
        if(c=='D'){
           if(fa!=set_find(b+n)){
               f[fa]=set_find(b+n);
               f[fb]=set_find(a+n);
//               printf("d:f[%d]==%d  f[%d]=%d\n",fa,f[fa],fb,f[fb]);
          }
        }
        else {
            if(fa==fb) printf("In the same gang.\n");
            else if(fa!=fb&&fa!=set_find(b+n)) printf("Not sure yet.\n");
            else printf("In different gangs.\n");
        }
     }
   }

    return 0;
}




阅读更多
换一批

Connect them

08-12

You have n computers numbered from 1 to n and you want to connect them to make a small local area network (LAN). All connections are two-way (that is connecting computers i and j is the same as connecting computers j and i). The cost of connecting computer i and computer j is cij. You cannot connect some pairs of computers due to some particular reasons. You want to connect them so that every computer connects to any other one directly or indirectly and you also want to pay as little as possible.nnGiven n and each cij , find the cheapest way to connect computers.nnInputnnThere are multiple test cases. The first line of input contains an integer T (T <= 100), indicating the number of test cases. Then T test cases follow.nnThe first line of each test case contains an integer n (1 < n <= 100). Then n lines follow, each of which contains n integers separated by a space. The j-th integer of the i-th line in these n lines is cij, indicating the cost of connecting computers i and j (cij = 0 means that you cannot connect them). 0 <= cij <= 60000, cij = cji, cii = 0, 1 <= i, j <= n.nnOutputnnFor each test case, if you can connect the computers together, output the method in in the following fomat:nni1 j1 i1 j1 ......nnwhere ik ik (k >= 1) are the identification numbers of the two computers to be connected. All the integers must be separated by a space and there must be no extra space at the end of the line. If there are multiple solutions, output the lexicographically smallest one (see hints for the definition of "lexicography small") If you cannot connect them, just output "-1" in the line.nnSample Inputnn2n3n0 2 3n2 0 5n3 5 0n2n0 0n0 0nnSample Outputnn1 2 1 3n-1

没有更多推荐了,返回首页