baekjoon 20260113

This commit is contained in:
songyc macbook 2026-01-13 22:44:15 +09:00
parent a0b2080c9c
commit cd85e08165
3 changed files with 141 additions and 0 deletions

View File

@ -0,0 +1,68 @@
import java.util.*;
public class _1647_1 {
static int[] parents;
static int[] rank;
static int find(int x) {
if(x != parents[x]) return parents[x] = find(parents[x]);
return x;
}
static void union(int x, int y) {
x = find(x);
y = find(y);
if(x==y) return;
if(rank[x] < rank[y]) {
int temp = x;
x = y;
y = temp;
}
if(rank[x] == rank[y]) rank[x]++;
parents[y] = x;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt(), M = sc.nextInt();
if(N==2) {
System.out.println(0);
return;
}
ArrayList<int[]> edges = new ArrayList<>();
for(int i=0; i<M; i++) {
int A = sc.nextInt(), B = sc.nextInt(), C = sc.nextInt();
int[] edge = {A, B, C};
edges.add(edge);
}
sc.close();
edges.sort((a,b) -> a[2] - b[2]);
parents = new int[N+1];
for(int i=1; i<=N; i++) parents[i] = i;
rank = new int[N+1];
int cnt = 0, ans = 0;
for (int[] edge : edges) {
int a = edge[0], b = edge[1], c = edge[2];
if(find(a) == find(b)) continue;
union(a, b);
cnt += 1;
ans += c;
if(cnt == N-2) break;
}
System.out.println(ans);
}
}

View File

@ -0,0 +1,71 @@
import java.util.*;
class Node implements Comparable<Node> {
int node;
int dist;
Node(int node, int dist) {
this.node = node;
this.dist = dist;
}
@Override
public int compareTo(Node other) {
return this.dist - other.dist;
}
}
public class _1647_2 {
static void prim(ArrayList<ArrayList<Node>> graph, int V) {
boolean[] visited = new boolean[V+1];
PriorityQueue<Node> pq = new PriorityQueue<>();
pq.add(new Node(1, 0));
int cnt = 0, total_dist = 0, max_dist = 0;
while(!pq.isEmpty()) {
Node now = pq.poll();
int current_node = now.node, current_dist = now.dist;
if(visited[current_node]) continue;
visited[current_node] = true;
total_dist += current_dist;
max_dist = Math.max(max_dist, current_dist);
cnt += 1;
if(cnt == V) break;
for(Node next : graph.get(current_node)) {
if(!visited[next.node]) {
pq.add(next);
}
}
}
System.out.println(total_dist - max_dist);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt(), M = sc.nextInt();
if(N==2) {
System.out.println(0);
sc.close();
return ;
}
ArrayList<ArrayList<Node>> graph = new ArrayList<>();
for(int i=0; i<=N; i++) graph.add(new ArrayList<>());
for(int i=0; i<M; i++) {
int A = sc.nextInt(), B = sc.nextInt(), C = sc.nextInt();
graph.get(A).add(new Node(B, C));
graph.get(B).add(new Node(A, C));
}
sc.close();
prim(graph, N);
}
}

View File

@ -0,0 +1,2 @@
for _ in range(int(input())) :
print("yes" if 6 <= len(input().rstrip()) <= 9 else "no")