4 public class inspection_rs
implements Runnable
{
5 private BufferedReader in
;
6 private PrintWriter out
;
8 private void check(boolean expr
, String msg
) {
14 private void checkBounds(int n
, int low
, int hi
, String nStr
) {
15 check((low
<= n
) && (n
<= hi
), nStr
+ " is not in [" + low
+ ", " + hi
+ "]");
18 private boolean[][] go
, w
;
19 private int[][] flow
, cap
, ans
;
20 private boolean[] mark
;
24 private static final int MAXW
= 10000;
26 private void solve() throws IOException
{
27 n
= Integer
.parseInt(in
.readLine());
28 checkBounds(n
, 1, 100, "n");
29 go
= new boolean[n
][n
];
30 w
= new boolean[n
][n
];
31 for (int i
= 0; i
< n
; i
++) {
32 StringTokenizer st
= new StringTokenizer(in
.readLine());
33 int w
= Integer
.parseInt(st
.nextToken());
34 for (int j
= 0; j
< w
; j
++) {
35 int u
= Integer
.parseInt(st
.nextToken()) - 1;
36 check(u
!= i
, "Edge from vertix to itself");
37 check(!go
[i
][u
], "Duplicate edges");
42 for (int i
= 0; i
< n
; i
++) {
43 for (int j
= 0; j
< n
; j
++) {
44 for (int k
= 0; k
< n
; k
++) {
45 if (w
[j
][i
] && w
[i
][k
]) {
51 for (int i
= 0; i
< n
; i
++) {
52 check(!w
[i
][i
], "graph is not acyclic");
57 flow
= new int[n
+ 2][n
+ 2];
58 cap
= new int[n
+ 2][n
+ 2];
59 int[][] baseFlow
= new int[n
+ 2][n
+ 2];
60 for (int i
= 0; i
< n
; i
++) {
61 for (int j
= 0; j
< n
; j
++) {
63 baseFlow
[i
][j
] = MAXW
;
64 baseFlow
[s
][i
] += MAXW
;
65 baseFlow
[j
][t
] += MAXW
;
71 flow
[i
][j
] = MAXW
- 1;
72 flow
[j
][i
] = -(MAXW
- 1);
74 flow
[s
][i
] += MAXW
- 1;
75 flow
[i
][s
] -= MAXW
- 1;
77 flow
[j
][t
] += MAXW
- 1;
78 flow
[t
][j
] -= MAXW
- 1;
85 mark
= new boolean[n
];
87 Arrays
.fill(mark
, false);
94 for (int i
= 0; i
< n
; i
++) {
95 for (int j
= 0; j
< n
; j
++) {
99 ans
[i
][j
] = baseFlow
[i
][j
] - flow
[i
][j
];
105 for (int i
= 0; i
< n
; i
++) {
111 for (int i
= 0; i
< n
; i
++) {
112 while (ans
[s
][i
] > 0) {
113 Arrays
.fill(mark
, false);
116 for (int j
= count
- 2; j
>= 0; j
--) {
117 out
.print((path
[j
] + 1) + " ");
125 private boolean findPath(int u
, int target
) {
130 for (int v
= 0; v
< n
; v
++) {
131 if (ans
[u
][v
] > 0 && !mark
[v
] && findPath(v
, target
)) {
140 private boolean dfs(int u
, int target
, int a
) {
145 for (int v
= 0; v
< n
; v
++) {
146 if (!mark
[v
] && flow
[u
][v
] + a
<= cap
[u
][v
] && dfs(v
, target
, a
)) {
155 public static void main(String
[] args
) {
156 new Thread(new inspection_rs()).start();
160 String problem
= getClass().getName().split("_")[0];
162 in
= new BufferedReader(new FileReader(new File(problem
+ ".in")));
163 out
= new PrintWriter(new File(problem
+ ".out"));
167 } catch (IOException e
) {