package Test9;
import java.util.Scanner;
public class Test4 {
public static void main(String[] args) {
//三个点求区的 面积
Scanner s= new Scanner(System.in);
int M = s.nextInt();
int N =s.nextInt();
int [][] arr = new int[M][N];
int [][] n = new int[10][4];
System.out.println(n[0][0]);
s.nextLine();
for (int i = 0; i < M; i++) {
String str= s.nextLine();
for (int j = 0; j < N; j++) {
arr[i][j] = Integer.parseInt(str.substring(j,j+1));
if (i<n[arr[i][j]][0]){
n[arr[i][j]][0]=i;
} if (j<n[arr[i][j]][1]){
n[arr[i][j]][1]=j;
} if (i>n[arr[i][j]][2]){
n[arr[i][j]][2]=i;
} if (j>n[arr[i][j]][3]){
n[arr[i][j]][3]=j;
}
}
}
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
System.out.print(arr[i][j]);
}
System.out.println();
}
OUT:for (int i = 0; i < 10; i++) {
if (n[i][0]==n[i][2]&&n[i][1]==n[i][3]){
continue;
}else {
for (int k = n[i][0]; i <= n[i][2]; i++) {
for (int j = n[i][1]; j <= n[i][3]; j++) {
if (arr[k][j] != i){
System.out.println("不符合");
break OUT;
}
}
}
}
}
System.out.println("符合");
}
//整理玩具
}
T5版本分支
package Test9;
import java.util.Scanner;
public class Test5 {
//写一个并查集
static int parent[] = new int[100000];
static{
for (int i = 0; i < parent.length; i++) {
parent[i]=i;
}
}
static void union(int x, int y){
parent[find(y)]=x;
}
static int find(int x){
if (parent[x] == x){
return x;
}else {
return find(parent[x]);
}
}
static boolean find(int x,int y){
do {
if (parent[y]==x) return true;
y = parent[y];
}while (parent[y] != y);
return false;
}
public static void main(String[] args) {
Scanner s= new Scanner(System.in);
int M = s.nextInt();
int Q = s.nextInt();
System.out.println(M+Q);
for (int i = 0; i < M-1; i++) {
union(s.nextInt(),s.nextInt());
}
for (int i = 0; i < Q; i++) {
if (find(s.nextInt(),s.nextInt())){
System.out.println("YES");
}else {
System.out.println("NO");
}
}
}
}