QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#357852#3047. Wind of Changeorz_zTL 0ms0kbC++146.2kb2024-03-19 13:55:082024-03-19 13:55:08

Judging History

你现在查看的是最新测评结果

  • [2024-03-19 13:55:08]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-03-19 13:55:08]
  • 提交

answer

//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
//#pragma GCC optimize("Ofast,fast-math")
//#pragma GCC target("avx,avx2")
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef double db;
#define F(i, a, b) for(int i = a; i <= (b); ++i)
#define F2(i, a, b) for(int i = a; i < (b); ++i)
#define dF(i, a, b) for(int i = a; i >= (b); --i)
template<typename T> void debug(string s, T x) {
	cerr << "[" << s << "] = [" << x << "]\n";
}
template<typename T, typename... Args> void debug(string s, T x, Args... args) {
	for (int i = 0, b = 0; i < (int)s.size(); i++) if (s[i] == '(' || s[i] == '{') b++;
	else if (s[i] == ')' || s[i] == '}') b--;
	else if (s[i] == ',' && b == 0) {
		cerr << "[" << s.substr(0, i) << "] = [" << x << "] | ";
		debug(s.substr(s.find_first_not_of(' ', i + 1)), args...);
		break;
	}
}
#ifdef ONLINE_JUDGE
#define Debug(...)
#else
#define Debug(...) debug(#__VA_ARGS__, __VA_ARGS__)
#endif
#define pb push_back
#define fi first
#define se second
#define Mry fprintf(stderr, "%.3lf MB\n", (&Med - &Mbe) / 1048576.0)
#define Try cerr << 1e3 * clock() / CLOCKS_PER_SEC << " ms\n";
typedef long long ll;
// namespace Fread {const int SIZE = 1 << 17; char buf[SIZE], *S, *T; inline char getchar() {if (S == T) {T = (S = buf) + fread(buf, 1, SIZE, stdin); if (S == T) return '\n';} return *S++;}}
// namespace Fwrite {const int SIZE = 1 << 17; char buf[SIZE], *S = buf, *T = buf + SIZE; inline void flush() {fwrite(buf, 1, S - buf, stdout), S = buf;} inline void putchar(char c) {*S++ = c;if (S == T) flush();} struct NTR {~NTR() {flush();}} ztr;}
// #ifdef ONLINE_JUDGE
// #define getchar Fread::getchar
// #define putchar Fwrite::putchar
// #endif
inline int ri() {
	int x = 0;
	bool t = 0;
	char c = getchar();
	while (c < '0' || c > '9') t |= c == '-', c = getchar();
	while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
	return t ? -x : x;
}
inline void wi(int x) {
	if (x < 0) {
		putchar('-'), x = -x;
	}
	if (x > 9) wi(x / 10);
	putchar(x % 10 + 48);
}
inline void wi(int x, char s) {
	wi(x), putchar(s);
}
bool Mbe;
// mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
const int mod = 998244353;
//const int inf = 0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3f;
const int _ = 2.5e5 + 5;

int n, m;

struct tree1;
tree1 *C, *D;
struct tree1 {
	vi d[_], dw[_], g[_], gw[_];
	int dep[_], sz[_], Fa[_], hson[_], top[_];
	void dfs1(int u) {
		sz[u] = 1;
		int len = d[u].size();
		F(i, 0, len - 1) {
			int v = d[u][i];
			if(!sz[v]) {
				dep[v] = dep[u] + dw[u][i];
				dfs1(v);
				Fa[v] = u;
				sz[u] += sz[v];
				if(sz[hson[u]] < sz[v]) hson[u] = v;
			}
		}
	}
	int cnt, dfn[_];
	void dfs2(int u, int tf) {
		top[u] = tf, dfn[u] = ++cnt;
		if(hson[u]) dfs2(hson[u], tf);
		for(int v :d[u]) if(!top[v]) dfs2(v, v);
	}
	int LCA(int u, int v) {
		while(top[u] != top[v]) {
			if(dep[top[u]] < dep[top[v]]) swap(u, v);
			u = Fa[top[u]];
		}
		return dep[u] < dep[v] ? u : v;
	}
	int Dis(int u, int v) {
		return dep[u] + dep[v] - 2ll * dep[LCA(u, v)];
	}
	int ans[_];
	void init() {
		dfs1(1), dfs2(1, 1);
		F(i, 1, n) ans[i] = infll;
	}
	bool vis[_];
	int sum, rt;
	void get_sz(int u, int fa) {
		sz[u] = 1;
		for(int v : d[u]) if(v != fa && !vis[v]) {
			get_sz(v, u);
			sz[u] += sz[v];
		}
	}
	void get_rt2(int u, int fa) {
		int mx = 0;
		for(int v : d[u]) if(v != fa && !vis[v]) {
			get_rt2(v, u);
			mx = max(mx, sz[v]);
		}
		mx = max(mx, sum - sz[u]);
		if(mx <= sum / 2) {
			rt = u;
		}
	}
	int get_rt(int u, int sm) {
		sum = sm;
		rt = 0;
		get_rt2(u, 0);
		return rt;
	}
	
