Cod sursa(job #2886612)

Utilizator AndreiCroitoruAndrei Croitoru AndreiCroitoru Data 7 aprilie 2022 22:29:17
Problema Critice Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.54 kb
#include <bits/stdc++.h>

#pragma GCC optimize("Ofast")

using namespace std;

const int N = 1000 + 1;

bool verif2[N];
int tata[N];
int flow[N][N];
queue <int> q;
vector <int> g[N];


int n, m;
bool bfs()
{
    memset(tata, 0, sizeof(tata));
    q.push(1);
    tata[1] = 1;
    while(!q.empty())
    {
        int aux = q.front();
        q.pop();
        for(auto x:g[aux])
        {
            if(!tata[x] && flow[aux][x] > 0)
            {
                tata[x] = aux;
                q.push(x);
            }
        }
    }
    return (tata[n]);
}

void EdmondKarp()
{
    while(bfs())
    {
        for(auto nxt: g[n])
        {
            if(flow[nxt][n] <= 0)
                continue ;
            int curent = flow[nxt][n];
            for(int i = nxt; i != tata[i]; i = tata[i])
            {
                int aux = tata[i];
                curent=min(curent, flow[aux][i]);
            }
            flow[nxt][n] -= curent;
            flow[n][nxt] += curent;
            for(int i = nxt; i != tata[i]; i = tata[i])
            {
                int aux = tata[i];
                flow[aux][i] -= curent;
                flow[i][aux] += curent;
            }
        }
    }
}


void dfs(int node)
{
    verif2[node] = 1;
    for(auto nxt: g[node])
    {
        if(!verif2[nxt] and flow[node][nxt] > 0)
            dfs(nxt);
    }
}


vector <pair<int, int>> muchii;
vector <int> ans;

int main()
{
    ifstream cin("critice.in");
    ofstream cout("critice.out");

    //int start = 1, fin = n;

    cin >> n >> m;
    muchii.emplace_back(0, 0);

    for(int i = 1; i <= m; i++)
    {
        int x, y, z;
        cin >> x >> y >> z;
        muchii.emplace_back(x, y);

        flow[x][y] = z;
        flow[y][x] = z;

        g[x].push_back(y);
        g[y].push_back(x);
    }

    EdmondKarp();
    dfs(1);
    for(int i = 1; i <= m; i++)
    {
        int x = muchii[i].first;
        int y = muchii[i].second;
        if((verif2[x] && !verif2[y]) || (!verif2[x] && verif2[y]))
        {
            ans.push_back(i);
        }
    }
    cout << ans.size() << '\n';
    for(auto it: ans)
    {
        //assert(it);
        cout << it << '\n';
    }
    return 0;
}



/**void addElement(int node)
{
    verif[node] = true;
    if( node == n )
        return ;
    for(auto nxt: g[node])
    {
        if(!verif[nxt] and flow[node][nxt] < cap[node][nxt])
        {
            q.push(nxt);
            tata[nxt] = node;
        }
    }
}*/