/*
ЗАПУСКАЕМ
░ГУСЯ░▄▀▀▀▄░РАБОТЯГУ░░
▄███▀░◐░░░▌░░░░░░░
░░░░▌░░░░░▐░░░░░░░
░░░░▐░░░░░▐░░░░░░░
░░░░▌░░░░░▐▄▄░░░░░
░░░░▌░░░░▄▀▒▒▀▀▀▀▄
░░░▐░░░░▐▒▒▒▒▒▒▒▒▀▀▄
░░░▐░░░░▐▄▒▒▒▒▒▒▒▒▒▒▀▄
░░░░▀▄░░░░▀▄▒▒▒▒▒▒▒▒▒▒▀▄
░░░░░░▀▄▄▄▄▄█▄▄▄▄▄▄▄▄▄▄▄▀▄
░░░░░░░░░░░▌▌░▌▌░░░░░
░░░░░░░░░░░▌▌░▌▌░░░░░
░░░░░░░░░▄▄▌▌▄▌▌░░░░░
*/
#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<pair<int, 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];
int last[MAXN];
void calc(int a, int b) {
if (was[{a, b}]) {
return;
}
int id = who[{a, b}];
was[{a, b}] = true;
if (pos[id].empty()) {
return;
}
cnt[{a, b}].resize(sz(pos[id]));
set<pair<int, pair<int, int> > > s;
vector<int> fk(sz(pos[id]), 0);
for (auto h: g[b]) {
if (h.first != a) {
// cout << b + 1 << ' ' << h.first + 1 << endl;
calc(b, h.first);
int id1 = h.second;
if (!pos[id1].empty()) {
int it = 0;
int last = 0;
for (auto x: pos[id1]) {
int l = 0;
int r = sz(pos[id]) - 1;
while (l < r) {
int mid = (l + r) >> 1;
if (pos[id][mid].second >= x.first) {
r = mid;
} else {
l = mid + 1;
}
}
if (l < sz(pos[id]) && pos[id][l].second >= x.first) {
fk[l] += cnt[{b, h.first}][it] - last;
last = cnt[{b, h.first}][it];
}
it++;
}
}
}
}
int cntf = 1;
for (int i = 0; i < sz(pos[id]); i++) {
cntf += fk[i];
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({last[id], i - 1});
} else {
on[id] = true;
last[id] = i;
}
}
for (int i = 0; i < n - 1; i++) {
if (on[i]) {
pos[i].pb({last[i], m - 1});
}
}
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().second);
}
}
for (auto h: g[c]) {
if (!pos[h.second].empty()) {
if (kek == pos[h.second].back().second) {
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
4984 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
4984 KB |
Output is correct |
4 |
Correct |
6 ms |
5112 KB |
Output is correct |
5 |
Incorrect |
6 ms |
5112 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
304 ms |
55048 KB |
Output is correct |
2 |
Correct |
308 ms |
49776 KB |
Output is correct |
3 |
Correct |
311 ms |
64888 KB |
Output is correct |
4 |
Correct |
301 ms |
61944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
4988 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
494 ms |
56296 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5112 KB |
Output is correct |
4 |
Incorrect |
7 ms |
5112 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |