結果

問題 No.2418 情報通だよ!Nafmoくん
ユーザー kpinkcatkpinkcat
提出日時 2023-10-03 16:11:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,311 bytes
コンパイル時間 2,357 ms
コンパイル使用メモリ 206,608 KB
実行使用メモリ 8,764 KB
最終ジャッジ日時 2023-10-03 16:11:47
合計ジャッジ時間 22,151 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
8,764 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 TLE -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 TLE -
testcase_11 TLE -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 36 ms
4,380 KB
testcase_16 TLE -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include<iostream>
#include<map>
#include<vector>
#include <algorithm>
#include<math.h>
#include <iomanip>
#include<set>
#include <numeric>
#include<string>
using ll = long long;
using namespace std;

int main(){
    int n, m, cnt = 0, r = 0;
    cin >> n >> m;
    vector<int> lis(2*n+1);
    map<int, int> mp;
    for (ll i = 0; i < m; i++){
        int a, b, t1, t2;
        cin >> a >> b;
        t1 = min(a, b);
        if (!lis[a] && !lis[b]){
            lis[a] = t1;
            lis[b] = t1;
        } else if (lis[a] && !lis[b]){
            lis[b] = lis[a];
        } else if (!lis[a] && lis[b]){
            lis[a] = lis[b];
        } else {
            t1 = max(lis[a], lis[b]);
            t2 = min(lis[a], lis[b]);
            if (t1 != t2){
                for (int j = 0; j < 2*n + 1; j++){
                    if (lis[j] == t1) lis[j] = t2;
                }
            }
        }
    }
    for (int j = 0; j < 2*n + 1; j++){
        if (lis[j]) mp[lis[j]]++;
    }
    for (auto iter = mp.begin(); iter != mp.end(); iter++){
        if (!(iter->second%2)) n -= (iter->second)/2;
        else {
            n -= (iter->second - 1)/2;
            r++;
        }
        if (r == 2){
            cnt++;
            r = 0;
        }
    }
    cout << cnt + n << endl;
}
0