Cod sursa(job #1123666)

Utilizator romircea2010FMI Trifan Mircea Mihai romircea2010 Data 26 februarie 2014 09:33:55
Problema Cuplaj maxim in graf bipartit Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 3.09 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;

const int NMax = 1024, MMax = 10010;
int from[NMax];
bool viz[NMax];
bool v[2][NMax];
int cap[NMax][NMax];
int flow[NMax][NMax];
int n, m;
vector <int> G[NMax], ans;
struct muchie
{
    int x, y, c;
    muchie () {x = y = c;}
    muchie (const int x, const int y, const int c)
    {
        this -> x = x;
        this -> y = y;
        this -> c = c;
    }
};
muchie a[MMax];

void Read()
{
    ifstream f("critice.in");
    f>>n>>m;
    for (int i = 1; i<=m; ++i)
    {
        int x, y, c; f>>x>>y>>c;
        G[x].push_back(y);
        G[y].push_back(x);
        cap[x][y] = cap[y][x] = c;
        a[i] = muchie (x, y, c);
    }
    f.close();
}

bool BFS()
{
    for (int i = 1; i<=n; ++i)
        viz[i] = false, from[i] = 0;
    viz[1] = true;
    queue <int> Q;
    Q.push(1);
    while (!Q.empty())
    {
        int node = Q.front(); Q.pop();
        for (vector <int> :: iterator it = G[node].begin(); it!=G[node].end(); ++it)
            if (!viz[*it] && cap[node][*it] > flow[node][*it])
            {
                from[*it] = node;
                viz[*it] = true;
                Q.push(*it);
            }
    }
    return viz[n];
}

void MaxFlow()
{
    while (BFS())
    {
        for (int i = 1; i<n; ++i)
        {
            if (cap[i][n] > flow[i][n] && from[i])
            {
                int localflow = cap[i][n] - flow[i][n];
                int node = i;
                while (node != 1)
                {
                    localflow = min(localflow, cap[from[node]][node] - flow[from[node]][node]);
                    node = from[node];
                }
                if (localflow == 0)
                    continue;

                flow[i][n] += localflow;
                flow[n][i] -= localflow;
                node = i;
                while (node != 1)
                {
                    flow[from[node]][node] += localflow;
                    flow[node][from[node]] -= localflow;
                    node = from[node];
                }
            }
        }
    }
}

void DFS(const int &node, const int &ind)
{
    v[ind][node] = true;
    for (vector <int> :: iterator it = G[node].begin(); it != G[node].end(); ++it)
        if (!v[ind][*it] && cap[node][*it] > flow[node][*it])
            DFS(*it, ind);
}

void Solve()
{
    MaxFlow();
    DFS(1, 0);
    DFS(n, 1);
    for (int i = 1; i<=m; ++i)
    {
        int x, y, c;
        x = a[i].x;
        y = a[i].y;
        c = a[i].c;
        if ((v[0][x] && v[1][y] && (flow[x][y] == cap[x][y] || flow[y][x] == cap[y][x])) || (v[0][y] && v[1][x] && (flow[y][x] == cap[y][x] || flow[x][y] == cap[x][y])))
            ans.push_back(i);
    }
}

void Write()
{
    ofstream g("critice.out");
    g<<ans.size()<<"\n";
    for (vector <int> :: iterator it = ans.begin(); it!=ans.end(); ++it)
        g<<*it<<"\n";
    g.close();
}

int main()
{
    Read();
    Solve();
    Write();
    return 0;
}