이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*
ЗАПУСКАЕМ
░ГУСЯ░▄▀▀▀▄░РАБОТЯГУ░░
▄███▀░◐░░░▌░░░░░░░
░░░░▌░░░░░▐░░░░░░░
░░░░▐░░░░░▐░░░░░░░
░░░░▌░░░░░▐▄▄░░░░░
░░░░▌░░░░▄▀▒▒▀▀▀▀▄
░░░▐░░░░▐▒▒▒▒▒▒▒▒▀▀▄
░░░▐░░░░▐▄▒▒▒▒▒▒▒▒▒▒▀▄
░░░░▀▄░░░░▀▄▒▒▒▒▒▒▒▒▒▒▀▄
░░░░░░▀▄▄▄▄▄█▄▄▄▄▄▄▄▄▄▄▄▀▄
░░░░░░░░░░░▌▌░▌▌░░░░░
░░░░░░░░░░░▌▌░▌▌░░░░░
░░░░░░░░░▄▄▌▌▄▌▌░░░░░
*/
#include <iostream>
#include <complex>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <numeric>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <cmath>
#include <bitset>
#include <cassert>
#include <queue>
#include <stack>
#include <deque>
#include <random>
using namespace std;
template<typename T1, typename T2>inline void chkmin(T1 &x, T2 y) { if (x > y) x = y; }
template<typename T1, typename T2>inline void chkmax(T1 &x, T2 y) { if (x < y) x = y; }
#define sz(c) (int)(c).size()
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define left left224
#define right right224
#define next next224
#define rank rank224
#define prev prev224
#define y1 y1224
#define read(FILENAME) freopen((FILENAME + ".in").c_str(), "r", stdin)
#define write(FILENAME) freopen((FILENAME + ".out").c_str(), "w", stdout)
#define files(FILENAME) read(FILENAME), write(FILENAME)
#define pb push_back
#define mp make_pair
const string FILENAME = "input";
const int MAXN = 100228;
int n, m, q;
int a[MAXN], b[MAXN];
bool on[MAXN];
int ans[MAXN];
vector<pair<int, int> > g[MAXN];
vector<int> pos[MAXN];
map<pair<int, int>, int> who;
map<pair<int, int>, bool> was;
map<pair<int, int>, vector<int> > cnt;
pair<int, int> f[MAXN];
void calc(int a, int b) {
if (was[{a, b}]) {
return;
}
int id = who[{a, b}];
if (pos[id].empty()) {
return;
}
was[{a, b}] = true;
cnt[{a, b}].resize(sz(pos[id]));
set<pair<int, pair<int, int> > > s;
for (auto h: g[b]) {
if (h.first != a) {
// cout << b + 1 << ' ' << h.first + 1 << endl;
calc(b, h.first);
int id = h.second;
if (!pos[id].empty()) {
s.insert({pos[id][0], {id, 0}});
}
}
}
for (auto h: g[b]) {
if (h.first != a) {
int id = h.second;
f[id] = {b, h.first};
}
}
int cntf = 1;
for (int i = 0; i < sz(pos[id]); i++) {
while (!s.empty()) {
auto x = *s.begin();
if (x.first <= pos[id][i]) {
cntf += cnt[f[x.second.first]][x.second.second] - (x.second.second == 0 ? 0: cnt[f[x.second.first]][x.second.second - 1]);
s.erase(x);
if (x.second.second + 1 < sz(pos[x.second.first])) {
x.second.second++;
x.first = pos[x.second.first][x.second.second];
s.insert(x);
}
} else {
break;
}
}
cnt[{a, b}][i] = cntf;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//read(FILENAME);
cin >> n >> m >> q;
for (int i = 0; i < n - 1; i++) {
cin >> a[i] >> b[i];
a[i]--, b[i]--;
g[a[i]].pb({b[i], i});
g[b[i]].pb({a[i], i});
who[{a[i], b[i]}] = i;
who[{b[i], a[i]}] = i;
}
for (int i = 0; i < m; i++) {
int id;
cin >> id;
id--;
if (on[id]) {
on[id] = false;
pos[id].pb(i - 1);
} else {
pos[id].pb(i);
on[id] = true;
}
}
for (int i = 0; i < n - 1; i++) {
if (on[i]) {
pos[i].pb(m);
}
}
for (int i = 0; i < q; i++) {
int c;
cin >> c;
c--;
int res = 1;
int kek = 0;
for (auto h: g[c]) {
if (!pos[h.second].empty()) {
chkmax(kek, pos[h.second].back());
}
}
for (auto h: g[c]) {
if (!pos[h.second].empty()) {
if (kek == pos[h.second].back()) {
calc(c, h.first);
calc(h.first, c);
//cout << c + 1 << ' ' << h.first + 1 << ' ' << cnt[{c, h.first}].back() << ' ' << cnt[{h.first, c}].back() << endl;
res += cnt[{c, h.first}].back() + cnt[{h.first, c}].back() - 1;
break;
}
}
}
cout << res << '\n';
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |