#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],mn;
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;
if(res2>t[x]-r-k){
res2=min(t[x]-r-k,res2);
mn=x;
}
if(k<r)swap(k,r);
}
}
return k;
}
ll R1(ll x,ll y){
ll k=0;
for(int i=0;i<v[x].size();i++){
if(v[x][i].fr!=y){
ll r=R1(v[x][i].fr,x)+v[x][i].sc;
if(k<r)swap(k,r);
if(r!=0 && x!=mn)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));
R(resx,0);
d[1]=res;
//cout<<res<<endl;
ll k=R1(mn,0);
q1.push(k);
// cout<<resx<<" "<<mn<<endl;
//cout<<res<<" "<<res2<<endl;
//cout<<k<<endl;
res=t[mn];
ll x=res;
//cout<<"! "<<res<<" "<<resx<<endl;
q1.push(0);
res-=q1.top();
//cout<<q1.top()<<endl;
q1.pop();
res-=q1.top();
//cout<<q1.top()<<endl;
q1.pop();
d[2]=res;
//cout<<"! "<<d[2]<<endl;
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;
}
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++){
| ~^~~~~~~~~~~~
designated_cities.cpp: In function 'll R1(ll, ll)':
designated_cities.cpp:69: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]
69 | for(int i=0;i<v[x].size();i++){
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14292 KB |
Output is correct |
2 |
Correct |
9 ms |
14292 KB |
Output is correct |
3 |
Correct |
9 ms |
14292 KB |
Output is correct |
4 |
Correct |
8 ms |
14292 KB |
Output is correct |
5 |
Correct |
7 ms |
14292 KB |
Output is correct |
6 |
Correct |
8 ms |
14420 KB |
Output is correct |
7 |
Correct |
9 ms |
14292 KB |
Output is correct |
8 |
Correct |
9 ms |
14292 KB |
Output is correct |
9 |
Correct |
8 ms |
14292 KB |
Output is correct |
10 |
Correct |
8 ms |
14396 KB |
Output is correct |
11 |
Correct |
8 ms |
14360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14292 KB |
Output is correct |
2 |
Correct |
700 ms |
53552 KB |
Output is correct |
3 |
Correct |
711 ms |
68352 KB |
Output is correct |
4 |
Correct |
557 ms |
53760 KB |
Output is correct |
5 |
Correct |
623 ms |
55364 KB |
Output is correct |
6 |
Correct |
628 ms |
56216 KB |
Output is correct |
7 |
Correct |
624 ms |
55360 KB |
Output is correct |
8 |
Correct |
661 ms |
69268 KB |
Output is correct |
9 |
Correct |
588 ms |
57484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14292 KB |
Output is correct |
2 |
Correct |
624 ms |
53452 KB |
Output is correct |
3 |
Correct |
659 ms |
70704 KB |
Output is correct |
4 |
Correct |
585 ms |
53480 KB |
Output is correct |
5 |
Correct |
604 ms |
54784 KB |
Output is correct |
6 |
Correct |
632 ms |
56140 KB |
Output is correct |
7 |
Correct |
604 ms |
56864 KB |
Output is correct |
8 |
Correct |
639 ms |
63424 KB |
Output is correct |
9 |
Correct |
590 ms |
57096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14292 KB |
Output is correct |
2 |
Correct |
9 ms |
14292 KB |
Output is correct |
3 |
Correct |
9 ms |
14292 KB |
Output is correct |
4 |
Correct |
8 ms |
14292 KB |
Output is correct |
5 |
Correct |
7 ms |
14292 KB |
Output is correct |
6 |
Correct |
8 ms |
14420 KB |
Output is correct |
7 |
Correct |
9 ms |
14292 KB |
Output is correct |
8 |
Correct |
9 ms |
14292 KB |
Output is correct |
9 |
Correct |
8 ms |
14292 KB |
Output is correct |
10 |
Correct |
8 ms |
14396 KB |
Output is correct |
11 |
Correct |
8 ms |
14360 KB |
Output is correct |
12 |
Correct |
11 ms |
14292 KB |
Output is correct |
13 |
Correct |
16 ms |
14892 KB |
Output is correct |
14 |
Correct |
14 ms |
14924 KB |
Output is correct |
15 |
Correct |
15 ms |
14804 KB |
Output is correct |
16 |
Correct |
14 ms |
14804 KB |
Output is correct |
17 |
Correct |
13 ms |
14796 KB |
Output is correct |
18 |
Correct |
14 ms |
14804 KB |
Output is correct |
19 |
Correct |
14 ms |
14804 KB |
Output is correct |
20 |
Correct |
15 ms |
14876 KB |
Output is correct |
21 |
Correct |
17 ms |
14792 KB |
Output is correct |
22 |
Correct |
18 ms |
14796 KB |
Output is correct |
23 |
Correct |
14 ms |
14804 KB |
Output is correct |
24 |
Correct |
15 ms |
14916 KB |
Output is correct |
25 |
Correct |
14 ms |
14932 KB |
Output is correct |
26 |
Correct |
15 ms |
14804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14292 KB |
Output is correct |
2 |
Correct |
700 ms |
53552 KB |
Output is correct |
3 |
Correct |
711 ms |
68352 KB |
Output is correct |
4 |
Correct |
557 ms |
53760 KB |
Output is correct |
5 |
Correct |
623 ms |
55364 KB |
Output is correct |
6 |
Correct |
628 ms |
56216 KB |
Output is correct |
7 |
Correct |
624 ms |
55360 KB |
Output is correct |
8 |
Correct |
661 ms |
69268 KB |
Output is correct |
9 |
Correct |
588 ms |
57484 KB |
Output is correct |
10 |
Correct |
8 ms |
14292 KB |
Output is correct |
11 |
Correct |
624 ms |
53452 KB |
Output is correct |
12 |
Correct |
659 ms |
70704 KB |
Output is correct |
13 |
Correct |
585 ms |
53480 KB |
Output is correct |
14 |
Correct |
604 ms |
54784 KB |
Output is correct |
15 |
Correct |
632 ms |
56140 KB |
Output is correct |
16 |
Correct |
604 ms |
56864 KB |
Output is correct |
17 |
Correct |
639 ms |
63424 KB |
Output is correct |
18 |
Correct |
590 ms |
57096 KB |
Output is correct |
19 |
Correct |
8 ms |
14400 KB |
Output is correct |
20 |
Correct |
637 ms |
53804 KB |
Output is correct |
21 |
Correct |
651 ms |
71300 KB |
Output is correct |
22 |
Correct |
559 ms |
53992 KB |
Output is correct |
23 |
Correct |
602 ms |
54156 KB |
Output is correct |
24 |
Correct |
579 ms |
59076 KB |
Output is correct |
25 |
Correct |
589 ms |
60184 KB |
Output is correct |
26 |
Correct |
661 ms |
59216 KB |
Output is correct |
27 |
Correct |
587 ms |
60672 KB |
Output is correct |
28 |
Correct |
601 ms |
62116 KB |
Output is correct |
29 |
Correct |
618 ms |
60456 KB |
Output is correct |
30 |
Correct |
585 ms |
60168 KB |
Output is correct |
31 |
Correct |
600 ms |
62680 KB |
Output is correct |
32 |
Correct |
641 ms |
70544 KB |
Output is correct |
33 |
Correct |
581 ms |
63652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14292 KB |
Output is correct |
2 |
Correct |
9 ms |
14292 KB |
Output is correct |
3 |
Correct |
9 ms |
14292 KB |
Output is correct |
4 |
Correct |
8 ms |
14292 KB |
Output is correct |
5 |
Correct |
7 ms |
14292 KB |
Output is correct |
6 |
Correct |
8 ms |
14420 KB |
Output is correct |
7 |
Correct |
9 ms |
14292 KB |
Output is correct |
8 |
Correct |
9 ms |
14292 KB |
Output is correct |
9 |
Correct |
8 ms |
14292 KB |
Output is correct |
10 |
Correct |
8 ms |
14396 KB |
Output is correct |
11 |
Correct |
8 ms |
14360 KB |
Output is correct |
12 |
Correct |
8 ms |
14292 KB |
Output is correct |
13 |
Correct |
700 ms |
53552 KB |
Output is correct |
14 |
Correct |
711 ms |
68352 KB |
Output is correct |
15 |
Correct |
557 ms |
53760 KB |
Output is correct |
16 |
Correct |
623 ms |
55364 KB |
Output is correct |
17 |
Correct |
628 ms |
56216 KB |
Output is correct |
18 |
Correct |
624 ms |
55360 KB |
Output is correct |
19 |
Correct |
661 ms |
69268 KB |
Output is correct |
20 |
Correct |
588 ms |
57484 KB |
Output is correct |
21 |
Correct |
8 ms |
14292 KB |
Output is correct |
22 |
Correct |
624 ms |
53452 KB |
Output is correct |
23 |
Correct |
659 ms |
70704 KB |
Output is correct |
24 |
Correct |
585 ms |
53480 KB |
Output is correct |
25 |
Correct |
604 ms |
54784 KB |
Output is correct |
26 |
Correct |
632 ms |
56140 KB |
Output is correct |
27 |
Correct |
604 ms |
56864 KB |
Output is correct |
28 |
Correct |
639 ms |
63424 KB |
Output is correct |
29 |
Correct |
590 ms |
57096 KB |
Output is correct |
30 |
Correct |
11 ms |
14292 KB |
Output is correct |
31 |
Correct |
16 ms |
14892 KB |
Output is correct |
32 |
Correct |
14 ms |
14924 KB |
Output is correct |
33 |
Correct |
15 ms |
14804 KB |
Output is correct |
34 |
Correct |
14 ms |
14804 KB |
Output is correct |
35 |
Correct |
13 ms |
14796 KB |
Output is correct |
36 |
Correct |
14 ms |
14804 KB |
Output is correct |
37 |
Correct |
14 ms |
14804 KB |
Output is correct |
38 |
Correct |
15 ms |
14876 KB |
Output is correct |
39 |
Correct |
17 ms |
14792 KB |
Output is correct |
40 |
Correct |
18 ms |
14796 KB |
Output is correct |
41 |
Correct |
14 ms |
14804 KB |
Output is correct |
42 |
Correct |
15 ms |
14916 KB |
Output is correct |
43 |
Correct |
14 ms |
14932 KB |
Output is correct |
44 |
Correct |
15 ms |
14804 KB |
Output is correct |
45 |
Correct |
8 ms |
14400 KB |
Output is correct |
46 |
Correct |
637 ms |
53804 KB |
Output is correct |
47 |
Correct |
651 ms |
71300 KB |
Output is correct |
48 |
Correct |
559 ms |
53992 KB |
Output is correct |
49 |
Correct |
602 ms |
54156 KB |
Output is correct |
50 |
Correct |
579 ms |
59076 KB |
Output is correct |
51 |
Correct |
589 ms |
60184 KB |
Output is correct |
52 |
Correct |
661 ms |
59216 KB |
Output is correct |
53 |
Correct |
587 ms |
60672 KB |
Output is correct |
54 |
Correct |
601 ms |
62116 KB |
Output is correct |
55 |
Correct |
618 ms |
60456 KB |
Output is correct |
56 |
Correct |
585 ms |
60168 KB |
Output is correct |
57 |
Correct |
600 ms |
62680 KB |
Output is correct |
58 |
Correct |
641 ms |
70544 KB |
Output is correct |
59 |
Correct |
581 ms |
63652 KB |
Output is correct |
60 |
Correct |
9 ms |
14292 KB |
Output is correct |
61 |
Correct |
919 ms |
62544 KB |
Output is correct |
62 |
Correct |
966 ms |
76196 KB |
Output is correct |
63 |
Correct |
866 ms |
61340 KB |
Output is correct |
64 |
Correct |
954 ms |
62944 KB |
Output is correct |
65 |
Correct |
845 ms |
61540 KB |
Output is correct |
66 |
Correct |
962 ms |
62932 KB |
Output is correct |
67 |
Correct |
920 ms |
61556 KB |
Output is correct |
68 |
Correct |
901 ms |
63128 KB |
Output is correct |
69 |
Correct |
969 ms |
64832 KB |
Output is correct |
70 |
Correct |
905 ms |
62932 KB |
Output is correct |
71 |
Correct |
864 ms |
62760 KB |
Output is correct |
72 |
Correct |
893 ms |
65828 KB |
Output is correct |
73 |
Correct |
959 ms |
72516 KB |
Output is correct |
74 |
Correct |
979 ms |
67816 KB |
Output is correct |