작성일 :

문제 링크

10693번 - Abdelrahman

설명

컴퓨터들이 케이블로 서로 도달 가능한 상태에서, 연결을 유지하며 제거할 수 있는 최대 케이블 수를 구하는 문제입니다.


접근법

연결 그래프를 유지하는 최소 간선 수는 N-1개입니다. 따라서 제거 가능한 최대 케이블 수는 M - (N - 1)입니다.


Code

C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
using System;

class Program {
  static void Main() {
    var t = int.Parse(Console.ReadLine()!);
    for (var tc = 1; tc <= t; tc++) {
      var parts = Console.ReadLine()!.Split();
      var n = int.Parse(parts[0]);
      var m = int.Parse(parts[1]);
      var ans = m - (n - 1);
      Console.WriteLine($"Case {tc}: {ans}");
    }
  }
}

C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int t; cin >> t;
  for (int tc = 1; tc <= t; tc++) {
    int n, m; cin >> n >> m;
    int ans = m - (n - 1);
    cout << "Case " << tc << ": " << ans << "\n";
  }

  return 0;
}

Tags: 10693, arithmetic, BOJ, C#, C++, 구현, 백준, 수학, 알고리즘

Categories: ,