Cod sursa(job #2808627)

Utilizator mara.lucianaBelu Mara Luciana mara.luciana Data 25 noiembrie 2021 12:56:21
Problema Paduri de multimi disjuncte Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 6.84 kb
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <queue>
#include <vector>
#include <utility>
#include <bits/stdc++.h>

using namespace std;

ifstream fin("disjoint.in");
ofstream fout("disjoint.out");

#define NMAX 100001

class Graf{
private:
    int nrNod, nrMuch;
    bool orientat;
    vector<vector<int>> listaAd;
    vector<pair<int, pair<int, int>>> muchii_cost;
    list<pair<int, int> > *muchii_dij;
    vector<pair <int, int>> muchii_apm;
    int tata[NMAX];
    int dim[NMAX];


public:
    Graf(int nrNoduri = 0, int nrMuchii = 0, bool eOrientat = false)
    {
        this->nrNod = nrNoduri;
        this->nrMuch = nrMuchii;
        this->orientat = eOrientat;
        muchii_dij = new list<pair<int,int>> [nrNod + 1];

    }

    ~Graf()
    {
        this->nrNod = 0;
        this->nrMuch = 0;
        listaAd.clear();
        muchii_cost.clear();
        muchii_apm.clear();
    }

    void set_nrNod(int &);
    void set_nrMuch(int &);
    int get_nrNod();
    int get_nrMuch();
    void citire_muchii();
    void citire_muchii_apm();
    void citire_muchii_dijk();
//lab 3
    void bfs(int);
//lab 4
    void init();
    int reprez(int);
    void unite(int,int);
    void apm_kruskall();
    void dijkstra(int);
    bool bellmanford(int);
    void disjoint();
};


void Graf::set_nrNod(int &n) {nrNod = n;}

void Graf::set_nrMuch(int &m) {nrMuch = m;}

int Graf::get_nrNod() {return nrNod;}

int Graf::get_nrMuch() {return nrMuch;}

void Graf::citire_muchii()
{
    int x, y;
    listaAd.resize(nrNod + 1);

    for(int i = 0; i < nrMuch; i++)
    {
        fin >> x >> y;
        listaAd[x].push_back(y);

        if(!orientat)
            listaAd[y].push_back(x);
    }
}

void Graf::citire_muchii_apm()
{
    int x, y, c;
    pair<int, pair<int, int>> p;// cost(f) - x(sf) - y(ss)

    for(int i = 0; i < nrMuch; i++)
    {
        fin >> x >> y >> c;
        p.first = c;
        p.second.first = x;
        p.second.second = y;

        muchii_cost.push_back(p);
    }
}

void Graf::citire_muchii_dijk()
{
    int x, y, c;

    for(int i = 0; i < nrMuch; i++)
    {
        fin >> x >> y >> c;
        muchii_dij[x].push_back(make_pair(y,c));
    }
}

void Graf::bfs(int nod)
{
    int temp;
    queue<int> q;
    int dist[nrNod + 1];

    for(int i = 1; i <= nrNod; i++)
        dist[i] = -1;

    q.push(nod);
    dist[nod] = 0;

    while(!q.empty())
    {
        temp = q.front();
        q.pop();

        for(auto vecin:listaAd[temp])
        {
            if(dist[vecin] == -1)
            {
                q.push(vecin);
                dist[vecin] = dist[temp] + 1;
            }
        }
    }

    for(int i = 1; i <= nrNod; i++)
        fout << dist[i] << ' ';
}

void Graf::init()
{
	for(int i = 1; i <= nrNod; i++)
    {
        tata[i] = i;
        dim[i] = 1;
    }
}
int Graf::reprez(int x)
{
	if(tata[x] == x)
        return x;
	return reprez(tata[x]);
}

void Graf::unite(int x,int y)
{
	int repx = reprez(x), repy = reprez(y);

	if (repx == repy)
        return;
	if (dim[repx] >= dim[repy])
	{
		tata[repy] = repx;
		dim[repx] += dim[repy];
	}
	else
	{
		tata[repx] = repy;
		dim[repy] += dim[repx];
	}


//    muchii_apm.push_back(make_pair(x,y));
}

void Graf::apm_kruskall()
{
    init();

    int cost = 0;

	sort(muchii_cost.begin(), muchii_cost.end());

	for(auto m : muchii_cost)
    {
		if(reprez(m.second.first) != reprez(m.second.second))
		{
            unite(m.second.first, m.second.second);
            cost += m.first;
        }
    }

    fout << cost << endl;
    int n = muchii_apm.size();
    fout <<  n << endl;
    for(auto m : muchii_apm)
    {
        fout << m.first << ' ' << m.second << endl;
    }

}

void Graf::dijkstra(int startNod)
{
    priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;

    int dist[nrNod+1],viz[nrNod+1]={0};

    for(int i = 1; i <= nrNod; i++)
        dist[i]=INT_MAX;

    pq.push(make_pair(0, startNod));
    dist[startNod] = 0;

    while (!pq.empty())
    {
        int x = pq.top().second;
        pq.pop();

        if(viz[x])
            continue;
        else
            viz[x] = 1;

        for (auto i : muchii_dij[x])
        {
            int y = i.first;
            int cost = i.second;

            if (dist[y] > dist[x] + cost)
            {
                dist[y] = dist[x] + cost;
                pq.push(make_pair(dist[y], y));
            }
        }
    }

    for(int i = 2; i <= nrNod; i++)
        if(dist[i] != INT_MAX)
            fout << dist[i] << ' ';
        else
            fout << 0 << ' ';
}

bool Graf::bellmanford(int startNod)
{
    bool in_queue[nrNod+1]={0};
    int dist[nrNod+1],viz[nrNod+1]={0};

    for(int i = 1; i <= nrNod; i++){
        dist[i] = INT_MAX;}


	queue<int> q;

	q.push(startNod);
	in_queue[startNod] = 1;
	dist[startNod] = 0;

	while(!q.empty())
    {
        int x = q.front();
        q.pop();

		in_queue[x] = 0;
		viz[x]++;

		if(viz[x] > nrNod)
        {
            fout << "Ciclu negativ!";
            return false;
        }

		for(auto i : muchii_dij[x])
        {
            int y = i.first;
            int cost = i.second;

			if(dist[x] + cost < dist[y])
            {
				dist[y] = dist[x] + cost;

				if(!in_queue[x])
				{
					q.push(y);
                    in_queue[y] = 1;
                }
			}
		}
	}

    for(int i = 2; i <= nrNod; i++)
    {
        if(dist[i] != INT_MAX)
            fout << dist[i] << ' ';
        else
            fout << 0 << ' ';
    }

    return true;
}

void Graf::disjoint()
{
    int N, M, x, y, cod;

    fin >> N >> M;

    set_nrNod(N);
    init();

    for(int i = 0; i < M; i++)
    {
        fin >> cod >> x >> y;

        if(cod == 1)
            unite(x, y);
        else
        {
            if(reprez(x) == reprez(y))
                fout << "DA" << endl;
            else
                fout << "NU" << endl;
        }
    }
}

void bfs_infoarena()
{
    int N, M, S;

    fin >> N >> M >> S;

    Graf G(N, M, true);

    G.citire_muchii();

    G.bfs(S);
}

void apm_infoarena()
{
    int N, M;

    fin >> N >> M;

    Graf G(N, M, true);

    G.citire_muchii_apm();

    G.apm_kruskall();
}

void dijkstra_infoarena()
{
    int N, M;

    fin >> N >> M;

    Graf G(N, M);

    G.citire_muchii_dijk();

    G.dijkstra(1);

}

void bellmanford_infoarena()
{
    int N, M;

    fin >> N >> M;

    Graf G(N, M);

    G.citire_muchii_dijk();

    G.bellmanford(1);
}

void disjoint_infoarena()
{
    Graf G;

    G.disjoint();
}

int main()
{
    disjoint_infoarena();

    fin.close();
    fout.close();

    return 0;
}