Cod sursa(job #2694668)

Utilizator ddeliaioanaaDumitrescu Delia Ioana ddeliaioanaa Data 10 ianuarie 2021 13:08:16
Problema Critice Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.34 kb
#include <bits/stdc++.h>
std::ifstream fin("critice.in");
std::ofstream fout("critice.out");

const int INF = 1e9;
int n, m;
int rGrph[1001][1001];
std::vector <std::pair <int, int>> muchii;
std::vector<int> critice, parent, nei[1001];
std::vector <bool> viz;
std::queue<int> q;

bool bfs(){
    int i;
    for(i = 1; i <= n; ++i)
        parent[i] = 0;

    q.push(1);
    parent[1] = -1;

    while(!q.empty()){
        int node = q.front();
        q.pop();
        if (node != n){
            for(auto next_node: nei[node]){
                if(!parent[next_node] && rGrph[node][next_node]){
                    parent[next_node] = node;
                    q.push(next_node);
                }
            }
        }
    }

    return (parent[n]);
}

void ford_fulkerson(){
    int node, path_flow;
    while(bfs()){
        for(auto next_node: nei[n]){
            if(parent[next_node] != 0 && rGrph[next_node][n]){
                node = n;
                parent[n] = next_node;
                path_flow = INF;
                while(node != 1){
                    path_flow = std::min(path_flow, rGrph[parent[node]][node]);
                    node = parent[node];
                }

                node = n;
                if (path_flow){
                        while(node != 1){
                        rGrph[parent[node]][node] -= path_flow;
                        rGrph[node][parent[node]] += path_flow;
                        node = parent[node];
                    }
                }
            }
        }
    }
}

void dfs(int node){
    viz[node] = true;
    for (auto next_node : nei[node])
        if(!viz[next_node] && rGrph[node][next_node])
            dfs(next_node);
}

void solve(){
    int i, nr = 0;
    ford_fulkerson();
    dfs(1);
    for (i = 0; i < m; ++i)
        if (viz[muchii[i].first] != viz[muchii[i].second])
            nr++;
    fout << nr;
    for (i = 0; i < m; ++i)
        if (viz[muchii[i].first] != viz[muchii[i].second])
            fout << i << '\n';
}

int main()
{
    int x, y, cap, i;
    fin >> n >> m;

    parent.resize(n + 1);
    viz.resize(n + 1, false);
    muchii.resize(m + 1);

    for(i = 0; i < m ; ++i){
        fin >> x >> y >> cap;
        rGrph[x][y] = cap;
        rGrph[y][x] = cap;
        nei[x].push_back(y);
        nei[y].push_back(x);
        muchii[i] = std::make_pair(x, y);
    }

    solve();

    return 0;
}