QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#309823#8134. LCA Countingucup-team027#WA 0ms3784kbC++231.9kb2024-01-20 21:04:242024-01-20 21:04:24

Judging History

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

  • [2024-01-20 21:04:24]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3784kb
  • [2024-01-20 21:04:24]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<T>>;

template<class T>
struct RMQ {
	vector<vector<T>> jmp;
	RMQ(const vector<T>& V) : jmp(1, V) {
		for (int pw = 1, k = 1; pw * 2 <= sz(V); pw *= 2, ++k) {
			jmp.emplace_back(sz(V) - pw * 2 + 1);
			rep(j,0,sz(jmp[k]))
				jmp[k][j] = min(jmp[k - 1][j], jmp[k - 1][j + pw]);
		}
	}
	T query(int a, int b) {
		assert(a < b); // or return inf if a == b
		int dep = 31 - __builtin_clz(b - a);
		return min(jmp[dep][a], jmp[dep][b - (1 << dep)]);
	}
};

struct LCA {
	int T = 0;
	vi time, path, ret;
	RMQ<int> rmq;

	LCA(vector<vi>& C) : time(sz(C)), rmq((dfs(C,0,-1), ret)) {}
	void dfs(vector<vi>& C, int v, int par) {
		time[v] = T++;
		for (int y : C[v]) if (y != par) {
			path.push_back(v), ret.push_back(time[v]);
			dfs(C, y, v);
		}
	}

	int lca(int a, int b) {
		if (a == b) return a;
		tie(a, b) = minmax(time[a], time[b]);
		return path[rmq.query(a, b)];
	}
	//dist(a,b){return depth[a] + depth[b] - 2*depth[lca(a,b)];}
};

signed main() {
	ios::sync_with_stdio(0); cin.tie(0);
	
	int n; cin >> n;
	vector<vector<int>> g(n);
	vector<int> isLf(n, 1);
	for (int i = 1; i < n; i++) {
		int p; cin >> p; p--;
		g[p].push_back(i);
		isLf[p] = 0;
	}

	LCA lca(g);
	vector<int> lfs;
	for (int i = 0; i < n; i++) {
		if (isLf[i]) lfs.push_back(i);
	}
	set<int> csc;
	for (int i = 1; i < lfs.size(); i++) {
		csc.insert(lca.lca(lfs[i-1], lfs[i]));
	}

	int ans = 0;
	for (int i = 1; i <= lfs.size(); i++) {
		cout << ans+i << ' ';
		if (csc.size()) {
			csc.erase(csc.begin());
			ans++;
		}
	}
	
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

7
1 1 2 4 2 2

output:

1 3 5 6 

result:

ok 4 number(s): "1 3 5 6"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3516kb

input:

10
1 1 2 2 1 1 1 2 4

output:

1 3 5 6 7 8 9 

result:

ok 7 numbers

Test #3:

score: -100
Wrong Answer
time: 0ms
memory: 3784kb

input:

10
1 2 2 4 5 6 1 2 4

output:

1 3 5 6 7 

result:

wrong answer 4th numbers differ - expected: '7', found: '6'