結果
| 問題 |
No.2677 Minmax Independent Set
|
| コンテスト | |
| ユーザー |
👑 null
|
| 提出日時 | 2024-02-23 19:31:58 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 326 ms / 2,000 ms |
| コード長 | 6,329 bytes |
| コンパイル時間 | 7,038 ms |
| コンパイル使用メモリ | 462,120 KB |
| 実行使用メモリ | 125,264 KB |
| 最終ジャッジ日時 | 2024-09-30 00:10:53 |
| 合計ジャッジ時間 | 16,907 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 61 |
ソースコード
/*
このコード、と~おれ!
Be accepted!
∧_∧
(。・ω・。)つ━☆・*。
⊂ ノ ・゜+.
しーJ °。+ *´¨)
.· ´¸.·*´¨) ¸.·*¨)
(¸.·´ (¸.·'* ☆
*/
#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <vector>
#include <numeric>
#include <iostream>
#include <random>
#include <map>
#include <unordered_map>
#include <queue>
#include <regex>
#include <functional>
#include <complex>
#include <list>
#include <cassert>
#include <iomanip>
#include <set>
#include <stack>
#include <bitset>
#include <array>
#include <chrono>
#include <unordered_set>
//#pragma GCC target("arch=skylake-avx512")
//#pragma GCC target("avx2")
//#pragma GCC optimize("O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse4")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define repeat(i, n, m) for(int i = n; i < (m); ++i)
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define printynl(a) printf(a ? "yes\n" : "no\n")
#define printyn(a) printf(a ? "Yes\n" : "No\n")
#define printYN(a) printf(a ? "YES\n" : "NO\n")
#define printim(a) printf(a ? "possible\n" : "imposible\n")
#define printdb(a) printf("%.50lf\n", a)
#define printLdb(a) printf("%.50Lf\n", a)
#define printdbd(a) printf("%.16lf\n", a)
#define prints(s) printf("%s\n", s.c_str())
#define all(x) (x).begin(), (x).end()
#define deg_to_rad(deg) (((deg)/360.0L)*2.0L*PI)
#define rad_to_deg(rad) (((rad)/2.0L/PI)*360.0L)
#define Please return
#define AC 0
#define manhattan_dist(a, b, c, d) (abs(a - c) + abs(b - d))
using ll = long long;
using ull = unsigned long long;
constexpr int INF = 1073741823;
constexpr int MINF = -1073741823;
constexpr ll LINF = ll(4661686018427387903);
constexpr ll MOD = 1e9 + 7;
constexpr ll mod = 998244353;
constexpr long double eps = 1e-14;
const long double PI = acosl(-1.0L);
using namespace std;
void scans(string& str) {
char c;
str = "";
scanf("%c", &c);
if (c == '\n')scanf("%c", &c);
while (c != '\n' && c != -1 && c != ' ') {
str += c;
scanf("%c", &c);
}
}
void scanc(char& str) {
char c;
scanf("%c", &c);
if (c == -1)return;
while (c == '\n') {
scanf("%c", &c);
}
str = c;
}
double acot(double x) {
return PI / 2 - atan(x);
}
ll LSB(ll n) { return (n & (-n)); }
template<typename T>
inline T chmin(T& a, const T& b) {
if (a > b)a = b;
return a;
}
template<typename T>
inline T chmax(T& a, const T& b) {
if (a < b)a = b;
return a;
}
//cpp_int
#if __has_include(<boost/multiprecision/cpp_int.hpp>)
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
using namespace boost::multiprecision;
#else
using cpp_int = ll;
#endif
//atcoder library
#if __has_include(<atcoder/all>)
#include <atcoder/all>
//using namespace atcoder;
#endif
/*
random_device seed_gen;
mt19937 engine(seed_gen());
uniform_int_distribution dist(1, 100);
*/
/*----------------------------------------------------------------------------------*/
/* Rerooting: 全方位木 DP
問題ごとに以下を書き換える
- 型DPと単位元
- 型DPに対する二項演算 merge
- まとめたDPを用いて新たな部分木のDPを計算する add_root
計算量: O(N)
*/
struct Rerooting {
/* start 問題ごとに書き換え */
struct DP { // DP の型
ll dp0, dp1;
DP(ll dp0_, ll dp1_) : dp0(dp0_), dp1(dp1_) {}
};
const DP identity = DP(0, 0); // 単位元(末端の値は add_root(identity) になるので注意)
function<DP(DP, DP)> merge = [](DP dp_cum, DP d) -> DP {
return DP(dp_cum.dp0 + d.dp0, dp_cum.dp1 + max(d.dp0, d.dp1));
};
function<DP(DP)> add_root = [](DP d) -> DP {
return DP(max(d.dp0, d.dp1), d.dp0 + 1);
};
/* end 問題ごとに書き換え */
// グラフの定義
struct Edge {
int to;
};
using Graph = vector<vector<Edge>>;
vector<vector<DP>> dp; // dp[v][i]: vから出るi番目の有向辺に対応する部分木のDP
vector<DP> ans; // ans[v]: 頂点vを根とする木の答え
Graph G;
Rerooting(int N) : G(N) {
dp.resize(N);
ans.assign(N, identity);
}
void add_edge(int a, int b) {
G[a].push_back({ b });
}
void build() {
dfs(0); // 普通に木DP
bfs(0, identity); // 残りの部分木に対応するDPを計算
}
DP dfs(int v, int p = -1) { // 頂点v, 親p
DP dp_cum = identity;
int deg = G[v].size();
dp[v] = vector<DP>(deg, identity);
for (int i = 0; i < deg; i++) {
int u = G[v][i].to;
if (u == p) continue;
dp[v][i] = dfs(u, v);
dp_cum = merge(dp_cum, dp[v][i]);
}
return add_root(dp_cum);
}
void bfs(int v, const DP& dp_p, int p = -1) { // bfs だが、実装が楽なので中身は dfs になっている
int deg = G[v].size();
for (int i = 0; i < deg; i++) { // 前のbfsで計算した有向辺に対応する部分木のDPを保存
if (G[v][i].to == p) dp[v][i] = dp_p;
}
vector<DP> dp_l(deg + 1, identity), dp_r(deg + 1, identity); // 累積merge
for (int i = 0; i < deg; i++) {
dp_l[i + 1] = merge(dp_l[i], dp[v][i]);
}
for (int i = deg - 1; i >= 0; i--) {
dp_r[i] = merge(dp_r[i + 1], dp[v][i]);
}
ans[v] = add_root(dp_l[deg]); // 頂点 v の答え
for (int i = 0; i < deg; i++) { // 一つ隣の頂点に対しても同様に計算
int u = G[v][i].to;
if (u == p) continue;
bfs(u, add_root(merge(dp_l[i], dp_r[i + 1])), v);
}
}
};
int main() {
int n;
scanf("%d", &n);
Rerooting reroot(n);
rep(i, n - 1) {
int a, b;
scanf("%d %d", &a, &b);
a--, b--;
reroot.add_edge(a, b);
reroot.add_edge(b, a);
}
reroot.build();
ll ans = LINF;
rep(i, n) {
chmin(ans, reroot.ans[i].dp1);
}
printf("%lld\n", ans);
Please AC;
}
null