Cod sursa(job #2211845)

Utilizator Dobricean_IoanDobricean Ionut Dobricean_Ioan Data 12 iunie 2018 00:47:05
Problema Critice Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.29 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <bitset>
#include <queue>

using namespace std;

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

const int MaxN = 1005;
const int Inf = 0x3f3f3f3f;

int n, m, C[MaxN][MaxN], t[MaxN];
vector<int> G[MaxN],rez;
vector < pair < int, int > > Muc;
bitset<MaxN> v;
queue <int> Q;
bool Viz1[MaxN],Viz2[MaxN];

void Dfs1(int node);
void Dfs2(int node);
int EdmondsKarp(int source, int sink);

int main() {
    int x, y, c;
    fin >> n >> m;
    for (int i = 1 ; i <= m ; ++i)   {
        fin >> x >> y >> c;
        G[x].push_back(y);
        G[y].push_back(x);
        Muc.push_back({x,y});
        C[x][y] += c;
        C[y][x] +=c;
    }
    EdmondsKarp(1,n);
    Dfs1(1);
    Dfs2(n);
    # define a i.first
    # define b i.second
    int cnt = 0;
    for ( const auto & i : Muc) {
		++cnt;
		if ( (C[a][b] == 0 or C[b][a] == 0) and (( Viz1[a] && Viz2[b] )^( Viz2[a] && Viz1[b])) )
			rez.push_back(cnt);
		}
	fout << rez.size() << "\n";
	for ( const auto & i : rez)
			fout << i << "\n";
    fin.close();
    fout.close();
}

 bool Bf(int source, int sink) {
    v.reset();
    v[source] = 1;
    Q.push(source);
    while (!Q.empty())
    {
        int x = Q.front();
        Q.pop();
        if (x == sink)
            continue;
        for (const auto& y : G[x])
            if (!v[y] && C[x][y] > 0)  {
                v[y] = 1;
                t[y] = x;
                Q.push(y);
            }
    }
    return v[sink];
}

int EdmondsKarp(int source, int sink) {
    int maxflow = 0, fmin;

    while (Bf(source, sink) )
        for (const auto& x : G[sink]) {
            if (!v[x] || C[x][sink] <= 0)
                continue;

            t[sink] = x;
            fmin = Inf;
            for (int i = sink ; i != source ; i = t[i])
                fmin = min(fmin, C[t[i]][i]);

            for (int i = sink ; i != source ; i = t[i])    {
                C[t[i]][i] -= fmin;
                C[i][t[i]] += fmin;
            }
            maxflow += fmin;
        }
    return maxflow;
}

void Dfs1(int node) {


	Viz1[node] = true;
	for ( const int & i : G[node] )
		if (!Viz1[i] and C[node][i] != 0  )
			Dfs1(i);
}

void Dfs2(int node) {

	Viz2[node] = true;
	for ( const int & i : G[node] )
		if (!Viz2[i] and C[node][i] != 0 )
			Dfs2(i);
}