QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#413887#5148. Tree DistanceFAKUMARERRE 0ms23008kbC++144.0kb2024-05-18 11:25:292024-05-18 11:25:30

Judging History

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

  • [2024-05-18 11:25:30]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:23008kb
  • [2024-05-18 11:25:29]
  • 提交

answer

#include <bits/stdc++.h>

const int MAXN = 200005;
const int MAXM = 1000005;

using namespace std;

struct Edge {
	int to, nxt, w;
} E[MAXN << 1];

int H[MAXN], F[MAXN], tot;

inline void Add_Edge (const int u, const int v, const int w) {
	E[++ tot] = {v, H[u], w}, H[u] = tot;
	E[++ tot] = {u, H[v], w}, H[v] = tot;
}

bool Vis[MAXN];
int Siz[MAXN], Max[MAXN];
long long Ans[MAXN];
int rt, Now;

inline void DFS1 (const int x, const int f) {
	Siz[x] = 1, Max[x] = 0;
	for (int i = H[x]; i; i = E[i].nxt)
		if (E[i].to != f && !Vis[E[i].to])
			DFS1 (E[i].to, x), Siz[x] += Siz[E[i].to], Max[x] = max (Max[x], Siz[E[i].to]);
	Max[x] = max (Max[x], Now - Siz[x]);
	if (Max[x] < Max[rt] || !rt) rt = x;
}

struct Node {
	int x;
	long long dis;
	
	inline bool operator < (const Node &a) const {
		return x < a.x;
	}
};

struct Ques {
	int l, id;
};

vector <int > P[MAXN];
vector <Ques> I[MAXN];
vector <Node> S;

inline void DFS2 (const int x, const int f, const long long dis) {
	S.push_back ({x, dis});
	for (int i = H[x]; i; i = E[i].nxt) 
		if (E[i].to != f && !Vis[E[i].to])
			DFS2 (E[i].to, x, dis + E[i].w);
}

Node Stack[MAXN];
int top = 0;

inline void DFS3 (const int x, const int f) {
	Vis[x] = 1, S.clear ();
	// for (int i = H[x]; i; i = E[i].nxt)
		// if (E[i].to != f && !Vis[E[i].to])
	DFS2 (x, f, 0);
	
	sort (S.begin (), S.end ()), top = 0;
	
	for (int i = 0; i < (int) S.size (); ++ i) {
		while (top && S[i].dis <= Stack[top].dis) -- top;
		if (top) P[S[i].x].push_back (Stack[top].x);
		Stack[++ top] = S[i];
	}
	
	top = 0;
	
	for (int i = (int) S.size () - 1;  ~ i; -- i) {
		while (top && S[i].dis <= Stack[top].dis) -- top;
		if (top) P[Stack[top].x].push_back (S[i].x);
		Stack[++ top] = S[i];
	}
	
	for (int i = H[x]; i; i = E[i].nxt)
		if (E[i].to != f && !Vis[E[i].to])
			Now = Siz[E[i].to], rt = 0, DFS1 (E[i].to, x), DFS3 (rt, x);
}

int N, Q;
int u, v, w;

namespace Tree_Cut {
	int Son[MAXN], Top[MAXN], Dep[MAXN];
	long long Dis[MAXM];
	
	inline void DFS_1 (const int x, const int f, const long long dis = 0) {
		Dep[x] = Dep[F[x] = f] + 1, Siz[x] = 1, Dis[x] = dis;
		for (int i = H[x]; i; i = E[i].nxt)
			if (E[i].to != f)
				DFS_1 (E[i].to, x, dis + E[i].w), Siz[x] += Siz[E[i].to], Siz[E[i].to] > Siz[Son[x]] ? Son[x] = E[i].to : Son[x];
	}
	
	inline void DFS_2 (const int x, const int top) {
		Top[x] = top;
		if (Son[x]) DFS_2 (Son[x], top);
		for (int i = H[x]; i; i = E[i].nxt)
			if (E[i].to != F[x] && E[i].to != Son[x])
				DFS_2 (E[i].to, E[i].to);
	}
	
	inline int LCA (int u, int v) {
		while (Top[u] != Top[v])
			Dep[Top[u]] > Dep[Top[v]] ? u = F[Top[u]] : v = F[Top[v]];
		return Dep[u] > Dep[v] ? v : u; 
	}
	
	
	inline long long GetDis (const int u, const int v) {
		return Dis[u] + Dis[v] - (Dis[LCA (u, v)] << 1);
	}
}

namespace BIT {
	long long T[MAXN];
	
	#define lowbit(x) (x & -x)
	
	inline void Build () {
		memset (T, 127, sizeof T);
	}
	
	inline void Add (int x, const long long v) {
		while (x) T[x] = min (T[x], v), x -= lowbit (x); 
	}
	
	inline long long Que (int x) {
		long long Ret = (1ll << 62);
		while (x <= N) Ret = min (Ret, T[x]), x += lowbit (x);
		return Ret;
	}
}

using namespace Tree_Cut;

int main () {
	
	cin >> N, Now = N;
	
	for (int i = 2; i <= N; ++ i)
		cin >> u >> v >> w, Add_Edge (u, v, w);
		
	DFS1 (1, 0), DFS3 (1, 0);
	
	DFS_1 (1, 0), DFS_2 (1, 1);
	
//	cerr << "N = " << N << '\n';
	
//	for (int i = 1; i <= N; ++ i) {
//		cerr << "R = " << i << '\n';
//		for (auto p : P[i]) cerr << "L = " << p << ", Dis = " << GetDis (p, i) << '\n';
//	}
	
	cin >> Q, BIT::Build ();
	
	for (int i = 1; i <= Q; ++ i)
		cin >> u >> v, I[v].push_back ({u, i});
	
	for (int i = 1; i <= N; ++ i) {
		for (auto p : P[i]) if (p != i) BIT::Add (p, GetDis (i, p));
		for (auto p : I[i]) Ans[p.id] = BIT::Que (p.l);
	}
	
	for (int i = 1; i <= Q; ++ i)	
		cout << (Ans[i] == (1ll << 62) ? -1 : Ans[i]) << '\n';
	
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 23008kb

input:

5
1 2 5
1 3 3
1 4 4
3 5 2
5
1 1
1 4
2 4
3 4
2 5

output:

-1
3
7
7
2

result:

ok 5 number(s): "-1 3 7 7 2"

Test #2:

score: -100
Runtime Error

input:

199999
31581 23211 322548833
176307 196803 690953895
34430 82902 340232856
36716 77480 466375266
7512 88480 197594480
95680 61864 679567992
19572 14126 599247796
188006 110716 817477802
160165 184035 722372640
23173 188594 490365246
54801 56250 304741654
10103 45884 643490340
127469 154479 214399361...

output:


result: