提出 #57463036


ソースコード 拡げる

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;
using pl = pair<ll, ll>;
constexpr ll INF = 1e9 + 7;
constexpr ll mod = 1e9 + 7;
constexpr ld eps = 1e-9;
const ld PI = acos(-1);

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int n;
    cin >> n;
    vector<vector<int>> g(n);
    for (int i = 0; i < n - 1; ++i) {
        int u, v;
        cin >> u >> v;
        --u, --v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    vector<int> sz(n, 0);
    auto dfs = [&](auto &&dfs, int v, int p) -> void {
        sz[v] = 1;
        for (auto to: g[v]) {
            if (to == p)continue;
            dfs(dfs, to, v);
            sz[v] += sz[to];
        }
    };
    dfs(dfs, 0, -1);
    auto find = [&](auto &&find, int v, int p) -> int {
        int mx = n - sz[v];
        for (auto to: g[v]) {
            if (to == p)continue;
            int it = find(find, to, v);
            if (~it)return it;
            mx = max(mx, sz[to]);
        }
        return mx <= n / 2 ? v : -1;
    };
    int v = find(find, 0, -1);
    vector<pl> res;
    vector<vector<int>> all;
    auto go = [&](auto &&go, int v, int p, int id) -> void {
        if (~id)all[id].push_back(v);
        for (auto to: g[v]) {
            if (to == p)continue;
            int it = id;
            if (!~it) {
                it = all.size();
                all.emplace_back();
            }
            go(go, to, v, it);
        }
    };
    go(go, v, -1, -1);
    set<pl> s;
    for (int i = 0; i < all.size(); ++i) {
        s.insert(pl{(ll)all[i].size(),i});
    }
    while(s.size() > 1){
        auto x = *s.rbegin();
        s.erase(x);
        auto y = *s.rbegin();
        s.erase(y);
        x.first--;
        y.first--;
        res.push_back({all[x.second].back(),all[y.second].back()});
        all[x.second].pop_back();
        all[y.second].pop_back();
        if(x.first)s.insert(x);
        if(y.first)s.insert(y);
    }
    assert(s.size() <= 1);
    if (s.size()) {
        auto [val,who] = *s.begin();
        assert(val == 1);
        res.push_back({v, all[who].back()});
    }
    for (auto [x, y]: res) {
        cout << x + 1 << " " << y + 1 << "\n";
    }
}

提出情報

提出日時
問題 F - Perfect Matching on a Tree
ユーザ ZergTricky
言語 C++ 20 (gcc 12.2)
得点 550
コード長 2315 Byte
結果 AC
実行時間 119 ms
メモリ 38924 KiB

コンパイルエラー

Main.cpp: In function ‘int main()’:
Main.cpp:62:23: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<std::vector<int> >::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
   62 |     for (int i = 0; i < all.size(); ++i) {
      |                     ~~^~~~~~~~~~~~

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 550 / 550
結果
AC × 2
AC × 32
セット名 テストケース
Sample 00_sample_01.txt, 00_sample_02.txt
All 00_sample_01.txt, 00_sample_02.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 02_handmade_01.txt, 02_handmade_02.txt, 02_handmade_03.txt, 02_handmade_04.txt, 02_handmade_05.txt, 02_handmade_06.txt, 02_handmade_07.txt, 02_handmade_08.txt, 02_handmade_09.txt, 02_handmade_10.txt
ケース名 結果 実行時間 メモリ
00_sample_01.txt AC 1 ms 3480 KiB
00_sample_02.txt AC 1 ms 3468 KiB
01_random_01.txt AC 56 ms 13920 KiB
01_random_02.txt AC 89 ms 17976 KiB
01_random_03.txt AC 26 ms 8348 KiB
01_random_04.txt AC 82 ms 18144 KiB
01_random_05.txt AC 16 ms 6392 KiB
01_random_06.txt AC 88 ms 18060 KiB
01_random_07.txt AC 26 ms 8684 KiB
01_random_08.txt AC 86 ms 17884 KiB
01_random_09.txt AC 47 ms 11876 KiB
01_random_10.txt AC 86 ms 17940 KiB
01_random_11.txt AC 52 ms 13648 KiB
01_random_12.txt AC 83 ms 18428 KiB
01_random_13.txt AC 37 ms 10168 KiB
01_random_14.txt AC 82 ms 18352 KiB
01_random_15.txt AC 28 ms 8672 KiB
01_random_16.txt AC 87 ms 17972 KiB
01_random_17.txt AC 12 ms 5468 KiB
01_random_18.txt AC 85 ms 18024 KiB
01_random_19.txt AC 78 ms 17252 KiB
01_random_20.txt AC 88 ms 18440 KiB
02_handmade_01.txt AC 100 ms 26980 KiB
02_handmade_02.txt AC 97 ms 27044 KiB
02_handmade_03.txt AC 119 ms 38924 KiB
02_handmade_04.txt AC 115 ms 38832 KiB
02_handmade_05.txt AC 81 ms 17684 KiB
02_handmade_06.txt AC 76 ms 17728 KiB
02_handmade_07.txt AC 78 ms 17860 KiB
02_handmade_08.txt AC 84 ms 17912 KiB
02_handmade_09.txt AC 80 ms 17916 KiB
02_handmade_10.txt AC 80 ms 17848 KiB