diff --git a/11-03-24/b.py b/11-03-24/b.py new file mode 100644 index 0000000..24e2cf1 --- /dev/null +++ b/11-03-24/b.py @@ -0,0 +1,43 @@ +def main(): + n = int(input()) + fa = [int(i)-1 for i in input().split()] + est_feuille = [True for _ in range(n)] + for i in range(n): + if fa[i] != i: + est_feuille[fa[i]] = False + + f_count = 0 + for i in range(n): + if est_feuille[i]: + f_count += 1 + + print(f_count) + + used = set() + def find_path(s): + l = [] + length = 0 + while s not in used: + l.append(s) + used.add(s) + length += 1 + s = fa[s] + return reversed(l), length + + + paths = [] + for i in range(n): + if est_feuille[i]: + p, n = find_path(i) + + print(n) + print(" ".join([str(i+1) for i in p])) + + print() + + + + + +for _ in range(int(input())): + main() \ No newline at end of file diff --git a/11-03-24/c.py b/11-03-24/c.py new file mode 100644 index 0000000..4a9321f --- /dev/null +++ b/11-03-24/c.py @@ -0,0 +1,53 @@ +def main(): + n, m = [int(i) for i in input().split()] + + data = [[a for a in input()] for _ in range(n)] + + colors = set() + for i in data: + for j in i: + colors.add(j) + + def voisins(i, j): + col = data[i][j] + v = [ + (i-1, j), + (i+1, j), + (i, j-1), + (i, j+1) + ] + return [ + (a, b) for (a, b) in v + if a >= 0 and b >= 0 + and a < n and b < m + and data[a][b] == col + ] + + vus = set() + + def parcours(i, j, pere=None): + vus.add((i, j)) + vois = voisins(i, j) + #print(f"voisins de {(i, j)}: {vois}") + for v_ in vois: + if v_ != pere: + if v_ in vus: + return True + vus.add(v_) + if parcours(*v_, pere=(i, j)): + return True + return False + + for i in range(n): + for j in range(m): + if (i, j) not in vus: + # print(vus) + if parcours(i, j): + return True + return False + + +if main(): + print("Yes") +else: + print("No") \ No newline at end of file diff --git a/18-03-24/a.py b/18-03-24/a.py new file mode 100644 index 0000000..88d98b5 --- /dev/null +++ b/18-03-24/a.py @@ -0,0 +1,25 @@ +[r1, c1, r2, c2] = [int(i) for i in input().split()] + +def tower(): + s = 0 + if r1 != r2: + s += 1 + if c1 != c2: + s += 1 + return s + + +def rook(): + if ((r1+c1)%2 != (r2+c2)%2) or (r1, c1)==(r2, c2): + return 0 + + s = 1 + if abs(r2-r1) != abs(c2-c1): + s += 1 + return s + +def king(): + return max(abs(r2-r1), abs(c2-c1)) + + +print(tower(), rook(), king()) \ No newline at end of file diff --git a/18-03-24/b.py b/18-03-24/b.py new file mode 100644 index 0000000..4db735e --- /dev/null +++ b/18-03-24/b.py @@ -0,0 +1,12 @@ +for i in range(int(input())): + n = int(input()) + dists = sorted([int(i) for i in input().split()]) + + weights = 0 + prev = 0 + for i, w in enumerate(dists): + weights += w - prev + weights -= (i*(n-i))*(w - prev) + prev = w + + print(weights) \ No newline at end of file