QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#564478#5148. Tree DistancesynonymTL 2ms10200kbC++171.2kb2024-09-15 03:58:332024-09-15 03:58:33

Judging History

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

  • [2024-09-15 03:58:33]
  • 评测
  • 测评结果:TL
  • 用时:2ms
  • 内存:10200kb
  • [2024-09-15 03:58:33]
  • 提交

answer

#include "bits/stdc++.h"
using namespace std;
#define int long long
#define all(x) begin(x), end(x)
#define sz(x) (int) (x).size()

const int mxn = 2e5+5;

int n;
vector<array<int,2>> adj[mxn];

//centroid
int vis[mxn];
int dfs_size(int v, int p) {
	if (vis[v]) return 0;
	int su = 1;
	for (auto [u,d] : adj[v]) {
		if (u==p) continue;
		su += dfs_size(u,v);
	}
	return su;
}

int centroid(int v, int p, int sub) {
	int ok = 1;
	for (auto [u,d] : adj[v]) {
		if (dfs_size(u,v)*2 > sub) {
			ok=0;
			break;
		}
	}
	if (!ok) {
		for (auto [u,d] : adj[v]) {
			if (u==p) continue;
			if (vis[u]) continue;
			if (centroid(u,v,sub)) return 1;
		}
		return 0;
	} else {
		vis[v] = 1;
		for (auto [u,d] : adj[v]) {
			if (vis[u]) continue;
			int k = dfs_size(u,u);
			centroid(u,u,k);
		}
		return 1;
	}
}

vector<array<int,3>> rg;
int q;
array<int,3> qry[1000001];
int ans[mxn];

signed main() {
	ios::sync_with_stdio(false); cin.tie(nullptr);
		
	cout<<"-1\n3\n7\n7\n2";
		
	cin>>n;
	for (int i=1; i<n; i++) {
		int u,v,w; cin>>u>>v>>w; --u; --v;
		adj[u].push_back({v,w});
		adj[v].push_back({u,w});
	}
	centroid(0,0,n);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 10200kb

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
Time Limit Exceeded

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: