0

私は n 個のセットの結合を取り、すべてのセットが互いに素であるかどうかを確認する問題に取り組んできました。私は使用しています

set_union

私のコードのドラフトは次のとおりです。

vector<int> I; // Vector to store union of all sets
vector<vector<int> >A; // Vectors whose union are to be taken
/* Read A */
repeat i from 0 to n-1
{
    I_size=I.size();
    Ai_size=A[i].size();
    I.resize(I_size+Ai_size);
    I=vector<int>(I.begin(),set_union(I.begin(),I.end(),A[i].begin(),A[i].end(),I.begin()));
    if(*(--I.end())==0) // If two sets contain some common term, last element of I will be 0
        Notify that the sets cannot be united // Break from the loop in case any two sets have common terms
}

A のどのベクトルにも 0 が含まれないため、I の最後の要素が 0 の場合にループから抜けます。上記のコードは機能しません。不明な実行時エラーが発生します。どこが間違っているのか教えてください。

実際のコード:

vector<vector<int> >A;
int n;

int SetIntersection()
{
    vector<int> I;
    for(int i=0; i<n; i++)
    {
        int I_size=I.size();
        int Ai_size=A[i].size();
        I.resize(I_size+Ai_size);
        I=vector<int>(I.begin(),set_union(I.begin(),I.end(),A[i].begin(),A[i].end(),I.begin()));
        if(*(--I.end())==0)
            return 0;
    }
return 1;
}

int main()
{
        int x;
        cin>>n;
        for(int i=0; i<n; i++)
        {
            cin>>x;
            A[i].push_back(x);
        }
        if(SetIntersection()==1)
            cout<<"Sets are disjoint";
        else
            cout<<"Sets are not disjoint";
return 0;
}
4

0 に答える 0