Cod sursa(job #2905702)

Utilizator raulandreipopRaul-Andrei Pop raulandreipop Data 23 mai 2022 09:42:16
Problema Critice Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.02 kb
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#define s second
#define f first
 
using namespace std;
 
typedef long long ll;
 
int n, m;
pair<ll , ll> flux[1005][1004];
vector<vector<int>> adj;
bool viz[1001];
int par[1001];
bool viz1[2][1001];
int nredge[1001][1001];
pair<int, int> edge[10001];
 
bool checkflow (int x, int y, int i)
{
    if (i == 1) swap(x, y);
    return (flux[x][y].s - flux[x][y].f > 0);
}
 
bool bfs ()
{
    queue<int> q;
    q.push(1);
    viz[1] = true;
    bool found = 0;
    while (!q.empty())
    {
        int crt = q.front();
        
        q.pop();
        viz[crt] = true;
        if (crt == n){
            continue;
        }
        else {
            for (auto to : adj[crt])
            {
                if (!viz[to] && flux[crt][to].s - flux[crt][to].f > 0)
                {
                    par[to] = crt;
                    q.push(to);
                }
            }
        }
    }
    if (viz[n]) return 1;
    return 0;
}
 
void dfs (int nod, int i)
{
    viz1[i][nod] = 1;
    
    for (auto to : adj[nod])
    {
        if (!viz1[i][to] && checkflow(nod, to, i))
        {
            dfs(to, i);
        }
    }
}
 
int main ()
{
    freopen("critice.in", "r", stdin);
    freopen("critice.out", "w", stdout);
    cin >> n >> m;
    adj.resize(n + 1);
    for (int i = 1; i <= m; i++)
    {
        int x, y; ll fx; cin >> x >> y >> fx;
        adj[x].push_back(y);
        adj[y].push_back(x);
        flux[x][y].s += fx;
        flux[y][x].s += fx;
        if (x > y) swap(x, y);
        nredge[x][y] = i;
        edge[i] = {x, y};
    }
    ll bottleneck = 0;
    ll ans = 0;
 
    while (bfs())
    {
        
        for (auto leaf : adj[n])
        {
            if (!viz[leaf] || !(flux[leaf][n].s - flux[leaf][n].f > 0))
                continue;
            par[n] = leaf;
            
            int v = n;
            ll minflow = 1e9;
            while (v != 1)
            {
                minflow = min(minflow, flux[par[v]][v].s - flux[par[v]][v].f);
                if (v == 0) continue;
                v = par[v];
            }
            v = n;
            while (v != 1)
            {
                flux[par[v]][v].f += minflow;
                flux[v][par[v]].f -= minflow;
                v = par[v];
            }
            ans += minflow;
        }
        for (int i = 1; i <= n; i++)
        {
            par[i] = -1;
            viz[i] = 0;
        }
    }
    dfs(1, 0);
    dfs(n, 1);
    ans = 0;
    vector<int> av;
 
    for (int i = 1; i <= m; i++)
    {
        int x = edge[i].first, y = edge[i].second;
        if ((viz1[0][x] && !viz1[1][x] && viz1[1][y] && !viz1[0][y])
            ||(viz1[1][x] && !viz1[0][x] && viz1[0][y] && !viz1[1][y]))
            {
                av.push_back(i);
            }
    }
 
    cout << av.size() << '\n';
    for (auto edge : av)
    {
        cout << edge << '\n';
    }
}