Cod sursa(job #3185709)

Utilizator sirbu27Sirbu Iulia-Georgiana sirbu27 Data 19 decembrie 2023 23:44:15
Problema Taramul Nicaieri Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.02 kb
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <fstream>

using namespace std;

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

int N;
vector<vector<int>> adj;
vector<vector<int>> check;
queue<pair<int, int>> edges;

struct node
{
    int in, out;
} v[101];

void bfs(int s)
{
    queue<int> q;
    q.push(s);

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

        for (int next : adj[curr])
        {
            if (v[curr].out != 0)
            {
                // cout << curr << " " << next << " " << v[next].in << endl;
                if (v[next].in > 0 && check[next][curr] == 0)
                {
                    // cout << curr << " " << next << endl;

                    check[curr][next] = 1;
                    edges.push({curr, next});
                    v[curr].out -= 1;
                    v[next].in -= 1;
                    if (v[next].out != 0)
                        q.push(next);
                }
            }
        }
    }
}

int main()
{
    fin >> N;
    adj.assign(N, vector<int>());
    check.assign(N, vector<int>(N, 0));

    for (int i = 0; i < N; i++)
    {
        for (int j = i + 1; j < N; j++)
        {
            adj[i].push_back(j);
            adj[j].push_back(i);
        }
    }

    for (int i = 0; i < N; i++)
    {
        fin >> v[i].out >> v[i].in;
    }

    bfs(0);

    fout << edges.size() << "\n";

    while (!edges.empty())
    {
        fout << edges.front().first + 1 << " " << edges.front().second + 1 << "\n";
        edges.pop();
    }

    // for (int i = 0; i < N; i++)
    // {
    //     for (int j = 0; j < N; j++)
    //     {
    //         cout << check[i][j] << " ";
    //     }
    //     cout << endl;
    // }

    // for (int i = 0; i < N; i++)
    // {
    //     for (int j = 0; j < adj[i].size(); j++)
    //     {
    //         cout << adj[i][j] << " ";
    //     }
    //     cout << endl;
    // }
}