P1526
import java.util.LinkedList;
import java.util.Scanner;
public class Exercise1526 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int in;
int[] a;
while ((in = sc.nextInt()) != 0) {
int sum = 0;
int n = in;
int m = sc.nextInt();
DFSGraph g = new DFSGraph(n);
for (int i = 0; i < m; i++)
g.addEdge(sc.nextInt(), sc.nextInt());
for (int i = 1; i <= n; i++)
if (!g.marked[i]) {
g.dfs(i);
sum++;
}
System.out.println(sum);
}
}
static class DFSGraph {
int v;
private boolean marked[];
private LinkedList<Integer> node[];
public DFSGraph(int v) {
marked = new boolean[v + 1];
node = (LinkedList<Integer>[]) new LinkedList[v + 1];
for (int i = 1; i <= v; i++)
node[i] = new LinkedList<Integer>();
}
private void addEdge(int s, int w) {
node[s].add(w);
node[w].add(s);
}
private void dfs(int s) {
marked[s] = true;
for (int w : node[s]) {
while (!marked[w])
dfs(w);
}
}
}
}
深度遍歷求解,耗時過多
import java.io.IOException;
import java.util.Scanner;
public class Exercise1526_2 {
static int[] a;
static int[] sz;
static int count;
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int in;
while ((in = sc.nextInt()) != 0) {
int n = in;
int m = sc.nextInt();
count = n;
a = new int[n + 1];
sz = new int[n + 1];
for (int i = 1; i <= n; i++)
a[i] = i;
for (int i = 1; i <= n; i++)
sz[i] = 1;
for (int i = 0; i < m; i++) {
int w = sc.nextInt();
int v = sc.nextInt();
union(w, v);
}
System.out.println(count);
}
}
public static int find(int p) {
while (a[p] != p)
p = a[p];
return p;
}
public static void union(int w, int v) {
int idw = find(w);
int idv = find(v);
if (idw == idv)
return;
if (sz[idw] < sz[idv]) {
a[idw] = idv;
sz[idv] += sz[idw];
} else {
a[idv] = idw;
sz[idw] += sz[idv];
}
count--;
}
}
路徑壓縮的並查集法,總時間超時
import java.io.IOException;
import java.io.StreamTokenizer;
public class Exercise1526_3 {
static int[] a;
static int[] sz;
static int count;
public static void main(String[] args) throws IOException {
StreamTokenizer st = new StreamTokenizer(System.in);
while (st.nextToken() != StreamTokenizer.TT_EOF) {
int n = (int) st.nval;
if (n == 0)
break;
st.nextToken();
int m = (int) st.nval;
count = n;
a = new int[n + 1];
sz = new int[n + 1];
for (int i = 1; i <= n; i++)
a[i] = i;
for (int i = 1; i <= n; i++)
sz[i] = 1;
for (int i = 0; i < m; i++) {
st.nextToken();
int w = (int) st.nval;
st.nextToken();
int v = (int) st.nval;
union(w, v);
}
System.out.println(count);
}
}
public static int find(int p) {
while (a[p] != p)
p = a[p];
return p;
}
public static void union(int w, int v) {
int idw = find(w);
int idv = find(v);
if (idw == idv)
return;
if (sz[idw] < sz[idv]) {
a[idw] = idv;
sz[idv] += sz[idw];
} else {
a[idv] = idw;
sz[idw] += sz[idv];
}
count--;
}
}
算法同2,換了IO方式(此IO方式已經過時)
import java.util.Scanner;
public class Exercise1529 {
private static int[][] a;
private static int[][] d;
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
a=new int[9][9];
while(sc.hasNext())
{
for(int i=1;i<=8;i++)
for(int j=1;j<=8;j++)
a[i][j]=sc.nextInt();
d=new int[9][9];
for(int i=1;i<=8;i++)
for(int j=1;j<=8;j++)
d[i][j]=Math.max(d[i-1][j], d[i][j-1])+a[i][j];
System.out.println(d[8][8]);
}
}
}
微策略筆試,DP,最優子結構
d[i][j]=Math.max(d[i-1][j], d[i][j-1])+a[i][j];
P1532
import java.util.Scanner;
public class Exercise1532 {
private static int[][] a;
private static int limit;
private static int max;
private static boolean flag;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
a = new int[9][9];
while (sc.hasNext()) {
limit = sc.nextInt();
max = 0;
flag = false;
for (int i = 1; i <= 8; i++)
for (int j = 1; j <= 8; j++)
a[i][j] = sc.nextInt();
dfs(1, 1, 0);
if (flag)
System.out.println(max);
else
System.out.println(-1);
}
}
private static void dfs(int i, int j, int d) {
d += a[i][j];
if (d > limit)
return;
if (i == 8 && j == 8) {
if (d > max) {
max = d;
flag = true;
}
return;
}
else if (i > 7)
dfs(i, j + 1, d);
else if (j > 7)
dfs(i + 1, j, d);
else {
dfs(i, j + 1, d);
dfs(i + 1, j, d);
}
}
}