結果

問題 No.1484 木に数を書き込む問題 / Just Write Numbers! 2
ユーザー warabi0906warabi0906
提出日時 2023-02-10 13:08:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 247 ms / 2,000 ms
コード長 3,901 bytes
コンパイル時間 4,206 ms
コンパイル使用メモリ 239,396 KB
実行使用メモリ 29,604 KB
最終ジャッジ日時 2023-09-21 15:39:13
合計ジャッジ時間 10,492 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 4 ms
4,380 KB
testcase_05 AC 12 ms
4,376 KB
testcase_06 AC 13 ms
4,408 KB
testcase_07 AC 8 ms
4,376 KB
testcase_08 AC 16 ms
4,728 KB
testcase_09 AC 13 ms
4,516 KB
testcase_10 AC 13 ms
4,380 KB
testcase_11 AC 11 ms
4,380 KB
testcase_12 AC 10 ms
4,380 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 139 ms
14,832 KB
testcase_15 AC 132 ms
14,404 KB
testcase_16 AC 122 ms
13,432 KB
testcase_17 AC 87 ms
10,944 KB
testcase_18 AC 155 ms
15,952 KB
testcase_19 AC 184 ms
18,048 KB
testcase_20 AC 180 ms
18,100 KB
testcase_21 AC 247 ms
18,060 KB
testcase_22 AC 184 ms
18,120 KB
testcase_23 AC 182 ms
18,328 KB
testcase_24 AC 184 ms
18,012 KB
testcase_25 AC 189 ms
18,316 KB
testcase_26 AC 188 ms
18,112 KB
testcase_27 AC 189 ms
18,108 KB
testcase_28 AC 183 ms
18,116 KB
testcase_29 AC 154 ms
29,604 KB
testcase_30 AC 168 ms
22,100 KB
testcase_31 AC 108 ms
16,992 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include "atcoder/all"
#define debug cout << "OK" << endl;
template<typename T>
inline bool chmax(T& a, const T b) { if (a < b) { a = b; return true; } return false; }
template<typename T>
inline bool chmin(T& a, const T b) { if (a > b) { a = b; return true; } return false; }
inline int Code(char c) {
	if ('A' <= c and c <= 'Z')return (int)(c - 'A');
	if ('a' <= c and c <= 'z')return (int)(c - 'a');
	if ('0' <= c and c <= '9')return (int)(c - '0');
	assert(false);
}
using namespace std;
using namespace atcoder;
#define int long long
constexpr int MOD = 998244353;
constexpr int INF = (1LL << 63);
using minit = modint998244353;

template<typename T>
ostream& operator << (ostream& st, const vector<T>& v) {
	for (const T value : v) {
		st << value << " ";
	}
	return st;
}
template<typename T>
istream& operator >> (istream& st, vector<T>& v) {
	for (T& value : v) {
		st >> value;
	}
	return st;
}

struct edge {
	int to, cost;
};

//
class Tree {
private:
	int N;
	bool LCA_inited;
	vector<vector<edge>> G;
	vector<vector<int>> parent;
	vector<int> dist;
	vector<int> distC;
	void dfs(long long v, long long p, long long d, long long d2);
	void LCAinit();
	void D_dfs(int v, int p, int d);
public:
	Tree(int n);
	~Tree();
	int max_dist;
	int s, t;
	vector<vector<edge>> Get_Graph();
	void add_edge(int u, int v, int c);
	void diameter();
	int lca(int u, int v);
	int get_dist(int u, int v);
	bool on_path(int u, int v, int a);
};

Tree::Tree(int N) {
	G.resize(N);
	LCA_inited = false;
	s = -1, t = -1, max_dist = -1;
	return;
}
Tree::~Tree() {}
vector<vector<edge>> Tree::Get_Graph() {
	return G;
}
void Tree::add_edge(int u, int v,int c) {
	G[v].push_back({ u,c });
	G[u].push_back({ v,c });
	return;
}
void Tree::LCAinit() {
	parent.assign(33, vector<long long>(N, -1LL));
	dist.assign(N, -1LL);
	distC.assign(N, -1LL);
	dfs(0LL, -1LL, 0LL, 0LL);
	for (int i = 0; i < 32; i++) {
		for (int j = 0; j < N; j++) {
			if (parent[i][j] < 0LL) {
				parent[i + 1][j] = -1LL;
			}
			else {
				parent[i + 1][j] = parent[i][parent[i][j]];
			}
		}
	}
	return;
}
void Tree::diameter() {
	D_dfs(0, -1, 0);
	s = t;
	D_dfs(s, -1, 0);
	return;
}
void Tree::D_dfs(int v, int p, int d) {
	if (max_dist < d) {
		max_dist = d;
		t = v;
	}
	for (const edge e : G[v]) {
		if (e.to == p)continue;
		D_dfs(e.to, v, d + e.cost);
	}
	return;
}
void Tree::dfs(int v, int p, int d, int d2) {
	parent[0][v] = p;
	dist[v] = d;
	distC[v] = d2;
	for (edge e : G[v]) {
		if (e.to == p)continue;
		dfs(e.to, v, d + 1, d2 + e.cost);
	}
	return;
}
int Tree::lca(int u, int v) {
	if (!LCA_inited)LCAinit(), LCA_inited = true;
	if (dist[u] < dist[v])swap(u, v);
	long long between = dist[u] - dist[v];
	for (int i = 0; i < 33; i++) {
		if (between & (1LL << i)) { u = parent[i][u]; }
	}
	if (u == v)return u;
	for (int i = 32; i >= 0; i--) {
		if (parent[i][u] != parent[i][v]) {
			u = parent[i][u];
			v = parent[i][v];
		}
	}
	assert(parent[0][u] == parent[0][v]);
	return parent[0][u];
}
int Tree::get_dist(int u, int v) {
	return distC[u] + distC[v] - 2 * distC[lca(u, v)];
}
bool Tree::on_path(int u, int v, int a) {
	return get_dist(u, v) == get_dist(u, a) + get_dist(v, a);
}
//

class Solver {
public:
	int T;
	Solver() :T(1) {}
	~Solver(){}
	void multi();
	void solve() const;
	void sp(int x)const;
};
void Solver::multi() {
	cin >> T;
	return;
}
void Solver::sp(const int x)const {
	cout << fixed << setprecision(x) << endl;
	return;
}

void Solver::solve() const {
	int N;
	cin >> N;
	Tree tree(N);
	for (int i = 0; i < N - 1; i++) {
		int u, v;
		cin >> u >> v;
		u--; v--;
		tree.add_edge(u, v, 1);
	}
	tree.diameter();
	cout << 2 * (N - 1) - tree.max_dist << endl;
}

signed main() {
	Solver solver;
	//solver.multi();
	//solver.sp();
	for (int i = 0; i < solver.T; i++)
		solver.solve();
	return 0;
}

/*
* わらびのコードこーどすぺーす
 (σ・∀・)σゲッツ!!
*/
0