Cod sursa(job #2925383)

Utilizator IvanAndreiIvan Andrei IvanAndrei Data 15 octombrie 2022 09:25:54
Problema Critice Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.92 kb
#include <fstream>
#include <vector>
#include <queue>
#include <bitset>

using namespace std;

ifstream in ("critice.in");
ofstream out ("critice.out");

const int max_size = 1e3 + 1, INF = 1e9 + 1;

struct str{
    int x, y;
};

int cap[max_size][max_size], ct[max_size][max_size], t[max_size], n;
vector <int> mc[max_size], ans;
vector <str> muchii;
bitset <max_size> viz;
queue <int> q;

bool bfsf ()
{
    queue <int> q;
    for (int i = 1; i <= n; i++)
    {
        viz[i] = 0;
        t[i] = 0;
    }
    viz[1] = 1;
    q.push(1);
    while (!q.empty())
    {
        int nod = q.front();
        q.pop();
        for (auto f : mc[nod])
        {
            if (!viz[f] && cap[nod][f] > 0)
            {
                t[f] = nod;
                viz[f] = 1;
                if (f == n)
                {
                    return true;
                }
                q.push(f);
            }
        }
    }
    return false;
}

void bfs (int nod)
{
    q.push(nod);
    viz[nod] = 1;
    while (!q.empty())
    {
        nod = q.front();
        q.pop();
        for (auto f : mc[nod])
        {
            if (viz[f] == 0)
            {
                viz[f] = 1;
                if (cap[f][nod] == 0 || cap[nod][f] == 0)
                {
                    ct[f][nod]++;
                    ct[nod][f]++;
                }
                else
                {
                    q.push(f);
                }
            }
        }
    }
}

int main ()
{
    int m;
    in >> n >> m;
    for (int i = 1; i <= m; i++)
    {
        int x, y, z;
        in >> x >> y >> z;
        muchii.push_back({x, y});
        cap[x][y] = z;
        cap[y][x] = z;
        mc[x].push_back(y);
        mc[y].push_back(x);
    }
    int flux = 0;
    while (bfsf())
    {
        for (auto f : mc[n])
        {
            if (cap[f][n] <= 0 || viz[f] == 0)
                continue;
            t[n] = f;
            int nod = n, mn = INF;
            while (nod != 1)
            {
                mn = min(mn, cap[t[nod]][nod]);
                nod = t[nod];
            }
            if (mn == 0)
                continue;
            nod = n;
            while (nod != 1)
            {
                cap[t[nod]][nod] -= mn;
                cap[nod][t[nod]] += mn;
                nod = t[nod];
            }
            flux += mn;
        }
    }
    //out << flux << '\n';
    for (int i = 1; i <= n; i++)
    {
        viz[i] = 0;
    }
    bfs(1);
    for (int i = 1; i <= n; i++)
    {
        viz[i] = 0;
    }
    bfs(n);
    for (int i = 0; i < muchii.size(); i++)
    {
        if (ct[muchii[i].x][muchii[i].y] == 2)
        {
            ans.push_back(i + 1);
        }
    }
    out << ans.size() << '\n';
    for (auto f : ans)
    {
        out << f << '\n';
    }
    in.close();
    out.close();
    return 0;
}