#include "bits/stdc++.h"
//#pragma GCC optimize("O3")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll>pairll;
typedef long double ld;
#define fr first
#define sc second
#define pb push_back
#define INF 100000000000000007
#define I 100000000000007
#define N 100007
#define endl '\n'
#define MOD 998244353
ll n,res,resx,d[2*N],res2,t[2*N];
vector<pairll>v[2*N];
map<ll,ll>m[2*N];
priority_queue<ll>q,q1;
ll S(ll x,ll y){
ll rs=0;
for(int i=0;i<v[x].size();i++){
if(v[x][i].fr!=y){
rs+=v[x][i].sc+S(v[x][i].fr,x);
}
}
return rs;
}
void D(ll x,ll y,ll z){
if(res>z){
res=z;
resx=x;
}
t[x]=z;
for(int i=0;i<v[x].size();i++){
if(v[x][i].fr!=y){
D(v[x][i].fr,x,z-v[x][i].sc+m[v[x][i].fr][x]);
}
}
return ;
}
ll R(ll x,ll y){
ll k=0;
for(int i=0;i<v[x].size();i++){
if(v[x][i].fr!=y){
ll r=R(v[x][i].fr,x)+v[x][i].sc;
res2=min(t[x]-r-k,res2);
if(k<r)swap(k,r);
if(r!=0 && x!=resx)q.push(r);
else if(r!=0)q1.push(r);
}
}
return k;
}
int main()
{
cin>>n;
for(int i=1;i<n;i++){
ll x,y,z,z1;
cin>>x>>y>>z>>z1;
v[x].pb({y,z});
v[y].pb({x,z1});
m[x][y]=z;
m[y][x]=z1;
}
res2=INF;
res=INF;
D(1,1,S(1,1));
ll k=R(resx,0);
q1.push(k);
ll x=res;
//cout<<res<<" "<<res2<<endl;
//cout<<"! "<<res<<" "<<resx<<endl;
q1.push(0);
res-=q1.top();
q1.pop();
res-=q1.top();
q1.pop();
d[2]=res2;
while(q1.size()>0){
q.push(q1.top());
q1.pop();
}
for(int i=3;i<=n;i++){
if(q.size()>0){
res-=q.top();
//cout<<i<<" "<<q.top()<<endl;
q.pop();
}
d[i]=res;
}
d[1]=x;
ll Q;
cin>>Q;
for(int i=1;i<=Q;i++){
cin>>x;
cout<<d[x]<<endl;
}
return 0;
}
Compilation message
designated_cities.cpp: In function 'll S(ll, ll)':
designated_cities.cpp:30:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | for(int i=0;i<v[x].size();i++){
| ~^~~~~~~~~~~~
designated_cities.cpp: In function 'void D(ll, ll, ll)':
designated_cities.cpp:44:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for(int i=0;i<v[x].size();i++){
| ~^~~~~~~~~~~~
designated_cities.cpp: In function 'll R(ll, ll)':
designated_cities.cpp:54:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for(int i=0;i<v[x].size();i++){
| ~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14292 KB |
Output is correct |
2 |
Correct |
8 ms |
14400 KB |
Output is correct |
3 |
Correct |
9 ms |
14452 KB |
Output is correct |
4 |
Correct |
8 ms |
14292 KB |
Output is correct |
5 |
Incorrect |
7 ms |
14420 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14296 KB |
Output is correct |
2 |
Correct |
564 ms |
57216 KB |
Output is correct |
3 |
Correct |
663 ms |
71844 KB |
Output is correct |
4 |
Correct |
537 ms |
57192 KB |
Output is correct |
5 |
Correct |
595 ms |
57320 KB |
Output is correct |
6 |
Correct |
571 ms |
59536 KB |
Output is correct |
7 |
Correct |
565 ms |
57940 KB |
Output is correct |
8 |
Correct |
616 ms |
72628 KB |
Output is correct |
9 |
Correct |
558 ms |
59476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14292 KB |
Output is correct |
2 |
Correct |
567 ms |
57160 KB |
Output is correct |
3 |
Correct |
614 ms |
77192 KB |
Output is correct |
4 |
Correct |
535 ms |
58492 KB |
Output is correct |
5 |
Correct |
620 ms |
60096 KB |
Output is correct |
6 |
Correct |
593 ms |
62564 KB |
Output is correct |
7 |
Correct |
571 ms |
61700 KB |
Output is correct |
8 |
Correct |
636 ms |
69804 KB |
Output is correct |
9 |
Correct |
567 ms |
61584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14292 KB |
Output is correct |
2 |
Correct |
8 ms |
14400 KB |
Output is correct |
3 |
Correct |
9 ms |
14452 KB |
Output is correct |
4 |
Correct |
8 ms |
14292 KB |
Output is correct |
5 |
Incorrect |
7 ms |
14420 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14296 KB |
Output is correct |
2 |
Correct |
564 ms |
57216 KB |
Output is correct |
3 |
Correct |
663 ms |
71844 KB |
Output is correct |
4 |
Correct |
537 ms |
57192 KB |
Output is correct |
5 |
Correct |
595 ms |
57320 KB |
Output is correct |
6 |
Correct |
571 ms |
59536 KB |
Output is correct |
7 |
Correct |
565 ms |
57940 KB |
Output is correct |
8 |
Correct |
616 ms |
72628 KB |
Output is correct |
9 |
Correct |
558 ms |
59476 KB |
Output is correct |
10 |
Correct |
7 ms |
14292 KB |
Output is correct |
11 |
Correct |
567 ms |
57160 KB |
Output is correct |
12 |
Correct |
614 ms |
77192 KB |
Output is correct |
13 |
Correct |
535 ms |
58492 KB |
Output is correct |
14 |
Correct |
620 ms |
60096 KB |
Output is correct |
15 |
Correct |
593 ms |
62564 KB |
Output is correct |
16 |
Correct |
571 ms |
61700 KB |
Output is correct |
17 |
Correct |
636 ms |
69804 KB |
Output is correct |
18 |
Correct |
567 ms |
61584 KB |
Output is correct |
19 |
Correct |
7 ms |
14316 KB |
Output is correct |
20 |
Correct |
587 ms |
59884 KB |
Output is correct |
21 |
Correct |
596 ms |
77116 KB |
Output is correct |
22 |
Correct |
612 ms |
58532 KB |
Output is correct |
23 |
Incorrect |
559 ms |
60236 KB |
Output isn't correct |
24 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14292 KB |
Output is correct |
2 |
Correct |
8 ms |
14400 KB |
Output is correct |
3 |
Correct |
9 ms |
14452 KB |
Output is correct |
4 |
Correct |
8 ms |
14292 KB |
Output is correct |
5 |
Incorrect |
7 ms |
14420 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |