Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace AlgorithmsLab5_C_v2
- {
- class Edge
- {
- public int From, To;
- public long Capactity, Flow;
- public Edge(int from, int to, int capacity, int flow)
- {
- From = from;
- To = to;
- Capactity = capacity;
- Flow = flow;
- }
- }
- class Program
- {
- private static List<Edge> _edges;
- private static List<int>[] _graph;
- static void CreateEdge(int from, int to, int capacity)
- {
- Edge forward = new Edge(from, to, capacity, 0),
- backward = new Edge(to, from, 0, 0);
- _graph[from].Add(_edges.Count);
- _edges.Add(forward);
- _graph[to].Add(_edges.Count);
- _edges.Add(backward);
- }
- private static int n;
- private static int[] _q;
- private static int[] _d;
- private static bool BFS()
- {
- int qh = 0, qt = 0;
- _q[qt++] = 0;
- for (int i = 0; i < _d.Length; i++)
- _d[i] = -1;
- _d[0] = 0;
- while (qh < qt && _d[n - 1] == -1)
- {
- int v = _q[qh++];
- for (int i = 0; i < _graph[v].Count; i++)
- {
- int id = _graph[v][i],
- to = _edges[id].To;
- if (_d[to] == -1 && _edges[id].Flow < _edges[id].Capactity)
- {
- _q[qt++] = to;
- _d[to] = _d[v] + 1;
- }
- }
- }
- return _d[n - 1] != -1;
- }
- private static int[] ptr;
- private static long DFS(int from, long flow)
- {
- if (flow == 0) return 0;
- if (from == n - 1) return flow;
- while (ptr[from] < _graph[from].Count)
- {
- int id = _graph[from][ptr[from]],
- to = _edges[id].To;
- if (_d[to] == _d[from] + 1)
- {
- long pushed = DFS(to, Math.Min(flow, _edges[id].Capactity - _edges[id].Flow));
- if (pushed != 0)
- {
- _edges[id].Flow += pushed;
- _edges[id ^ 1].Flow -= pushed;
- return pushed;
- }
- }
- ptr[from]++;
- }
- return 0;
- }
- static void Main(string[] args)
- {
- new Thread(Solve, 64000000).Start();
- }
- private static void Solve()
- {
- StreamReader sr = new StreamReader("cut.in");
- StreamWriter sw = new StreamWriter("cut.out");
- string[] terms = sr.ReadLine().Split(' ');
- n = int.Parse(terms[0]);
- int m = int.Parse(terms[1]);
- _edges = new List<Edge>();
- _graph = new List<int>[n];
- _q = new int[n];
- _d = new int[n];
- ptr = new int[n];
- for (int i = 0; i < n; i++)
- _graph[i] = new List<int>();
- for (int i = 0; i < m; i++)
- {
- terms = sr.ReadLine().Split(' ');
- int from = int.Parse(terms[0]) - 1,
- to = int.Parse(terms[1]) - 1,
- capacity = int.Parse(terms[2]);
- CreateEdge(@from, to, capacity);
- CreateEdge(to, @from, capacity);
- }
- while (BFS())
- {
- for (int i = 0; i < ptr.Length; i++)
- ptr[i] = 0;
- while (true)
- if (DFS(0, long.MaxValue) == 0) break;
- }
- bool[] visited = new bool[n];
- DfsOnFull(0, _graph, _edges, visited);
- sw.WriteLine(visited.Count(x => x));
- for (int i = 0; i < n; i++)
- if (visited[i])
- sw.Write((i + 1) + " ");
- sw.Close();
- }
- static void DfsOnFull(int from, List<int>[] graph, List<Edge> edges, bool[] used)
- {
- if (!used[from])
- {
- used[from] = true;
- foreach (var edge_id in graph[from])
- if (edges[edge_id].Flow < edges[edge_id].Capactity)
- DfsOnFull(edges[edge_id].To, graph, edges, used);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement