upload solutions

This commit is contained in:
2026-04-26 08:46:11 +03:00
commit c71becc3d6
20 changed files with 562 additions and 0 deletions

72
B/Main.java Normal file
View File

@@ -0,0 +1,72 @@
import java.util.*;
import java.io.*;
public class Main {
static Map<String, String> parent = new HashMap<>();
static String find(String i) {
if (!parent.containsKey(i)) {
parent.put(i, i);
return i;
}
if (parent.get(i).equals(i))
return i;
String root = find(parent.get(i));
parent.put(i, root);
return root;
}
static boolean union(String a, String b) {
String rootA = find(a);
String rootB = find(b);
if (rootA.equals(rootB))
return true;
boolean isNumA = isNumber(rootA);
boolean isNumB = isNumber(rootB);
if (isNumA && isNumB) {
return rootA.equals(rootB);
}
if (isNumA) {
parent.put(rootB, rootA);
} else {
parent.put(rootA, rootB);
}
return true;
}
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
if (!sc.hasNextInt())
return;
int n = sc.nextInt();
String[] seq1 = new String[n];
String[] seq2 = new String[n];
for (int i = 0; i < n; i++)
seq1[i] = sc.next();
for (int i = 0; i < n; i++)
seq2[i] = sc.next();
boolean possible = true;
for (int i = 0; i < n; i++) {
if (!union(seq1[i], seq2[i])) {
possible = false;
break;
}
}
System.out.println(possible ? "YES" : "NO");
}
static boolean isNumber(String s) {
if (s == null || s.isEmpty())
return false;
char first = s.charAt(0);
return (first >= '0' && first <= '9') || (first == '-' && s.length() > 1);
}
}

3
B/test1.in Normal file
View File

@@ -0,0 +1,3 @@
3
3 1 2
3 1 3

3
B/test2.in Normal file
View File

@@ -0,0 +1,3 @@
4
4 5 igrek igrek
4 iks 3 iks

3
B/test3.in Normal file
View File

@@ -0,0 +1,3 @@
5
x 3 x y 3
x y 2 z 3