結果
問題 | No.2618 除霊 |
ユーザー | coindarw |
提出日時 | 2024-01-26 23:40:10 |
言語 | C++23 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 323 ms / 2,000 ms |
コード長 | 6,953 bytes |
コンパイル時間 | 5,927 ms |
コンパイル使用メモリ | 318,900 KB |
実行使用メモリ | 32,224 KB |
最終ジャッジ日時 | 2024-09-28 09:11:15 |
合計ジャッジ時間 | 18,544 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,816 KB |
testcase_02 | AC | 2 ms
6,944 KB |
testcase_03 | AC | 2 ms
6,940 KB |
testcase_04 | AC | 2 ms
6,940 KB |
testcase_05 | AC | 2 ms
6,940 KB |
testcase_06 | AC | 239 ms
27,616 KB |
testcase_07 | AC | 232 ms
27,432 KB |
testcase_08 | AC | 277 ms
30,216 KB |
testcase_09 | AC | 305 ms
30,328 KB |
testcase_10 | AC | 320 ms
30,892 KB |
testcase_11 | AC | 205 ms
25,620 KB |
testcase_12 | AC | 288 ms
30,632 KB |
testcase_13 | AC | 186 ms
31,668 KB |
testcase_14 | AC | 231 ms
31,544 KB |
testcase_15 | AC | 120 ms
23,804 KB |
testcase_16 | AC | 132 ms
24,116 KB |
testcase_17 | AC | 173 ms
28,484 KB |
testcase_18 | AC | 186 ms
30,952 KB |
testcase_19 | AC | 131 ms
24,864 KB |
testcase_20 | AC | 122 ms
24,000 KB |
testcase_21 | AC | 164 ms
27,296 KB |
testcase_22 | AC | 188 ms
32,208 KB |
testcase_23 | AC | 194 ms
31,776 KB |
testcase_24 | AC | 193 ms
31,416 KB |
testcase_25 | AC | 240 ms
32,224 KB |
testcase_26 | AC | 222 ms
31,140 KB |
testcase_27 | AC | 193 ms
31,284 KB |
testcase_28 | AC | 190 ms
32,096 KB |
testcase_29 | AC | 238 ms
32,128 KB |
testcase_30 | AC | 242 ms
32,220 KB |
testcase_31 | AC | 290 ms
30,636 KB |
testcase_32 | AC | 177 ms
24,040 KB |
testcase_33 | AC | 214 ms
26,096 KB |
testcase_34 | AC | 227 ms
26,624 KB |
testcase_35 | AC | 253 ms
28,356 KB |
testcase_36 | AC | 275 ms
29,876 KB |
testcase_37 | AC | 276 ms
29,864 KB |
testcase_38 | AC | 298 ms
30,932 KB |
testcase_39 | AC | 312 ms
31,316 KB |
testcase_40 | AC | 306 ms
30,720 KB |
testcase_41 | AC | 320 ms
31,288 KB |
testcase_42 | AC | 323 ms
31,000 KB |
ソースコード
#ifdef ONLINE_JUDGE #include <bits/stdc++.h> #include <atcoder/all> #else #include <mylibs/all.h> #endif using ll = long long; using lll = __int128_t; #define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i) #define reps(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i) #define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i) #define rreps(i, n) for (int i = ((int)(n)); i > 0; --i) #define rep2(i, s, n) for (int i = (s); i < (int)(n); i++) #define repc2(i, s, n) for (int i = (s); i <= (int)(n); i++) #define length(v) ((int)(v).size()) constexpr int inf = 2'000'000'000; constexpr ll linf = 4'000'000'000'000'000'000, M7 = 1'000'000'007, M9 = 998'244'353; #define all(v) begin(v), end(v) #define rall(v) rbegin(v), rend(v) using namespace std; using namespace atcoder; // clang-format off #define Vec(type, ...) __make_vec<type>(__VA_ARGS__) template <class T> vector<T> __make_vec(size_t a) {return vector<T>(a);}template <class T, class... Ts> auto __make_vec(size_t a, Ts... ts) {return vector<decltype(__make_vec<T>(ts...))>(a, __make_vec<T>(ts...));} #define VecI(init, type, ...) __make_vecI<type, init>(__VA_ARGS__) template <class T, T init>vector<T> __make_vecI(size_t a) {return vector<T>(a, init);} template <class T, T init, class... Ts> auto __make_vecI(size_t a, Ts... ts) {return vector<decltype(__make_vecI<T, init>(ts...))>(a, __make_vecI<T, init>(ts...));} template <typename T, typename U>inline ostream& operator<<(ostream& os, const pair<T, U>& p) noexcept {return os << p.first << " " << p.second;} inline ostream& operator<<(ostream& os, const modint& m) noexcept { return os << m.val(); } template <int M>inline ostream& operator<<(ostream& os, const static_modint<M>& m) noexcept { return os << m.val(); } template <typename T> struct is_static_modint : std::false_type {}; template <int MOD> struct is_static_modint<static_modint<MOD>> : std::true_type {}; template <template <typename...> typename C, typename Number>concept MyContainer = std::is_same_v<C<Number>, std::vector<Number>> || std::is_same_v<C<Number>, std::deque<Number>> || std::is_same_v<C<Number>, std::set<Number>> || std::is_same_v<C<Number>, std::unordered_set<Number>> || std::is_same_v<C<Number>, std::unordered_multiset<Number>> || std::is_same_v<C<Number>, std::multiset<Number>>; template <typename Number>concept MyNumber = std::is_same_v<Number, int> || std::is_same_v<Number, ll> || std::is_same_v<Number, char> || std::is_same_v<Number, modint> || is_static_modint<Number>::value; template <template <typename...> typename C, typename Number>concept MyContainerNumber = MyContainer<C, Number> && MyNumber<Number>; template <template <typename...> typename OutCon, template <typename...> typename InCon, typename Number>concept MyNestedContainerNumber = MyContainer<OutCon, InCon<Number>> && MyContainerNumber<InCon, Number>; template <template <typename...> typename C, typename Number>requires MyContainerNumber<C, Number>std::ostream& operator<<(std::ostream& os, const C<Number>& t) {auto itr = t.begin();auto end = t.end();if (itr != end) {os << *itr++;for (; itr != end; ++itr) os << ' ' << *itr;}return os;} template <template <typename...> typename OutCon, template <typename...> typename InCon, typename Number>requires MyNestedContainerNumber<OutCon, InCon, Number>std::ostream& operator<<(std::ostream& os, const OutCon<InCon<Number>>& t) {auto itr = t.begin();auto end = t.end();if (itr != end) {os << *itr++;for (; itr != end; ++itr) os << '\n' << *itr;}return os;} template <typename T, typename U>istream& operator>>(istream& is, pair<T, U>& p) {return is >> p.first >> p.second;} template <typename T>istream& operator>>(istream& is, vector<T>& v) {for (auto& e : v) is >> e;return is;} void inp() {} template <typename T, typename... Args>void inp(T& a, Args&... args) {cin >> a, inp(args...);} template <typename T>void inp1(vector<T>& v, int offset = 1, int len = -1) {if (len == -1) len = int(v.size()) - offset;assert(offset >= 0 && len >= 0);for (int i = offset; i < offset + len; ++i) cin >> v[i];} template <typename T>void oup(const T& a) {cout << a << "\n";} template <typename T, typename... Args>void oup(const T& a, const Args&... args) {cout << a << " ", oup(args...);} inline string YESNO(bool cond) { return cond ? "YES" : "NO"; }inline string yesno(bool cond) { return cond ? "yes" : "no"; }inline string YesNo(bool cond) { return cond ? "Yes" : "No"; } inline auto add1(auto vec, ll offset = 1) {for (auto& e : vec) e += offset;return vec;} #ifdef ONLINE_JUDGE #define debug(...) #else #define debug(...) cerr << "<" << #__VA_ARGS__ << ">: ", debug_out(__VA_ARGS__) template <typename T>void debug_out(T t) {cerr << t << "\n";} template <typename T, typename... Args>void debug_out(T t, Args... args) {cerr << t << ", ";debug_out(args...);} #endif #ifdef ONLINE_JUDGE #define todo(...) static_assert(false) #else #define todo(...) #endif // clang-format on int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; inp(n); struct edge { int to; edge(int to) : to(to) {} }; vector<vector<edge>> G(n); rep(i, n - 1) { int a, b; inp(a, b); a--, b--; G[a].emplace_back(b); G[b].emplace_back(a); } int m; inp(m); vector<int> v(m); inp(v); v = add1(v, -1); vector<bool> vb(n); rep(i, m) vb[v[i]] = true; vector<vector<int>> idx1 = vector<vector<int>>(n, vector<int>()); // idx1[i] := iにいるオバケ以外の影響を受けない頂点の集合 vector<vector<int>> idx2 = vector<vector<int>>(n, vector<int>()); // idx2[i] := iにいるオバケの影響を受ける頂点の集合 rep(i, m) { idx2[v[i]].emplace_back(v[i]); for (const auto& e : G[v[i]]) { idx2[e.to].emplace_back(v[i]); } } rep(i, m) { for (const auto& e : G[v[i]]) { if (idx2[e.to].size() == 1) idx1[v[i]].emplace_back(e.to); } } rep(i, n) { sort(all(idx1[i])); sort(all(idx2[i])); } int sum = 0; rep(i, n) if (idx2[i].size()) sum++; rep(i, n) { int ans = sum; // 距離0の頂点 bool f = vb[i]; for (const auto& e : G[i]) if (vb[e.to]) f = true; if (f) ans--; // 距離2~の頂点 for (const auto& e : G[i]) { ans -= idx1[e.to].size(); auto lb = lower_bound(all(idx1[e.to]), i); if (lb != idx1[e.to].end() && *lb == i) ans++; } // 距離1の頂点 for (const auto& e : G[i]) { if (idx2[e.to].size() > 2) continue; if (idx2[e.to].size() == 0) continue; bool of = false; rep(j, min<int>(3, idx2[e.to].size())) { if (idx2[e.to][j] != i && idx2[e.to][j] != e.to) of = true; } if (!of) ans--; } oup(ans); } return 0; }