树(4):并查集,堆,哈夫曼树

并查集

1107 Social Clusters (30 分)
 

When register on a social network, you are always asked to specify your hobbies in order to find some potential friends with the same hobbies. A social cluster is a set of people who have some of their hobbies in common. You are supposed to find all the clusters.

Input Specification:

Each input file contains one test case. For each test case, the first line contains a positive integer N (≤), the total number of people in a social network. Hence the people are numbered from 1 to N. Then N lines follow, each gives the hobby list of a person in the format:

Ki​​: hi​​[1] hi​​[2] ... hi​​[Ki​​]

where Ki​​ (>) is the number of hobbies, and [ is the index of the j-th hobby, which is an integer in [1, 1000].

Output Specification:

For each case, print in one line the total number of clusters in the network. Then in the second line, print the numbers of people in the clusters in non-increasing order. The numbers must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:

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

Sample Output:

3
4 3 1


#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int MAXN=1010;
int n,k,temp;
int father[MAXN];
int isroot[MAXN]={0};
int course[MAXN]={0};

int findfather(int x)
{
    int a=x;
    while(x!=father[x])
    {
        x=father[x];
    }
    while(a!=father[a])
    {
        int z=a;
        a=father[a];
        father[z]=x;
    }
    return x;
} 

void unit(int a,int b)
{
    int fa=findfather(a);
    int fb=findfather(b);
    if(fa!=fb) father[fa]=fb;
}

void init(int n)
{
    for(int i=1;i<=n;i++)
    {
        father[i]=i;
        isroot[i]=false;
    }
}
bool cmp(int a,int b)
{
    return a>b;
}
int main()
{
    scanf("%d",&n);
    init(n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d: ",&k);
        for(int j=0;j<k;j++)
        {
            scanf("%d",&temp);
            if(course[temp]==0) course[temp]=i;
            unit(i,findfather(course[temp]));
        }
    }
    
    for(int i=1;i<=n;i++)
    {
        isroot[findfather(i)]++;
    }
    int ans=0;
    for(int i=1;i<=n;i++)
    {
        if(isroot[i]!=0) ans++;
    }
    printf("%d\n",ans);
    sort(isroot+1,isroot+n+1,cmp);
    for(int i=1;i<=ans;i++)
    {
        printf("%d",isroot[i]);
        if(i<ans) printf(" ");
    }
    return 0;
}

1098 Insertion or Heap Sort (25 分)
 

According to Wikipedia:

Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.

Heap sort divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the largest element and moving that to the sorted region. it involves the use of a heap data structure rather than a linear-time search to find the maximum.

Now given the initial sequence of integers, together with a sequence which is a result of several iterations of some sorting method, can you tell which sorting method we are using?

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤). Then in the next line, N integers are given as the initial sequence. The last line contains the partially sorted sequence of the N numbers. It is assumed that the target sequence is always ascending. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in the first line either "Insertion Sort" or "Heap Sort" to indicate the method used to obtain the partial result. Then run this method for one more iteration and output in the second line the resulting sequence. It is guaranteed that the answer is unique for each test case. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.

Sample Input 1:

10
3 1 2 8 7 5 9 4 6 0
1 2 3 7 8 5 9 4 6 0

Sample Output 1:

Insertion Sort
1 2 3 5 7 8 9 4 6 0

Sample Input 2:

10
3 1 2 8 7 5 9 4 6 0
6 4 5 1 0 3 2 7 8 9

Sample Output 2:

Heap Sort
5 4 3 1 0 2 6 7 8 9


#include <cstdio>
#include <algorithm> 
using namespace std;
const int M=110;
int n;
int init[M],tar[M],temp[M];
void insertion(int index)
{
    if(index>0) sort(temp,temp+index);
}
bool judge(int init[],int tar[])
{
    for(int i=1;i<=n;i++)
    {
        if(init[i]!=tar[i]) return false;
    }
    return true;
}
void print()
{
    for(int i=1;i<=n;i++)
    {
        printf("%d",temp[i]);
        if(i<n) printf(" ");
    }
    printf("\n");
}
void down(int low,int high)
{
    int i=low,j=i*2;
    while(j<=high)
    {
        if(j+1<=high&&temp[j+1]>temp[j])
        {
            j+=1;
        }
        if(temp[j]>temp[i])
        {
            swap(temp[i],temp[j]);
            i=j;
            j=i*2;
        }else break;    
    } 
}


int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&init[i]);
    for(int i=1;i<=n;i++) scanf("%d",&tar[i]);
    for(int i=1;i<=n;i++) temp[i]=init[i];
    for(int i=2;i<=n;i++)
    {
        insertion(i);
        if(judge(tar,temp)&&i!=2)
        {
            printf("Insertion Sort\n");
            insertion(i+1);
            print();
            return 0;
        }
    }
    printf("Heap Sort\n");
    for(int i=1;i<=n;i++) temp[i]=init[i];
    for(int i=n/2;i>=1;i--) down(i,n);
    bool flag=false;
    for(int i=n;i>1;i--)
    {
        if(judge(temp,tar)&&i!=n)
        {
            flag=true;
        }
        swap(temp[i],temp[1]);
        down(1,i-1);
        if(flag)
        {
            print();
            break;
        }
        
    }
    return 0;
}

 我好菜啊

猜你喜欢

转载自www.cnblogs.com/fremontxutheultimate/p/11321821.html