結果
| 問題 |
No.2677 Minmax Independent Set
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-02-13 21:08:41 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,230 bytes |
| コンパイル時間 | 2,216 ms |
| コンパイル使用メモリ | 204,204 KB |
| 最終ジャッジ日時 | 2025-02-19 06:03:01 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 2 TLE * 1 -- * 58 |
ソースコード
// 次数が大きい順に試していくヒューリスティック
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> adj;
// 木DPを行う
pair<int, int> rec(int r, int par = -1) {
int ret0 = 0;
int ret1 = 1;
for (auto &&c : adj[r]) {
if (c == par) continue;
auto [dp0, dp1] = rec(c, r);
ret0 += max(dp0, dp1);
ret1 += dp0;
}
return {ret0, ret1};
}
int main() {
auto start = chrono::system_clock::now();
int N;
cin >> N;
adj.resize(N);
for (int i = 0; i < N - 1; i++) {
int u, v;
cin >> u >> v;
u--;
v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
vector<pair<int, int>> order;
for (int i = 0; i < N; i++) {
order.push_back({adj[i].size(), i});
}
sort(order.begin(), order.end(), greater<pair<int, int>>());
int ans = numeric_limits<int>::max();
for (auto &&[_, r] : order) {
auto now = chrono::system_clock::now();
auto ms = chrono::duration_cast<std::chrono::milliseconds>(now - start).count();
if (ms > 3500) break;
auto [dp0, dp1] = rec(r);
ans = min(ans, dp1);
}
cout << ans << endl;
}