Cod sursa(job #2859144)

Utilizator Theo20067Cismaru Theodor-Alexe Theo20067 Data 28 februarie 2022 21:36:45
Problema Subsecventa de suma maxima Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.27 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin ("flori.in");
ofstream fout("flori.out");
int F[1001],t[1001],i,j,x,r1,r2,r,n,y,k,c,nr;
int root(int x)
{
    while(t[x]>0)
        x=t[x];
    return x;
}
int main()
{
    fin>>n>>k;
    for(i=1;i<=n;i++)
        t[i]=-1;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=k;j++)
        {
            fin>>x;
            if(F[x]==0)
                F[x]=i;
            else
            {
                y=F[x];
                r1=root(y);
                r2=root(i);
                if(r1!=r2)
                {
                    if(t[r1]<=t[r2])
                    {
                        t[r1]=t[r1]+t[r2];
                        t[r2]=r1;
                    }
                    else
                    {
                        t[r2]=t[r2]+t[r1];
                        t[r1]=r2;
                    }
                }
            }
        }
    }
    for(i=1;i<=n;i++)
    {
        if(t[root(i)]<0)
        {

            fout<<i<<" ";
            for(j=i+1;j<=n;j++)
            {
                if(root(i)==root(j))
                    fout<<j<<" ";
            }
            fout<<"\n";
            t[root(t[i])]=0;
        }
    }
    return 0;
}