#include <bits/stdc++.h>
using namespace std;
#define forn(i,n) for (int i=0; i<n; ++i)
vector<vector<int>> adj(5001);
vector<vector<int>> cnt(5001,vector<int>(5001,0));
int path[5001];
int ptr=0;
void dfs(int u, int p, int to) {
for (auto v:adj[u]) {
if (v==p) continue;
if (v==to) {
path[ptr++]=v;
path[ptr++]=u;
return;
}
dfs(v,u,to);
if (ptr && path[ptr-1]==v) {
path[ptr++]=u;
return;
}
}
}
void solve() {
int n; cin>>n;
if (n>5000) exit(0);
forn(i,n-1) {
int u,v; cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i=0; i<=5000; ++i) {
path[i]=0;
}
int K; cin>>K;
forn(q,K) {
int u,v; cin>>u>>v;
ptr=0;
dfs(u,0,v);
for (int l=0, r=ptr-1; l<r; ++l, --r) {
swap(path[l],path[r]);
}
for (int i=0; i<ptr; ++i) {
for (int j=0; j*ptr+i<5000; ++j) cnt[path[i]][j*ptr+i]++;
}
}
int Q; cin>>Q;
forn(QQ,Q) {
int q; cin>>q;
if (q==1) {
int u,v; cin>>u>>v;
ptr=0;
dfs(u,0,v);
for (int l=0, r=ptr-1; l<r; ++l, --r) {
swap(path[l],path[r]);
}
for (int i=0; i<ptr; ++i) {
for (int j=0; j*ptr+i<5000; ++j) cnt[path[i]][j*ptr+i]++;
}
} else if (q==2) {
int u,v; cin>>u>>v;
ptr=0;
dfs(u,0,v);
for (int l=0, r=ptr-1; l<r; ++l, --r) {
swap(path[l],path[r]);
}
for (int i=0; i<ptr; ++i) {
for (int j=0; j*ptr+i<5000; ++j) cnt[path[i]][j*ptr+i]--;
}
} else {
int u,v; cin>>u>>v;
ptr=0;
dfs(u,0,v);
for (int l=0, r=ptr-1; l<r; ++l, --r) {
swap(path[l],path[r]);
}
int ans=0;
int l,r; cin>>l>>r;
r=min(r,n);
for (int i=0; i<ptr; ++i) {
for (int j=l; j<=r; ++j) ans+=cnt[path[i]][j];
}
cout<<ans<<'\n';
}
}
}
int32_t main() {
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
43 ms |
98392 KB |
Output isn't correct |
2 |
Incorrect |
73 ms |
98516 KB |
Output isn't correct |
3 |
Incorrect |
67 ms |
98508 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
39 ms |
98364 KB |
Output isn't correct |
2 |
Incorrect |
39 ms |
98384 KB |
Output isn't correct |
3 |
Incorrect |
39 ms |
98460 KB |
Output isn't correct |
4 |
Incorrect |
42 ms |
98468 KB |
Output isn't correct |
5 |
Incorrect |
50 ms |
98432 KB |
Output isn't correct |
6 |
Incorrect |
43 ms |
98388 KB |
Output isn't correct |
7 |
Incorrect |
40 ms |
98432 KB |
Output isn't correct |
8 |
Incorrect |
40 ms |
98356 KB |
Output isn't correct |
9 |
Incorrect |
41 ms |
98428 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
41 ms |
98452 KB |
Output isn't correct |
2 |
Incorrect |
49 ms |
98380 KB |
Output isn't correct |
3 |
Incorrect |
41 ms |
98392 KB |
Output isn't correct |
4 |
Incorrect |
40 ms |
98468 KB |
Output isn't correct |
5 |
Incorrect |
44 ms |
98360 KB |
Output isn't correct |
6 |
Incorrect |
40 ms |
98376 KB |
Output isn't correct |
7 |
Incorrect |
39 ms |
98392 KB |
Output isn't correct |
8 |
Incorrect |
40 ms |
98444 KB |
Output isn't correct |