Cod sursa(job #1805024)

Utilizator BrandonChris Luntraru Brandon Data 13 noiembrie 2016 13:18:05
Problema Critice Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.9 kb
#include <fstream>
#include <vector>
#include <queue>
#include <cstring>

#define Pe pair <int, int>
#define mp make_pair
#define fi first
#define se second
using namespace std;

class InputReader {
public:
  InputReader() {}
  InputReader(const char *file_name) {
    input_file = fopen(file_name, "r");
    cursor = 0;
    fread(buffer, SIZE, 1, input_file);
  }
  inline InputReader &operator >>(int &n) {
    while (buffer[cursor] < '0' || buffer[cursor] > '9') {
      advance();
    }
    n = 0;
    while ('0' <= buffer[cursor] && buffer[cursor] <= '9') {
      n = n * 10 + buffer[cursor] - '0';
      advance();
    }
    return *this;
  }
private:
  FILE *input_file;
  static const int SIZE = 1 << 17;
  int cursor;
  char buffer[SIZE];
  inline void advance() {
    ++ cursor;
    if (cursor == SIZE) {
      cursor = 0;
      fread(buffer, SIZE, 1, input_file);
    }
  }
};

InputReader cin ("critice.in");
ofstream cout ("critice.out");

const int MaxN = 1005, Inf = 0x3f3f3f3f;

vector <Pe> Edg;
vector <int> Ans, G[MaxN];
queue <int> Q;
int n, m;
int Capacity[MaxN][MaxN], Flow[MaxN][MaxN], father[MaxN];
int used[MaxN];

inline void ClearQ() {
  while (Q.size()) {
    Q.pop();
  }
}

bool Bfs(int StNode = 1) {
  ClearQ();
  memset(father, 0, sizeof father);
  Q.push(StNode);
  father[StNode] = -1;
  while (Q.size()) {
    int node = Q.front();
    Q.pop();

    for (auto i: G[node]) {
      if (!father[i] and Capacity[node][i] - Flow[node][i]) {
        Q.push(i);
        father[i] = node;

        if (i == n) {
          return true;
        }
      }
    }
  }

  return false;
}

inline int CalcUpdate(int node = n) {
  int ans = Inf;
  while (father[node] != -1) {
    int parent = father[node];
    ans = min(ans, Capacity[parent][node] - Flow[parent][node]);
    node = parent;
  }

  return ans;
}

inline void FlowUpdate(int Quantity, int node = n) {
  while (father[node] != -1) {
    int parent = father[node];
    Flow[parent][node] += Quantity;
    Flow[node][parent] -= Quantity;
    node = parent;
  }
}

bool Check(Pe coord) {
  ClearQ();
  memset(father, 0, sizeof father);
  Q.push(1);
  father[1] = -1;
  while (Q.size()) {
    int node = Q.front();
    Q.pop();

    for (auto i: G[node]) {
      if (!father[i] and (Capacity[node][i] - Flow[node][i] or mp(node, i) == coord or mp(i, node) == coord)) {
        Q.push(i);
        father[i] = node;

        if (i == n) {
          return true;
        }
      }
    }
  }

  return false;
}

inline int FlowAllowed(int type, int node, int nxt) {
  if (type == 1) {
    return Capacity[node][nxt] - Flow[node][nxt];
  }

  return Capacity[nxt][node] - Flow[nxt][node];
}

void Dfs(int node, int type) {
  used[node] = type;
  for (auto nxt: G[node]) {
    if (used[nxt] or !FlowAllowed(type, node, nxt)) {
      continue;
    }

    Dfs(nxt, type);
    /*if (type == 1) {
      if (Capacity[node][nxt] - Flow[node][nxt]) {
        Dfs(nxt, type);
      }
    }
    else {
      if (Capacity[nxt][node] - Flow[nxt][node]) {
        Dfs(nxt, type);
      }
    }*/
  }
}

int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    G[a].push_back(b);
    G[b].push_back(a);
    Capacity[a][b] = c;
    Capacity[b][a] = c;
    Edg.push_back(mp(a, b));
  }

  while (Bfs()) {
    int Quantity = CalcUpdate();
    FlowUpdate(Quantity);
  }

  Dfs(1, 1);
  Dfs(n, 2);
  for (int i = 0; i < m; ++i) {
    int node1 = Edg[i].fi;
    int node2 = Edg[i].se;
    if (used[node1] and used[node2] and used[node1] != used[node2]) {
      Ans.push_back(i + 1);
    }
  }

  /*for (int i = 0; i < m; ++i) {
    if (Check(Edg[i])) {
      Ans.push_back(i + 1);
    }
  }*/

  cout << Ans.size() << '\n';
  for (auto it: Ans) {
    cout << it << '\n';
  }
  return 0;
}