QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#201872#5515. Cat Exercisejmyszka#Compile Error//C++172.4kb2023-10-05 17:14:202024-07-04 02:16:48

Judging History

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

  • [2024-07-04 02:16:48]
  • 评测
  • [2023-10-05 17:14:20]
  • 提交

answer

#include <bits/stdc++.h>
#include <fstream>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template<class A, class B>
ostream& operator<<(ostream& o, const pair<A, B>& p) {return o << '(' << p.first << ", " << p.second << ')';}
template<size_t Index = 0, typename... Types>
ostream& printTupleElements(ostream& o, const tuple<Types...>& t) {if constexpr (Index < sizeof...(Types)){if(Index > 0){o << ", ";}o << get<Index>(t);printTupleElements<Index + 1>(o, t);}return o;}
template<typename... Types>
ostream& operator<<(ostream& o, const tuple<Types...>& t){o << "(";printTupleElements(o, t);return o << ")";}
template<class T>
auto operator<<(ostream& o, const T& x) -> decltype(x.end(), o){o << '{';bool first = true;for (const auto& e : x){if (!first){o << ", ";}o << e;first = false;} return o << '}';}
#define DEBUG
#ifdef DEBUG
#define fastio()
#define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n'
#define check(x) if (!(x)) { cerr << "Check failed: " << #x << " in line " << __LINE__ << endl; exit(1); }
#else
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0);
#define debug(...)
#define check(x) 
#endif
typedef long long ll;
#define pi pair<int, int>
#define pll pair<ll, ll>
#define st first
#define nd second
#define vi vector<int>
#define vll vector<ll>
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
constexpr int LIM = 2e5;
int dist[LIM + 10];
vi g[LIM + 10];
int tab[LIM + 10];
ll dp[LIM + 10];
void dfs(int v, int o, int r) {
	for(auto x : g[v]) {
		if(x != o) {
			if(tab[x] < tab[r]) {
				dist[x] = dist[v] + 1;
				dfs(x, v, r);
			}
		}
	}
}
void solve() {
	//ifstream cin("nazwa.in");
	//ofstream cout("nazwa.out");
	int n;
	cin >> n;
	vector<pi>vs;
	for(int i = 1; i <= n; i++) {
		cin >> tab[i];
		vs.eb(tab[i], i);
	}
	sort(all(vs));
	for(int i = 1; i < n; i++) {
		int a, b;
		cin >> a >> b;
		g[a].eb(b);
		g[b].eb(a);
	}
	for(auto [val, v] : vs) {
		for(int j = 1; j <= n; j++) {
			dist[j] = -1e9;
		}
		dist[v] = 0;
		dfs(v, v, v);
		for(int j = 1; j <= n; j++) {
			dp[v] = max(dp[v], dp[j] + dist[j]);
		}
		if(val == n) {
			cout << dp[v] << '\n';
			return;
		}
	}
}
int main() {
	fastio();
	int t = 1;
	//cin >> t;
	while(t--) {
		solve();
	}
}

详细

This language is not supported yet.