結果

問題 No.1718 Random Squirrel
ユーザー kwm_tkwm_t
提出日時 2021-10-22 22:54:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 106 ms / 2,000 ms
コード長 3,197 bytes
コンパイル時間 4,244 ms
コンパイル使用メモリ 241,784 KB
実行使用メモリ 60,236 KB
最終ジャッジ日時 2023-10-24 14:19:06
合計ジャッジ時間 6,831 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 52 ms
9,284 KB
testcase_12 AC 11 ms
4,392 KB
testcase_13 AC 34 ms
6,908 KB
testcase_14 AC 54 ms
9,548 KB
testcase_15 AC 51 ms
8,492 KB
testcase_16 AC 24 ms
5,852 KB
testcase_17 AC 49 ms
9,020 KB
testcase_18 AC 71 ms
10,340 KB
testcase_19 AC 48 ms
8,228 KB
testcase_20 AC 20 ms
5,588 KB
testcase_21 AC 78 ms
11,924 KB
testcase_22 AC 87 ms
11,924 KB
testcase_23 AC 77 ms
11,924 KB
testcase_24 AC 77 ms
11,924 KB
testcase_25 AC 86 ms
11,924 KB
testcase_26 AC 52 ms
17,744 KB
testcase_27 AC 51 ms
18,292 KB
testcase_28 AC 52 ms
17,744 KB
testcase_29 AC 104 ms
60,236 KB
testcase_30 AC 103 ms
60,236 KB
testcase_31 AC 106 ms
60,236 KB
testcase_32 AC 105 ms
60,236 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using mint = modint1000000007;
//const int mod = 1000000007;
//using mint = modint998244353;
//const int mod = 998244353;
//const int INF = 1e9;
//const long long LINF = 1e18;
//const bool debug = false;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i,l,r)for(int i=(l);i<(r);++i)
#define rrep(i, n) for (int i = (n-1); i >= 0; --i)
#define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i)
#define all(x) (x).begin(),(x).end()
#define allR(x) (x).rbegin(),(x).rend()
#define endl "\n"
#define P pair<long long,long long>
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
struct ReRooting {
	struct Edge {
		int to;
		Edge(int _to) :to(_to) {}
	};
	struct Data {
		long long sum;
		long long maxd;
		Data() :sum(0), maxd(0) {}
		Data(long long _sum, long long _maxd) :sum(_sum), maxd(_maxd) {}
	};
	vector<vector<Edge>>g;
	vector<Data>dp;
	vector<Data>ans;
	vector<bool>pos;
	int sz, k;
	ReRooting(int _sz) :sz(_sz) {
		g.resize(sz);
		dp.reserve(sz);
		ans.resize(sz);
		pos.resize(sz);
	}
	void ReadGraph() {
		cin >> k;
		rep(i, sz - 1) {
			int a, b; cin >> a >> b; a--; b--;
			g[a].push_back({ b });
			g[b].push_back({ a });
		}
		rep(i, k) {
			int d; cin >> d; d--;
			pos[d] = true;
		}
	}
	Data  merge(const Data &lh, const Data &rh, int v) {
		Data ret = lh;
		if ((0 != rh.sum) || (pos[v]))ret.sum += rh.sum + 2;
		if (0 != rh.maxd)chmax(ret.maxd, rh.maxd + 1);
		if (pos[v])chmax(ret.maxd, 1);
		return ret;
	}
	Data  mergeSub(const Data &lh, const Data &rh) {//★★
		Data ret = lh;
		ret.sum += rh.sum;
		chmax(ret.maxd, rh.maxd);
		return ret;
	}
	void dfs(int v, int p = -1) {
		Data val;
		for (auto e : g[v]) {
			if (p == e.to)continue;
			dfs(e.to, v);
			Data tmp = dp[e.to];
			val = merge(val, tmp, e.to);
		}
		dp[v] = val;
	}
	void dfsR(int v, int p = -1) {
		ans[v] = dp[v];
		if (-1 != p)ans[v] = merge(ans[v], dp[p], p);
		vector<Data>vdata;
		vector<int>vpos;
		for (auto e : g[v]) vdata.push_back(dp[e.to]);
		for (auto e : g[v]) vpos.push_back(e.to);
		int sz = vdata.size();
		vector<Data>sumL(sz + 1), sumR(sz + 1);
		rep(i, sz) sumL[i + 1] = merge(sumL[i], vdata[i], vpos[i]);
		rep(i, sz) sumR[i + 1] = merge(sumR[i], vdata[sz - i - 1], vpos[sz - i - 1]);
		/*rep(i, sumL.size()) {
			cout << sumL[i].sum << " " << sumL[i].maxd << endl;
		}
		cout << endl;
		rep(i, sumR.size()) {
			cout << sumR[i].sum << " " << sumR[i].maxd << endl;
		}
		cout << endl;*/
		for (int i = 0; i < g[v].size(); ++i) {
			auto e = g[v][i];
			if (p == e.to)continue;
			dp[v] = mergeSub(sumL[i], sumR[sz - (i + 1)]);
			dfsR(e.to, v);
		}
	}
	void output() {
		rep(i, sz)cout << ans[i].sum - ans[i].maxd << endl;
	}
	void solve() {
		ReadGraph();
		dfs(0);
		//rep(i, sz) cout << dp[i].sum << " " << dp[i].maxd << endl;
		dfsR(0);
		output();
	}
};
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n; cin >> n;
	ReRooting reroot(n);
	reroot.solve();
	return 0;
}
0