結果

問題 No.556 仁義なきサルたち
ユーザー pekempeypekempey
提出日時 2017-08-11 23:04:35
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 235 ms / 2,000 ms
コード長 773 bytes
コンパイル時間 615 ms
コンパイル使用メモリ 78,720 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-20 23:23:58
合計ジャッジ時間 2,494 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 4 ms
5,376 KB
testcase_10 AC 4 ms
5,376 KB
testcase_11 AC 5 ms
5,376 KB
testcase_12 AC 5 ms
5,376 KB
testcase_13 AC 10 ms
5,376 KB
testcase_14 AC 35 ms
5,376 KB
testcase_15 AC 54 ms
5,376 KB
testcase_16 AC 68 ms
5,376 KB
testcase_17 AC 118 ms
5,376 KB
testcase_18 AC 235 ms
5,376 KB
testcase_19 AC 221 ms
5,376 KB
testcase_20 AC 220 ms
5,376 KB
testcase_21 AC 224 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;

int main() {
  int n, m;
  cin >> n >> m;

  vector<int> boss(n);
  for (int i = 0; i < n; i++) {
    boss[i] = i;
  }

  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    a = boss[a];
    b = boss[b];

    int sa = 0;
    int sb = 0;
    for (int j = 0; j < n; j++) {
      if (boss[j] == a) sa++;
      if (boss[j] == b) sb++;
    }

    if (sa > sb || (sa == sb && a < b)) {
      for (int j = 0; j < n; j++) {
        if (boss[j] == b) boss[j] = a;
      }
    } else {
      for (int j = 0; j < n; j++) {
        if (boss[j] == a) boss[j] = b;
      }
    }
  }

  for (int i = 0; i < n; i++) {
    cout << boss[i] + 1 << endl;
  }
}
0