baekjoon 20251225

This commit is contained in:
songyc macbook 2025-12-25 22:49:53 +09:00
parent 59fb800c04
commit 6c5c41c27e
2 changed files with 37 additions and 0 deletions

View File

@ -0,0 +1 @@
print(-1 if (lambda x : x[0] < x[1] or ((x[0] + x[1]) % 2 != 0 and (x[0] - x[1]) % 2 != 0))(s := list(map(int,input().split()))) else ' '.join((lambda x : list([str((x[0]+x[1])//2), str((x[0]-x[1])//2)]))(s)))

View File

@ -0,0 +1,36 @@
export {};
const input = require("fs").readFileSync(0).toString().trim().split('\n');
const [n, m]: number[] = input[0].split(" ").map(Number);
let parent: number[] = Array.from({length: n}, (_, i) => i);
let rank: number[] = new Array(n).fill(0);
const find = (x: number): number => {
if(x !== parent[x]) return parent[x] = find(parent[x]);
return parent[x];
}
const union = (x: number, y: number): boolean => {
x = find(x);
y = find(y);
if(x===y) return false;
if(rank[x] < rank[y]) [x, y] = [y, x];
parent[y] = x;
if(rank[x] === rank[y]) rank[x] += 1;
return true;
}
let ans: number = 0;
for(let i=1; i<=m; i++) {
let [u, v]: number[] = input[i].split(" ").map(Number);
if(!union(u, v)) {
ans = i;
break;
}
}
console.log(ans);