	void build(vi &a) {
		for(int v : a) g[v].clear(), gw[v].clear();
		sort(a.begin(), a.end(), [&](int u, int v) { return dfn[u] < dfn[v]; });
		int len = a.size();
		F(i, 0, len - 2) a.pb(LCA(a[i], a[i + 1]));
		if(len >= 1) a.pb(LCA(a[len - 1], a[0]));
		sort(a.begin(), a.end(), [&](int u, int v) { return dfn[u] < dfn[v]; });
		a.resize(unique(a.begin(), a.end()) - a.begin());
		len = a.size();
		F(i, 1, len - 1) {
			int lca = LCA(a[i - 1], a[i]);
			g[lca].pb(a[i]);
//			gw[lca].pb(Dis(lca, a[i]));
		}
	}
	int Mn[_];
	int dp[_];
	void pre_dfs(int u, int fa) {
		Mn[u] = dp[u];
		for(int v : g[u]) if(v != fa) {
			pre_dfs(v, u);
			Mn[u] = min(Mn[u], Mn[v]);
		}
//		Debug(u, Mn[u]);
	}
	void dfs(int u, int fa, int Val) {
		pii mn = make_pair(infll, infll);
		for(int v : g[u]) if(v != fa) {
			if(Mn[v] < mn.fi) mn.se = mn.fi, mn.fi = Mn[v];
			else if(mn.se > Mn[v]) mn.se = Mn[v];
		}
		ans[u] = min(ans[u], min(Val, mn.fi - 2 * dep[u]) + dp[u]);
		if(dp[u] < mn.fi) mn.se = mn.fi, mn.fi = dp[u];
		else if(dp[u] < mn.se) mn.se = dp[u];
		for(int v : g[u]) if(v != fa) {
			if(Mn[v] == mn.fi) dfs(v, u, min(Val, mn.se - 2ll * dep[u]));
			else dfs(v, u, min(Val, mn.fi - 2ll * dep[u]));
		}
	}
	void work(vi &a) {
//		for(int v : a) Debug(v);
//		Debug("end");
		pre_dfs(a[0], 0), dfs(a[0], 0, infll);
	}
	
	vi A;
	void get_dian(int u, int fa) {
		A.pb(u);
		for(int v : d[u]) if(v != fa && !vis[v]) {
			get_dian(v, u);
		}
	}
	void get_dep(int u, int fa) {
		int len = d[u].size();
		F(i, 0, len - 1) {
			int v = d[u][i];
			if(v != fa && !vis[v]) {
				D->dp[v] = D->dp[u] + dw[u][i];
				get_dep(v, u);
			}
		}
		D->dp[u] += D->dep[u];
	}
	void solve(int u) {
		vis[u] = 1;
		A.clear();
		get_dian(u, 0);
		D->build(A);
		for(int v : A) D->dp[v] = infll;
		D->dp[u] = 0;
		get_dep(u, 0);
		D->work(A);
		for(int v : d[u]) if(!vis[v]) {
			get_sz(v, u);
			solve(get_rt(v, sz[v]));
		}
	}
} A, B;
bool Med;
signed main() {
	// Mry;
	n = ri();
	F(i, 1, n - 1) {
		int u = ri(), v = ri(), w = ri();
		A.d[u].pb(v);
		A.dw[u].pb(w);
		A.d[v].pb(u);
		A.dw[v].pb(w);
	}
	F(i, 1, n - 1) {
		int u = ri(), v = ri(), w = ri();
		B.d[u].pb(v);
		B.dw[u].pb(w);
		B.d[v].pb(u);
		B.dw[v].pb(w);
	}
	A.init(), B.init();
	C = &A, D = &B;
	A.get_sz(1, 0);
	A.solve(A.get_rt(1, n));
	F(i, 1, n) printf("%lld\n", B.ans[i]);
	// Try;
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Time Limit Exceeded

input:

250000
137466 116241 624409157
188961 163586 148491970
13958 122239 46409636
186120 44010 919858866
72320 102560 92679302
158544 236582 882613876
22331 114267 646741536
210782 42861 679450428
56693 45397 898958944
71188 114724 904191407
15203 225401 210626781
31071 144225 278121829
110354 83972 4557...

output:


result: