Pagini recente » Cod sursa (job #2686694) | Cod sursa (job #3174925) | Cod sursa (job #2816748) | Cod sursa (job #2029824) | Cod sursa (job #2694136)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#define INF 100001
using namespace std;
int n, m;
vector<vector<int>> residual;
vector<bool> visited;
vector<vector<int>> v;
vector<int> path;
vector<pair<int,int>> vertices;
bool bfs(){
queue<int> q;
path.clear();
path.resize(n+1, 0);
q.emplace(1);
path[1] = -1;
while(!q.empty() && !path[n]){
int current = q.front();
q.pop();
for(auto &node: v[current])
if(residual[current][node]>0 && !path[node]){
path[node] = current;
q.emplace(node);
}
}
return path[n] != 0;
}
void edmonds_karp(){
int maximum_flow = 0;
while(bfs()){
for(auto &node: v[n]){
int current = n;
path[current] = node;
if(path[node]>0){
int flow = INF;
while(path[current]!=-1){
flow = min(flow, residual[path[current]][current]);
current = path[current];
}
current = n;
while(path[current]!=-1){
residual[path[current]][current] -= flow;
residual[current][path[current]] += flow;
current = path[current];
}
maximum_flow+= flow;
}
}
}
}
void dfs(int node1){
visited[node1] = true;
for(auto &node2: v[node1])
if(!visited[node2] && residual[node1][node2])
dfs(node2);
}
int main() {
ifstream fin("critice.in");
ofstream fout("critice.out");
fin>>n>>m;
residual.resize(n+1, vector<int>(n+1, 0));
v.resize(n+1);
while(m--){
int x, y, c;
fin>>x>>y>>c;
v[x].emplace_back(y);
v[y].emplace_back(x);
residual[x][y] = c;
residual[y][x] = c;
vertices.emplace_back(x, y);
}
edmonds_karp();
visited.resize(n+1, false);
dfs(1);
int total = 0;
for(auto &vertex: vertices)
if(visited[vertex.first]!=visited[vertex.second])
total++;
fout<<total<<'\n';
for(int i=0;i<vertices.size();++i)
if(visited[vertices[i].first]!=visited[vertices[i].second])
fout<<i+1<<'\n';
return 0;
}