結果
| 問題 |
No.768 Tapris and Noel play the game on Treeone
|
| ユーザー |
はまやんはまやん
|
| 提出日時 | 2019-02-23 23:11:52 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 65 ms / 2,000 ms |
| コード長 | 6,017 bytes |
| コンパイル時間 | 1,968 ms |
| コンパイル使用メモリ | 179,592 KB |
| 実行使用メモリ | 9,960 KB |
| 最終ジャッジ日時 | 2024-12-14 19:21:22 |
| 合計ジャッジ時間 | 5,154 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 22 |
ソースコード
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
//#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
struct Reroot {
using T = bool;
T def = true;
T comp(T from, T to) {
if (to) return false;
return from;
}
int N; vector<vector<int>> E; vector<T> dp, res;
Reroot() { }
Reroot(int n) { N = n; E.clear(); E.resize(0); E.resize(n); }
void add(int a, int b) { E[a].push_back(b); E[b].push_back(a); }
vector<T> build() {
dp.resize(N);
res.resize(N);
dfs1(0);
dfs2(0);
return res;
}
void dfs1(int cu, int pa = -1) {
dp[cu] = def;
fore(to, E[cu]) if (to != pa) {
dfs1(to, cu);
dp[cu] = comp(dp[cu], dp[to]);
}
}
void dfs2(int cu, int pa = -1) {
res[cu] = def;
fore(to, E[cu]) res[cu] = comp(res[cu], dp[to]);
int cnt = 0;
fore(to, E[cu]) if (dp[to]) cnt++;
T bak = dp[cu];
fore(to, E[cu]) if (to != pa) {
if (dp[to]) cnt--;
if (cnt == 0) dp[cu] = true;
else dp[cu] = false;
dfs2(to, cu);
if (dp[to]) cnt++;
}
dp[cu] = bak;
/*int n = E[cu].size();
vector<T> lft(n), rht(n);
lft[0] = dp[E[cu][0]];
rep(i, 1, n) lft[i] = comp(lft[i - 1], dp[E[cu][i]]);
rht[n-1] = dp[E[cu][n-1]];
rrep(i, n-2, 0) rht[i] = comp(rht[i + 1], dp[E[cu][i]]);
T bak = dp[cu];
rep(i, 0, n) if (E[cu][i] != pa) {
if (n == 1) dp[cu] = def;
else if (i == 0) dp[cu] = rht[1];
else if (i == n - 1) dp[cu] = lft[n - 2];
else dp[cu] = comp(lft[i - 1], rht[i + 1]);
dfs2(E[cu][i], cu);
}
dp[cu] = bak;*/
}
};
template<class V> struct BIT {
BIT() {} // [L, R)
int NV;vector<V> bit;
BIT(int n){ init(n); }
void init(int n) { NV = 1; while (NV < n)NV *= 2; bit.resize(NV); clear(); }
V operator[](int e) { V s = 0; e++; while (e) s += bit[e - 1], e -= e&-e; return s; }
void add(int e, V v) { e++; while (e <= NV) bit[e - 1] += v, e += e&-e; }
int lower_bound(V val) {
V tv = 0; int i, ent = 0; for (i = NV - 1; i >= 0; i--)
if(tv+bit[ent+(1<<i)-1]<=val)tv+=bit[ent+(1<<i)-1],ent += (1 << i);return ent;
}
V get(int L, int R) {
assert(0 <= L); assert(R <= NV); assert(L <= R);
V res = 0; if(R) res += operator[](R - 1); if (L) res -= operator[](L - 1);return res;
}
void clear() { rep(i, 0, NV) bit[i] = 0; }
};
/*---------------------------------------------------------------------------------------------------
∧_∧
∧_∧ (´<_` ) Welcome to My Coding Space!
( ´_ゝ`) / ⌒i
/ \ | |
/ / ̄ ̄ ̄ ̄/ |
__(__ニつ/ _/ .| .|____
\/____/ (u ⊃
---------------------------------------------------------------------------------------------------*/
//---------------------------------------------------------------------------------------------------
/*void solve(TreeDiameter &td, HLDecomposition &hld, int N, vector<ll> &dist) {
rep(i, 0, N) {
ll d = -1;
if (td.dia % 2 == 0) {
chmax(d, (ll)hld.distance(i, td.leaf_even[0]));
chmax(d, (ll)hld.distance(i, td.leaf_even[1]));
}
else {
chmax(d, (ll)hld.distance(i, td.leaf_odd.first[0]));
chmax(d, (ll)hld.distance(i, td.leaf_odd.second[0]));
}
dist[i] = d;
}
}*/
//---------------------------------------------------------------------------------------------------
void _main() {
/*int N, M;
cin >> N;
TreeDiameter td1; td1.init(N);
HLDecomposition hld1; hld1.init(N);
rep(i, 0, N - 1) {
int a, b; cin >> a >> b;
a--; b--;
td1.add_edge(a, b);
hld1.add(a, b);
}
td1.build();
hld1.build();
cin >> M;
TreeDiameter td2; td2.init(M);
HLDecomposition hld2; hld2.init(M);
rep(i, 0, M - 1) {
int a, b; cin >> a >> b;
a--; b--;
td2.add_edge(a, b);
hld2.add(a, b);
}
td2.build();
hld2.build();
ll ma = -1;
chmax(ma, 1LL * td1.dia);
chmax(ma, 1LL * td2.dia);
vector<ll> dist1(N), dist2(M);
solve(td1, hld1, N, dist1);
solve(td2, hld2, M, dist2);
BIT<ll> bit1(101010), bit2(101010);
rep(i, 0, N) bit1.add(dist1[i], 1);
rep(i, 0, M) bit2.add(dist2[i], 1);
ll other = 0;
ll ans = 0;
rep(i, 0, N) {
ll ng = 0;
if(0 <= ma - dist1[i]) ng = bit2.get(0, ma - dist1[i]);
other += ng;
ans += dist1[i] * (M - ng);
}
rep(i, 0, M) {
ll ng = 0;
if (0 <= ma - dist2[i]) ng = bit1.get(0, ma - dist2[i]);
other += ng;
ans += dist2[i] * (N - ng);
}
ans += ma * other / 2;
ans += 1LL * (1LL * N * M - other / 2);
cout << ans << endl;*/
int N;
cin >> N;
Reroot rr(N);
rep(i, 0, N - 1) {
int a, b; cin >> a >> b;
a--; b--;
rr.add(a, b);
}
auto dp = rr.build();
vector<int> ans;
rep(i, 0, N) if (dp[i]) ans.push_back(i);
int n = ans.size();
printf("%d\n", n);
fore(i, ans) printf("%d\n", i + 1);
}
はまやんはまやん