提出 #73921933
ソースコード 拡げる
//BEST TEMPLATE EVER
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define nn '\n'
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((int)(x).size())
#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define rrep(i,a,b) for(int i=(a);i>(b);--i)
#define each(x,a) for(auto& x:a)
#define each2(x,y,a) for(auto& [x,y]:a)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define un_mp unordered_map
#define un_st unordered_set
#define popcnt __builtin_popcountll
#if defined(__GNUC__) || defined(__clang__)
#define AINLINE __attribute__((always_inline)) inline
#else
#define AINLINE inline
#endif
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vstr = vector<string>;
using vll = vector<ll>;
using i128 = __int128_t;
template<class Fun> class y_combinator_result {
Fun fun_;
public:
template<class T> explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}
template<class ...Args> decltype(auto) operator()(Args &&...args) { return fun_(std::ref(*this), std::forward<Args>(args)...); }
};
template<class Fun> decltype(auto) y_comb(Fun &&fun) { return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun)); }
//y_comb([&](...) -> type {...}) - рекурсия лямбд внутри другой функции
template<class T> AINLINE istream& operator>>(istream& in, vector<T>& a) { for (auto& x : a) in >> x; return in; }
template<class T> AINLINE ostream& operator<<(ostream& out, const vector<T>& a) { for (int i = 0; i < sz(a); ++i) out << a[i] << " \n"[i + 1 == sz(a)]; return out; }
AINLINE uint64_t splitmix64(uint64_t x) noexcept {
x += 0x9e3779b97f4a7c15ULL;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9ULL;
x = (x ^ (x >> 27)) * 0x94d049bb133111ebULL;
return x ^ (x >> 31);
}
static uint64_t sm_state = (uint64_t)chrono::steady_clock::now().time_since_epoch().count() ^ (uint64_t)(uintptr_t)new int;
AINLINE uint64_t rnd() noexcept { sm_state += 0x9e3779b97f4a7c15ULL; return splitmix64(sm_state); }
struct chash {
static AINLINE uint64_t seed() noexcept { static const uint64_t s = rnd(); return s; }
AINLINE size_t operator()(uint64_t x) const noexcept { uint64_t sd = seed(); return (size_t)splitmix64(x + sd); }
AINLINE size_t operator()(int64_t x) const noexcept { uint64_t sd = seed(); return (size_t)splitmix64((uint64_t)x + sd); }
AINLINE size_t operator()(uint32_t x) const noexcept { uint64_t sd = seed(); return (size_t)splitmix64((uint64_t)x + sd); }
AINLINE size_t operator()(int32_t x) const noexcept { uint64_t sd = seed(); return (size_t)splitmix64((uint64_t)(int64_t)x + sd); }
AINLINE size_t operator()(string_view s) const noexcept {
uint64_t sd = seed();
const uint8_t* p = (const uint8_t*)s.data();
size_t n = s.size();
uint64_t h = splitmix64(sd ^ (uint64_t)n);
while (n >= 8) {
uint64_t x;
memcpy(&x, p, 8);
h = splitmix64(h ^ (x + sd));
p += 8; n -= 8;
}
if (n) {
uint64_t x = 0;
memcpy(&x, p, n);
h = splitmix64(h ^ (x + sd));
}
return (size_t)h;
}
AINLINE size_t operator()(const string& s) const noexcept { return (*this)(string_view(s)); }
AINLINE size_t operator()(const char* s) const noexcept { return (*this)(string_view(s, strlen(s))); }
template<class A, class B>
AINLINE size_t operator()(const pair<A, B>& p) const noexcept {
uint64_t a = (uint64_t)(*this)(p.first);
uint64_t b = (uint64_t)(*this)(p.second);
return (size_t)splitmix64(a ^ (b + 0x9e3779b97f4a7c15ULL + (a << 6) + (a >> 2)));
}
};
template<class K, class V> using hash_map = __gnu_pbds::gp_hash_table<K, V, chash>;
template<class K> using hash_set = __gnu_pbds::gp_hash_table<K, __gnu_pbds::null_type, chash>;
template<class T> using ord_set = __gnu_pbds::tree<T, __gnu_pbds::null_type, less<T>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>;
// order_of_key(x) - сколько меньше x, find_by_order(x) - итератор на элемент на позиции x (0-based)
template<typename T, T MOD>
struct ModInt {
T v;
AINLINE ModInt() noexcept : v(0) {}
template<typename U> AINLINE ModInt(U x) noexcept { set(x); }
template<typename U>
AINLINE void set(U x) noexcept {
if constexpr (std::is_signed_v<U>) {
__int128 t = (__int128)x % (__int128)MOD;
if (t < 0) t += MOD;
v = (T)t;
}
else {
__uint128_t t = (__uint128_t)x % (__uint128_t)MOD;
v = (T)t;
}
}
AINLINE explicit operator T() const noexcept { return v; }
friend AINLINE ostream& operator<<(ostream& os, const ModInt& a) { return os << a.v; }
friend AINLINE istream& operator>>(istream& is, ModInt& a) { long long x; is >> x; a.set(x); return is; }
AINLINE ModInt& operator+=(const ModInt& o) noexcept { v += o.v; if (v >= MOD) v -= MOD; return *this; }
AINLINE ModInt& operator-=(const ModInt& o) noexcept { if (v < o.v) v += MOD; v -= o.v; return *this; }
AINLINE ModInt& operator*=(const ModInt& o) noexcept {
if constexpr (sizeof(T) <= 4) {
v = (T)(1ull * v * o.v % MOD);
} else {
v = (T)((__int128)v * o.v % MOD);
}
return *this;
}
static AINLINE ModInt pow(ModInt a, long long e) noexcept { ModInt r = 1; for (; e; e >>= 1, a *= a) if (e & 1) r *= a; return r; }
static AINLINE ModInt inv(ModInt a) noexcept { return pow(a, (long long)MOD - 2); }
AINLINE ModInt& operator/=(const ModInt& o) noexcept { return *this *= inv(o); }
friend AINLINE ModInt operator+(ModInt a, const ModInt& b) noexcept { return a += b; }
friend AINLINE ModInt operator-(ModInt a, const ModInt& b) noexcept { return a -= b; }
friend AINLINE ModInt operator*(ModInt a, const ModInt& b) noexcept { return a *= b; }
friend AINLINE ModInt operator/(ModInt a, const ModInt& b) noexcept { return a /= b; }
friend AINLINE bool operator==(const ModInt& a, const ModInt& b) noexcept { return a.v == b.v; }
friend AINLINE bool operator!=(const ModInt& a, const ModInt& b) noexcept { return a.v != b.v; }
};
AINLINE int msb(ll x) { return x ? 63 - __builtin_clzll((ull)x) : -1; } //старший 1-бит (0 based)
AINLINE int lsb(ll x) { return x ? __builtin_ctzll((ull)x) : -1; } //младший 1-бит (0 based)
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const ld PI = acos(-1.0L);
const ld EPS = 1e-9;
using mint = ModInt<int, MOD>;
template <typename T, typename K>
AINLINE T modpow(T a, K e) { T r = 1; for (; e; e >>= 1, a = (ull)a * a % MOD) if (e & 1) r = (ull)r * a % MOD; return r; }
template <typename T, typename K>
AINLINE T binpow(T a, K e) { T r = 1; for (; e; e >>= 1, a = a * a) if (e & 1) r = r * a; return r; }
void solve();
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(15);
//freopen("", "r", stdin);
//freopen("", "w", stdout);
int T = 1;
// cin >> T;
while (T--) solve();
return 0;
}
void dfs(int u, const vector<vector<int>>& graph, vector<bool>& visited, multiset<int>&vals, vi&a, vector<bool>&ans, int parent) {
visited[u] = true;
if (vals.contains(a[u-1]) || ans[parent]) {
ans[u] = 1;
}
else {
ans[u] = 0;
}
vals.insert(a[u-1]);
for (int v : graph[u]) {
if (!visited[v]) {
dfs(v, graph, visited, vals,a,ans,u);
}
}
auto it = vals.find(a[u-1]);
vals.erase(it);
}
void solve() {
int n;
cin >> n;
vi a(n); cin >> a;
vector<pair<int,int>> edges(n-1);
each2(x,y,edges) cin >> x >> y;
vector<vector<int>> graph(n+1);
each2(x,y,edges) {
graph[x].pb(y);
graph[y].pb(x);
}
vector<bool> visited(n+1);
multiset<int> vals;
vector<bool> ans(n+1);
dfs(1, graph, visited, vals,a,ans, 0);
for (int i=1; i<=n; ++i) cout << (ans[i] ? "Yes":"No") << nn;
}
提出情報
ジャッジ結果
| セット名 |
Sample |
All |
| 得点 / 配点 |
0 / 0 |
400 / 400 |
| 結果 |
|
|
| セット名 |
テストケース |
| Sample |
sample_01.txt, sample_02.txt, sample_03.txt |
| All |
hack_01.txt, hack_02.txt, hack_03.txt, hack_04.txt, hack_05.txt, hack_06.txt, sample_01.txt, sample_02.txt, sample_03.txt, test_01.txt, test_02.txt, test_03.txt, test_04.txt, test_05.txt, test_06.txt, test_07.txt, test_08.txt, test_09.txt, test_10.txt, test_11.txt, test_12.txt, test_13.txt, test_14.txt, test_15.txt, test_16.txt, test_17.txt, test_18.txt, test_19.txt, test_20.txt, test_21.txt, test_22.txt, test_23.txt, test_24.txt, test_25.txt, test_26.txt, test_27.txt, test_28.txt, test_29.txt, test_30.txt, test_31.txt, test_32.txt, test_33.txt, test_34.txt, test_35.txt, test_36.txt, test_37.txt, test_38.txt |
| ケース名 |
結果 |
実行時間 |
メモリ |
| hack_01.txt |
AC |
158 ms |
34416 KiB |
| hack_02.txt |
AC |
156 ms |
34468 KiB |
| hack_03.txt |
AC |
42 ms |
17708 KiB |
| hack_04.txt |
AC |
42 ms |
17572 KiB |
| hack_05.txt |
AC |
111 ms |
34400 KiB |
| hack_06.txt |
AC |
111 ms |
34396 KiB |
| sample_01.txt |
AC |
1 ms |
3564 KiB |
| sample_02.txt |
AC |
1 ms |
3704 KiB |
| sample_03.txt |
AC |
1 ms |
3660 KiB |
| test_01.txt |
AC |
1 ms |
3428 KiB |
| test_02.txt |
AC |
1 ms |
3660 KiB |
| test_03.txt |
AC |
197 ms |
51088 KiB |
| test_04.txt |
AC |
192 ms |
51184 KiB |
| test_05.txt |
AC |
191 ms |
51128 KiB |
| test_06.txt |
AC |
208 ms |
51008 KiB |
| test_07.txt |
AC |
124 ms |
51044 KiB |
| test_08.txt |
AC |
110 ms |
51104 KiB |
| test_09.txt |
AC |
43 ms |
17516 KiB |
| test_10.txt |
AC |
42 ms |
17568 KiB |
| test_11.txt |
AC |
42 ms |
17704 KiB |
| test_12.txt |
AC |
42 ms |
17508 KiB |
| test_13.txt |
AC |
42 ms |
17700 KiB |
| test_14.txt |
AC |
41 ms |
17552 KiB |
| test_15.txt |
AC |
70 ms |
16800 KiB |
| test_16.txt |
AC |
70 ms |
16756 KiB |
| test_17.txt |
AC |
69 ms |
16824 KiB |
| test_18.txt |
AC |
70 ms |
16956 KiB |
| test_19.txt |
AC |
68 ms |
16752 KiB |
| test_20.txt |
AC |
61 ms |
16808 KiB |
| test_21.txt |
AC |
58 ms |
18264 KiB |
| test_22.txt |
AC |
56 ms |
17112 KiB |
| test_23.txt |
AC |
51 ms |
17872 KiB |
| test_24.txt |
AC |
52 ms |
17952 KiB |
| test_25.txt |
AC |
56 ms |
17468 KiB |
| test_26.txt |
AC |
53 ms |
18136 KiB |
| test_27.txt |
AC |
170 ms |
42272 KiB |
| test_28.txt |
AC |
126 ms |
27364 KiB |
| test_29.txt |
AC |
166 ms |
41552 KiB |
| test_30.txt |
AC |
179 ms |
41048 KiB |
| test_31.txt |
AC |
90 ms |
35060 KiB |
| test_32.txt |
AC |
93 ms |
39472 KiB |
| test_33.txt |
AC |
134 ms |
30160 KiB |
| test_34.txt |
AC |
168 ms |
42712 KiB |
| test_35.txt |
AC |
151 ms |
36056 KiB |
| test_36.txt |
AC |
105 ms |
20148 KiB |
| test_37.txt |
AC |
122 ms |
45796 KiB |
| test_38.txt |
AC |
83 ms |
24372 KiB |