1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
| import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.StringTokenizer;
/**
* @author jaeji
*
*/
class edge{
int to;
int flow;
int cap;
edge rever;
edge(){
this(0,0,0,null);
}
edge(int ar_to, int ar_flow, int ar_cap, edge ar_rever){
to = ar_to;
flow = ar_flow;
cap = ar_cap;
rever = ar_rever;
}
int residu() {
return cap-flow;
}
void addflow(int ar_flow) {
flow+=ar_flow;
rever.flow -=ar_flow;
}
}
public class Main {
public static void main(String[] args) throws IOException {
//정점 분할 필요
//0~399 까지는 나눴을 떄 간선이 들어오는 정점, 400~799까지는 나눴을 때 간선이 나가는 정점
BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) );
//BufferedReader br = new BufferedReader( new FileReader("input.txt" ) );
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int p = Integer.parseInt(st.nextToken());
int[] prev = new int[800];
ArrayList[] adj = new ArrayList[800];
Queue<Integer> q = new LinkedList<Integer>();
int total = 0,u=0,v=0,u1=0,v1=0,temp=0,v2=0,u2=0;
int flow = 0;
for(int i=0;i<800;i++) {
adj[i] = new ArrayList<edge>();
}
for(int i=0;i<n;i++) {
u = i;
v = i+400;
edge temp_edge = new edge(v,0,1,null);
edge temp_rever_edge = new edge(u,0,0,temp_edge);
temp_edge.rever = temp_rever_edge;
adj[u].add(temp_edge);
adj[v].add(temp_rever_edge);
}
for(int i=0;i<p;i++) {
st = new StringTokenizer(br.readLine());
u1 = Integer.parseInt(st.nextToken())-1+400;
v1 = Integer.parseInt(st.nextToken())-1;
u2 = u1-400;
v2 = v1+400;
edge temp_edge1 = new edge(v1,0,1,null);
edge temp_rever_edge1 = new edge(u1,0,0,temp_edge1);
temp_edge1.rever = temp_rever_edge1;
adj[u1].add(temp_edge1);
adj[v1].add(temp_rever_edge1);
edge temp_edge2 = new edge(v2,0,0,null);
edge temp_rever_edge2 = new edge(u2,0,1,temp_edge1);
temp_edge2.rever = temp_rever_edge2;
adj[u2].add(temp_edge2);
adj[v2].add(temp_rever_edge2);
}
int curr = 0;
edge next = null;
int S = 400; // 분할된 1번 정점의 나가는 곳
int E = 1; // 분할된 2번 정점의 들어오는 곳
while(true) {
edge[] path = new edge[800];
Arrays.fill(prev, -1);
q.clear();
q.add(S);
curr = 0;
next = null;
while(!q.isEmpty()) { // 소스에서 흐를수 있는 유량찾기
curr = q.poll();
for(int i=0;i<adj[curr].size();i++) {
next = (edge) adj[curr].get(i);
if(next.residu() > 0 && prev[next.to] == -1) {
q.add(next.to);
prev[next.to] = curr;
path[next.to] = next;
if(next.to == E) break;
}
}
}
if(prev[E] == -1) break; // 경로를 탐색 한 후 싱크로 가는 경로가 없는 경우
flow = Integer.MAX_VALUE;
for(int i=E; i!=S; i=prev[i]) {
flow = Math.min(flow,path[i].residu());
}
for(int i=E; i!=S; i=prev[i]) {
path[i].addflow(flow);
}
total += flow;
}
System.out.println(total);
}
}
|