Skip to content

Commit 42a8c72

Browse files
authored
Merge pull request #512 from AlgorithmWithGod/khj20006
[20250721] BOJ / P5 / 파산하는 왕국 / 권혁준
2 parents 13d9fbb + e9cd7ae commit 42a8c72

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
class IOController {
6+
BufferedReader br;
7+
BufferedWriter bw;
8+
StringTokenizer st;
9+
10+
public IOController() {
11+
br = new BufferedReader(new InputStreamReader(System.in));
12+
bw = new BufferedWriter(new OutputStreamWriter(System.out));
13+
st = new StringTokenizer("");
14+
}
15+
16+
String nextLine() throws Exception {
17+
String line = br.readLine();
18+
st = new StringTokenizer(line);
19+
return line;
20+
}
21+
22+
String nextToken() throws Exception {
23+
while (!st.hasMoreTokens()) nextLine();
24+
return st.nextToken();
25+
}
26+
27+
int nextInt() throws Exception {
28+
return Integer.parseInt(nextToken());
29+
}
30+
31+
long nextLong() throws Exception {
32+
return Long.parseLong(nextToken());
33+
}
34+
35+
double nextDouble() throws Exception {
36+
return Double.parseDouble(nextToken());
37+
}
38+
39+
void close() throws Exception {
40+
bw.flush();
41+
bw.close();
42+
}
43+
44+
void write(String content) throws Exception {
45+
bw.write(content);
46+
}
47+
48+
}
49+
50+
public class Main {
51+
52+
static IOController io;
53+
54+
//
55+
56+
static int N;
57+
static int[][] a;
58+
static boolean[] dp, vis;
59+
60+
public static void main(String[] args) throws Exception {
61+
62+
io = new IOController();
63+
64+
for(int T=io.nextInt(); T-->0;){
65+
init();
66+
solve();
67+
}
68+
69+
io.close();
70+
71+
}
72+
73+
public static void init() throws Exception {
74+
75+
N = io.nextInt();
76+
a = new int[N][N];
77+
dp = new boolean[1<<N];
78+
vis = new boolean[1<<N];
79+
for(int i=0;i<N;i++) for(int j=0;j<N;j++) a[i][j] = io.nextInt();
80+
81+
}
82+
83+
static void solve() throws Exception {
84+
85+
dp[0] = true;
86+
vis[0] = true;
87+
int cnt = 0;
88+
for(int i=0;i<N;i++) {
89+
if(get(((1<<N)-1)^(1<<i))) {
90+
cnt++;
91+
io.write((i+1) + " ");
92+
}
93+
}
94+
if(cnt == 0) io.write("0");
95+
io.write("\n");
96+
97+
}
98+
99+
static boolean get(int k) {
100+
if(vis[k]) return dp[k];
101+
vis[k] = true;
102+
for(int i=0;i<N;i++) if((k & (1<<i)) != 0) {
103+
int p = k^(1<<i);
104+
int sum = 0;
105+
if(!get(p)) continue;
106+
for(int j=0;j<N;j++) if((p & (1<<j)) == 0) sum += a[i][j];
107+
if(sum > 0) return (dp[k] = true);
108+
}
109+
return dp[k];
110+
}
111+
112+
}
113+
```

0 commit comments

Comments
 (0)