Skip to content

Commit dbdc950

Browse files
authored
Merge pull request #76 from AlgorithmWithGod/khj20006
[20250211] BOJ / 플래3 / 도형 / 권혁준
2 parents 5f0b9da + 3e9891c commit dbdc950

File tree

2 files changed

+166
-0
lines changed

2 files changed

+166
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
```java
2+
3+
import java.util.*;
4+
import java.io.*;
5+
6+
7+
class Main {
8+
9+
// IO field
10+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
12+
static StringTokenizer st;
13+
14+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
15+
static int nextInt() {return Integer.parseInt(st.nextToken());}
16+
static long nextLong() {return Long.parseLong(st.nextToken());}
17+
static void bwEnd() throws Exception {bw.flush();bw.close();}
18+
19+
// Additional field
20+
21+
static int[] A;
22+
static long[][] dp;
23+
static long[][] sum;
24+
static int N, K;
25+
26+
public static void main(String[] args) throws Exception {
27+
28+
ready();
29+
solve();
30+
31+
bwEnd();
32+
}
33+
34+
static void ready() throws Exception{
35+
36+
N = Integer.parseInt(br.readLine());
37+
A = new int[N];
38+
nextLine();
39+
for(int i=0;i<N;i++) A[i] = nextInt();
40+
K = Integer.parseInt(br.readLine());
41+
dp = new long[500001][K+1];
42+
sum = new long[500001][K+1];
43+
44+
}
45+
46+
static void solve() throws Exception{
47+
48+
Arrays.sort(A);
49+
dp[A[0]][1] = 1;
50+
sum[A[0]][1] = 1;
51+
for(int i=1;i<N;i++) {
52+
for(int s=500000;s>2*A[i];s--) {
53+
for(int k=K;k>2;k--) {
54+
dp[s][k] += sum[s-A[i]][k-1];
55+
}
56+
}
57+
for(int s=500000;s>A[i];s--) {
58+
for(int k=K;k>1;k--) {
59+
sum[s][k] += sum[s-A[i]][k-1];
60+
}
61+
}
62+
for(int s=500000;s>A[i];s--) if(dp[s-A[i]][1] != 0) dp[s][2] += dp[s-A[i]][1];
63+
dp[A[i]][1]++;
64+
sum[A[i]][1]++;
65+
}
66+
67+
long ans = 0;
68+
for(int i=1;i<=500000;i++) ans += dp[i][K];
69+
bw.write(ans+"\n");
70+
71+
}
72+
73+
}
74+
75+
```
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
```java
2+
3+
import java.util.*;
4+
import java.io.*;
5+
6+
7+
class Main {
8+
9+
// IO field
10+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
12+
static StringTokenizer st;
13+
14+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
15+
static int nextInt() {return Integer.parseInt(st.nextToken());}
16+
static long nextLong() {return Long.parseLong(st.nextToken());}
17+
static void bwEnd() throws Exception {bw.flush();bw.close();}
18+
19+
// Additional field
20+
static int N;
21+
static boolean[][] know;
22+
static int[] vis;
23+
static boolean res = true;
24+
25+
public static void main(String[] args) throws Exception {
26+
27+
ready();
28+
solve();
29+
30+
bwEnd();
31+
}
32+
33+
static void ready() throws Exception{
34+
35+
N = Integer.parseInt(br.readLine());
36+
know = new boolean[N+1][N+1];
37+
vis = new int[N+1];
38+
nextLine();
39+
for(int a = nextInt(), b = nextInt();a != -1;) {
40+
know[a][b] = true;
41+
know[b][a] = true;
42+
nextLine();
43+
a = nextInt();
44+
b = nextInt();
45+
}
46+
47+
}
48+
49+
static void solve() throws Exception{
50+
51+
for(int i=1;i<=N;i++) if(vis[i] == 0) {
52+
vis[i] = 1;
53+
dfs(i,1);
54+
}
55+
if(!res) bw.write("-1");
56+
else {
57+
List<Integer> A = new ArrayList<>();
58+
List<Integer> B = new ArrayList<>();
59+
for(int i=1;i<=N;i++) if(vis[i] == 1) A.add(i); else B.add(i);
60+
bw.write("1\n");
61+
for(int i:A) bw.write(i+" ");
62+
bw.write("-1\n");
63+
for(int i:B) bw.write(i+" ");
64+
bw.write("-1\n");
65+
}
66+
67+
}
68+
69+
static void dfs(int n, int team) throws Exception {
70+
if(!res) return;
71+
List<Integer> next = new ArrayList<>();
72+
int opposite = team == 1 ? 2 : 1;
73+
for(int i=1;i<=N;i++) {
74+
if(know[n][i] || i == n) continue;
75+
if(vis[i] == 0) {
76+
vis[i] = opposite;
77+
next.add(i);
78+
continue;
79+
}
80+
else if(vis[i] == opposite) continue;
81+
res = false;
82+
return;
83+
}
84+
for(int i:next) {
85+
dfs(i,opposite);
86+
}
87+
}
88+
89+
}
90+
91+
```

0 commit comments

Comments
 (0)