1 import java
.io
.BufferedReader
;
2 import java
.io
.FileReader
;
3 import java
.io
.PrintWriter
;
4 import java
.io
.IOException
;
5 import java
.util
.StringTokenizer
;
7 import java
.util
.ArrayList
;
9 public class inspection_petr
implements Runnable
{
10 static final int INF
= 1000000;
12 private void solve() throws IOException
{
14 boolean[][] e
= new boolean[n
][n
];
15 int[] ne
= new int[n
];
16 List
<Integer
>[][] paths
= new List
[n
][n
];
18 for (int i
= 0; i
< n
; ++i
) {
20 paths
[i
][i
] = new ArrayList
<Integer
>();
21 for (int j
= 0; j
< m
; ++j
) {
22 int b
= nextInt() - 1;
24 paths
[i
][b
] = new ArrayList
<Integer
>();
30 for (int k
= 0; k
< n
; ++k
)
31 for (int i
= 0; i
< n
; ++i
)
32 for (int j
= 0; j
< n
; ++j
)
33 if (paths
[i
][j
] == null && paths
[i
][k
] != null && paths
[k
][j
] != null) {
34 paths
[i
][j
] = new ArrayList
<Integer
>();
35 paths
[i
][j
].addAll(paths
[i
][k
]);
36 paths
[i
][j
].addAll(paths
[k
][j
]);
38 int[][] c
= new int[2 * n
+ 2][2 * n
+ 2];
41 for (int i
= 0; i
< n
; ++i
)
42 for (int j
= 0; j
< n
; ++j
) {
47 if (paths
[i
][j
] != null) {
51 int[][] flow
= new int[2 * n
+ 2][2 * n
+ 2];
52 res
-= maxFlow(2 * n
+ 2, s
, t
, flow
, c
);
53 int[] topological
= new int[n
];
54 for (int i
= 0; i
< n
; ++i
)
56 for (int i
= 0; i
< n
; ++i
) {
57 for (int j
= 0; j
< n
; ++j
)
60 topological
[n
- 1 - i
] = j
;
61 for (int k
= 0; k
< n
; ++k
)
67 List
<List
<Integer
>>[] ending
= new List
[n
];
68 for (int i
= 0; i
< n
; ++i
)
69 ending
[i
] = new ArrayList
<List
<Integer
>>();
70 for (int i
= 0; i
< n
; ++i
) {
71 for (int j
= 0; j
< n
; ++j
)
72 if (e
[topological
[i
]][j
]) {
73 List
<Integer
> l
= getSome(n
, paths
, topological
[i
], ending
, flow
);
75 l
= new ArrayList
<Integer
>();
76 l
.add(topological
[i
]);
82 List
<List
<Integer
>> all
= new ArrayList
<List
<Integer
>>();
83 for (List
<List
<Integer
>> ll
: ending
)
85 if (all
.size() != res
)
86 throw new RuntimeException();
87 writer
.println(all
.size());
88 for (List
<Integer
> l
: all
) {
101 private List
<Integer
> getSome(int n
, List
<Integer
>[][] paths
, int at
, List
<List
<Integer
>>[] ending
, int[][] flow
) {
102 for (int i
= 0; i
< n
; ++i
)
103 if (flow
[i
][at
+ n
] > 0 && !ending
[i
].isEmpty()) {
105 List
<Integer
> l
= ending
[i
].get(ending
[i
].size() - 1);
106 ending
[i
].remove(ending
[i
].size() - 1);
107 l
.addAll(paths
[i
][at
]);
113 private int maxFlow(int n
, int s
, int t
, int[][] flow
, int[][] c
) {
116 int by
= dfs(n
, s
, t
, flow
, c
, new boolean[n
], INF
);
124 private int dfs(int n
, int at
, int t
, int[][] flow
, int[][] c
, boolean[] mark
, int by
) {
130 for (int i
= 0; i
< n
; ++i
)
131 if (flow
[at
][i
] < c
[at
][i
]) {
132 int nby
= Math
.min(by
, c
[at
][i
] - flow
[at
][i
]);
133 nby
= dfs(n
, i
, t
, flow
, c
, mark
, nby
);
142 public static void main(String
[] args
) throws InterruptedException
{
143 Thread thread
= new Thread(new inspection_petr());
148 static final String TASK_ID
= "inspection";
149 static final String IN_FILE
= TASK_ID
+ ".in";
150 static final String OUT_FILE
= TASK_ID
+ ".out";
151 BufferedReader reader
;
152 StringTokenizer tokenizer
;
157 reader
= new BufferedReader(new FileReader(IN_FILE
));
159 writer
= new PrintWriter(OUT_FILE
);
163 } catch (Exception e
) {
169 int nextInt() throws IOException
{
170 return Integer
.parseInt(nextToken());
173 long nextLong() throws IOException
{
174 return Long
.parseLong(nextToken());
177 double nextDouble() throws IOException
{
178 return Double
.parseDouble(nextToken());
181 String
nextToken() throws IOException
{
182 while (tokenizer
== null || !tokenizer
.hasMoreTokens()) {
183 tokenizer
= new StringTokenizer(reader
.readLine());
185 return tokenizer
.nextToken();