クラスAとチェックセットを軽くたたく

A1118

#include<iostream>
using namespace std;

const int maxn=10010;

int father[maxn];

bool bird[maxn]={
    
    false};

int n,Q;

int findfather(int x)      //问题就出现在这里,如果使用递归的方式寻找父亲结点会比使用while快速
{
    
    
    if(father[x]!=x)
        father[x]=findfather(father[x]);
    return father[x];
}



int cishu=0;


/*
    树的个数=鸟的个数-合并的次数
*/

int main()
{
    
    
    scanf("%d",&n);
    int tree=maxn;

    for(int i=0; i<maxn; i++)
        father[i]=i;

    int temp,temp1,temp2[10];

    for(int i=0; i<n; i++)
    {
    
    
        scanf("%d",&temp);
        for(int j=0; j<temp; j++)
        {
    
    
            cin >> temp2[j];
            bird[temp2[j]]=true;
            int faA=findfather(temp2[j]);
            int faB=findfather(temp2[0]);
            if(faA!=faB)
            {
    
    
                father[faA]=faB;
                cishu++;
            }
        }
    }

    int birdshu=0;
    for(int i=1; i<maxn; i++)
    {
    
    
        birdshu+=bird[i];
    }
    printf("%d %d\n",birdshu-cishu,birdshu);
    scanf("%d",&Q);
    for(int i=0; i<Q; i++)
    {
    
    
        cin >> temp >> temp1;
        if(findfather(temp1)==findfather(temp))
            puts("Yes");
        else
            puts("No");
    }
    return 0;
}

A1114

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

const int maxn=10010;

int n,hc[maxn],ha[maxn];

struct node
{
    
    
    int x,y;
}Node[maxn];

int p[maxn],c[maxn];

bool st[maxn]={
    
    false};

int findfather(int x)
{
    
    
    if(p[x]!=x)
        p[x]=findfather(p[x]);
    return p[x];
}

struct Family
{
    
    
    int id,c,hc,ha;
};

bool cmp(Family a, Family b)
{
    
    
    if(a.ha*b.c!=b.ha*a.c)
    {
    
    
        return a.ha*b.c>b.ha*a.c;
    }
    return a.id<b.id;
}

vector<Family> family;

int main()
{
    
    
    cin >> n;
    int m=0;
    int id,father,mother,k;
    for(int i=0; i<n; i++)
    {
    
    
        cin >> id >> father >> mother >> k;
        st[id]=true;
        if(father!=-1)
            Node[m++]={
    
    id,father};
        if(mother!=-1)
            Node[m++]={
    
    id,mother};
        for(int j=0; j<k; j++)
        {
    
    
            int son;
            cin >> son;
            Node[m++]={
    
    id,son};
        }
        cin >> hc[id] >> ha[id];
    }

    for(int i=0; i<maxn; i++)
    {
    
    
        p[i]=i;
        c[i]=1;
    }

    for(int i=0; i<m; i++)
    {
    
    
        int a=Node[i].x,b=Node[i].y;
        st[a]=st[b]=true;
        int faA=findfather(a);
        int faB=findfather(b);

        if(faA!=faB)
        {
    
    
            if(faA>faB)
                swap(faA,faB);      //faA小,faB大
            p[faB]=faA;
            hc[faA]+=hc[faB];
            ha[faA]+=ha[faB];
            c[faA]+=c[faB];
        }
    }

    for(int i=0; i<maxn; i++)
    {
    
    
        if(st[i]==true && p[i]==i)
        {
    
    
            family.push_back({
    
    i,c[i],hc[i],ha[i]});
        }
    }
    cout << family.size() << endl;

    sort(family.begin(),family.end(),cmp);

    for(int i=0; i<family.size(); i++)
    {
    
    
        printf("%04d %d %.3lf %.3lf\n",family[i].id,family[i].c,(double)family[i].hc/family[i].c,(double)family[i].ha/family[i].c);
    }
}




おすすめ

転載: blog.csdn.net/wsfhdhjs/article/details/109779272