// Super Idol的笑容
// 都没你的甜
// 八月正午的阳光
// 都没你耀眼
// 热爱105°C的你
// 滴滴清纯的蒸馏水
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << ": " << x << endl
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
#define rep(x,start,end) for(int x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
int n,q;
vector<ii> al[200005];
int w[400005];
vector<int> ans;
ii memo[400005];
ii pp[200005];
vector<ii> childs[200005];
int val[200005];
vector<int> f[200005];
vector<int> b[200005];
ii dfs(int i,int p,int id){
if (memo[id]!=ii(-1,-1)) return memo[id];
if (pp[i].fi==-1){
for (auto [it,id]:al[i]){
if (it==p){
pp[i]={it,id};
continue;
}
childs[i].pub({it,id});
}
sort(all(childs[i]));
f[i].pub(0);
for (auto [it,id]:childs[i]){
auto temp=dfs(it,i,id);
val[i]+=temp.fi;
f[i].pub(temp.se);
}
b[i].pub(0);
rep(x,sz(f[i]),1) b[i].pub(f[i][x]);
reverse(all(b[i]));
rep(x,1,sz(f[i])) f[i][x]=max(f[i][x],f[i][x-1]);
rep(x,sz(b[i])-1,0) b[i][x]=max(b[i][x],b[i][x+1]);
return memo[id]={val[i]+w[id^1],f[i].back()+w[id]};
}
else{
auto temp1=dfs(pp[i].fi,i,pp[i].se);
auto temp2=dfs(p,i,id^1);
int idx=lb(all(childs[i]),ii(p,-1))-childs[i].begin();
return memo[id]={
val[i]-temp2.fi+temp1.fi+w[id^1],
max({f[i][idx],b[i][idx+1],temp1.se})+w[id],
};
}
}
multiset<int,greater<int> > s[200005];
void dfs2(int i,int p){
for (auto [it,id]:al[i]){
if (it==p) continue;
dfs2(it,i);
int temp=*s[it].begin();
s[it].erase(s[it].begin());
s[it].insert(temp+w[id]);
if (sz(s[i])<sz(s[it])) swap(s[i],s[it]);
for (auto x:s[it]) s[i].insert(x);
}
if (s[i].empty()) s[i].insert(0);
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin.exceptions(ios::badbit | ios::failbit);
cin>>n;
int a,b;
rep(x,0,n-1){
cin>>a>>b;
al[a].pub({b,x<<1});
al[b].pub({a,x<<1|1});
cin>>w[x<<1]>>w[x<<1|1];
}
memset(memo,-1,sizeof(memo));
memset(pp,-1,sizeof(pp));
ans={0,0};
rep(x,1,n+1){
int tot=0;
for (auto [it,id]:al[x]) tot+=dfs(it,x,id).fi;
ans[1]=max(ans[1],tot);
}
int curr=0;
int best=-1;
rep(x,1,n+1) if (sz(al[x])==1){
auto temp=dfs(al[x][0].fi,x,al[x][0].se);
if (curr<temp.fi+temp.se){
curr=temp.fi+temp.se;
best=x;
}
}
curr=dfs(al[best][0].fi,best,al[best][0].se).fi;
dfs2(best,-1);
while (!s[best].empty()){
auto it=s[best].begin();
curr+=*it;
s[best].erase(it);
ans.pub(curr);
}
int tot=0;
rep(x,0,2*(n-1)) tot+=w[x];
cin>>q;
while (q--){
cin>>a;
cout<<tot-ans[min(a,sz(ans)-1)]<<endl;
}
}
Compilation message
designated_cities.cpp: In function 'int main()':
designated_cities.cpp:122:29: warning: 'void* memset(void*, int, size_t)' writing to an object of type 'struct std::pair<long long int, long long int>' with no trivial copy-assignment [-Wclass-memaccess]
122 | memset(memo,-1,sizeof(memo));
| ^
In file included from /usr/include/c++/10/bits/stl_algobase.h:64,
from /usr/include/c++/10/bits/specfun.h:45,
from /usr/include/c++/10/cmath:1927,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:41,
from designated_cities.cpp:8:
/usr/include/c++/10/bits/stl_pair.h:211:12: note: 'struct std::pair<long long int, long long int>' declared here
211 | struct pair
| ^~~~
designated_cities.cpp:123:25: warning: 'void* memset(void*, int, size_t)' writing to an object of type 'struct std::pair<long long int, long long int>' with no trivial copy-assignment [-Wclass-memaccess]
123 | memset(pp,-1,sizeof(pp));
| ^
In file included from /usr/include/c++/10/bits/stl_algobase.h:64,
from /usr/include/c++/10/bits/specfun.h:45,
from /usr/include/c++/10/cmath:1927,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:41,
from designated_cities.cpp:8:
/usr/include/c++/10/bits/stl_pair.h:211:12: note: 'struct std::pair<long long int, long long int>' declared here
211 | struct pair
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
37844 KB |
Output is correct |
2 |
Correct |
17 ms |
37800 KB |
Output is correct |
3 |
Correct |
20 ms |
37880 KB |
Output is correct |
4 |
Correct |
17 ms |
37804 KB |
Output is correct |
5 |
Correct |
19 ms |
37884 KB |
Output is correct |
6 |
Correct |
18 ms |
37844 KB |
Output is correct |
7 |
Correct |
18 ms |
37880 KB |
Output is correct |
8 |
Correct |
17 ms |
37808 KB |
Output is correct |
9 |
Correct |
17 ms |
37844 KB |
Output is correct |
10 |
Correct |
18 ms |
37864 KB |
Output is correct |
11 |
Correct |
17 ms |
37884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
37796 KB |
Output is correct |
2 |
Correct |
526 ms |
93640 KB |
Output is correct |
3 |
Correct |
457 ms |
99712 KB |
Output is correct |
4 |
Correct |
522 ms |
93856 KB |
Output is correct |
5 |
Correct |
552 ms |
85812 KB |
Output is correct |
6 |
Correct |
539 ms |
88484 KB |
Output is correct |
7 |
Correct |
504 ms |
86328 KB |
Output is correct |
8 |
Correct |
478 ms |
99620 KB |
Output is correct |
9 |
Correct |
451 ms |
91816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
37844 KB |
Output is correct |
2 |
Correct |
558 ms |
93772 KB |
Output is correct |
3 |
Correct |
463 ms |
99640 KB |
Output is correct |
4 |
Correct |
579 ms |
93720 KB |
Output is correct |
5 |
Correct |
559 ms |
85904 KB |
Output is correct |
6 |
Correct |
521 ms |
88980 KB |
Output is correct |
7 |
Correct |
466 ms |
91696 KB |
Output is correct |
8 |
Correct |
529 ms |
96204 KB |
Output is correct |
9 |
Correct |
453 ms |
91832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
37844 KB |
Output is correct |
2 |
Correct |
17 ms |
37800 KB |
Output is correct |
3 |
Correct |
20 ms |
37880 KB |
Output is correct |
4 |
Correct |
17 ms |
37804 KB |
Output is correct |
5 |
Correct |
19 ms |
37884 KB |
Output is correct |
6 |
Correct |
18 ms |
37844 KB |
Output is correct |
7 |
Correct |
18 ms |
37880 KB |
Output is correct |
8 |
Correct |
17 ms |
37808 KB |
Output is correct |
9 |
Correct |
17 ms |
37844 KB |
Output is correct |
10 |
Correct |
18 ms |
37864 KB |
Output is correct |
11 |
Correct |
17 ms |
37884 KB |
Output is correct |
12 |
Correct |
18 ms |
37880 KB |
Output is correct |
13 |
Correct |
24 ms |
38480 KB |
Output is correct |
14 |
Correct |
21 ms |
38476 KB |
Output is correct |
15 |
Correct |
22 ms |
38404 KB |
Output is correct |
16 |
Correct |
21 ms |
38476 KB |
Output is correct |
17 |
Correct |
21 ms |
38372 KB |
Output is correct |
18 |
Correct |
22 ms |
38532 KB |
Output is correct |
19 |
Correct |
21 ms |
38404 KB |
Output is correct |
20 |
Correct |
22 ms |
38392 KB |
Output is correct |
21 |
Correct |
20 ms |
38400 KB |
Output is correct |
22 |
Correct |
20 ms |
38440 KB |
Output is correct |
23 |
Correct |
20 ms |
38484 KB |
Output is correct |
24 |
Correct |
21 ms |
38428 KB |
Output is correct |
25 |
Correct |
20 ms |
38528 KB |
Output is correct |
26 |
Correct |
20 ms |
38536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
37796 KB |
Output is correct |
2 |
Correct |
526 ms |
93640 KB |
Output is correct |
3 |
Correct |
457 ms |
99712 KB |
Output is correct |
4 |
Correct |
522 ms |
93856 KB |
Output is correct |
5 |
Correct |
552 ms |
85812 KB |
Output is correct |
6 |
Correct |
539 ms |
88484 KB |
Output is correct |
7 |
Correct |
504 ms |
86328 KB |
Output is correct |
8 |
Correct |
478 ms |
99620 KB |
Output is correct |
9 |
Correct |
451 ms |
91816 KB |
Output is correct |
10 |
Correct |
20 ms |
37844 KB |
Output is correct |
11 |
Correct |
558 ms |
93772 KB |
Output is correct |
12 |
Correct |
463 ms |
99640 KB |
Output is correct |
13 |
Correct |
579 ms |
93720 KB |
Output is correct |
14 |
Correct |
559 ms |
85904 KB |
Output is correct |
15 |
Correct |
521 ms |
88980 KB |
Output is correct |
16 |
Correct |
466 ms |
91696 KB |
Output is correct |
17 |
Correct |
529 ms |
96204 KB |
Output is correct |
18 |
Correct |
453 ms |
91832 KB |
Output is correct |
19 |
Correct |
17 ms |
37796 KB |
Output is correct |
20 |
Correct |
522 ms |
93812 KB |
Output is correct |
21 |
Correct |
465 ms |
105928 KB |
Output is correct |
22 |
Correct |
527 ms |
99400 KB |
Output is correct |
23 |
Correct |
541 ms |
99624 KB |
Output is correct |
24 |
Correct |
523 ms |
99780 KB |
Output is correct |
25 |
Correct |
531 ms |
100444 KB |
Output is correct |
26 |
Correct |
601 ms |
99028 KB |
Output is correct |
27 |
Correct |
557 ms |
91724 KB |
Output is correct |
28 |
Correct |
531 ms |
94764 KB |
Output is correct |
29 |
Correct |
532 ms |
97756 KB |
Output is correct |
30 |
Correct |
534 ms |
100128 KB |
Output is correct |
31 |
Correct |
488 ms |
94588 KB |
Output is correct |
32 |
Correct |
513 ms |
102828 KB |
Output is correct |
33 |
Correct |
452 ms |
98084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
37844 KB |
Output is correct |
2 |
Correct |
17 ms |
37800 KB |
Output is correct |
3 |
Correct |
20 ms |
37880 KB |
Output is correct |
4 |
Correct |
17 ms |
37804 KB |
Output is correct |
5 |
Correct |
19 ms |
37884 KB |
Output is correct |
6 |
Correct |
18 ms |
37844 KB |
Output is correct |
7 |
Correct |
18 ms |
37880 KB |
Output is correct |
8 |
Correct |
17 ms |
37808 KB |
Output is correct |
9 |
Correct |
17 ms |
37844 KB |
Output is correct |
10 |
Correct |
18 ms |
37864 KB |
Output is correct |
11 |
Correct |
17 ms |
37884 KB |
Output is correct |
12 |
Correct |
18 ms |
37796 KB |
Output is correct |
13 |
Correct |
526 ms |
93640 KB |
Output is correct |
14 |
Correct |
457 ms |
99712 KB |
Output is correct |
15 |
Correct |
522 ms |
93856 KB |
Output is correct |
16 |
Correct |
552 ms |
85812 KB |
Output is correct |
17 |
Correct |
539 ms |
88484 KB |
Output is correct |
18 |
Correct |
504 ms |
86328 KB |
Output is correct |
19 |
Correct |
478 ms |
99620 KB |
Output is correct |
20 |
Correct |
451 ms |
91816 KB |
Output is correct |
21 |
Correct |
20 ms |
37844 KB |
Output is correct |
22 |
Correct |
558 ms |
93772 KB |
Output is correct |
23 |
Correct |
463 ms |
99640 KB |
Output is correct |
24 |
Correct |
579 ms |
93720 KB |
Output is correct |
25 |
Correct |
559 ms |
85904 KB |
Output is correct |
26 |
Correct |
521 ms |
88980 KB |
Output is correct |
27 |
Correct |
466 ms |
91696 KB |
Output is correct |
28 |
Correct |
529 ms |
96204 KB |
Output is correct |
29 |
Correct |
453 ms |
91832 KB |
Output is correct |
30 |
Correct |
18 ms |
37880 KB |
Output is correct |
31 |
Correct |
24 ms |
38480 KB |
Output is correct |
32 |
Correct |
21 ms |
38476 KB |
Output is correct |
33 |
Correct |
22 ms |
38404 KB |
Output is correct |
34 |
Correct |
21 ms |
38476 KB |
Output is correct |
35 |
Correct |
21 ms |
38372 KB |
Output is correct |
36 |
Correct |
22 ms |
38532 KB |
Output is correct |
37 |
Correct |
21 ms |
38404 KB |
Output is correct |
38 |
Correct |
22 ms |
38392 KB |
Output is correct |
39 |
Correct |
20 ms |
38400 KB |
Output is correct |
40 |
Correct |
20 ms |
38440 KB |
Output is correct |
41 |
Correct |
20 ms |
38484 KB |
Output is correct |
42 |
Correct |
21 ms |
38428 KB |
Output is correct |
43 |
Correct |
20 ms |
38528 KB |
Output is correct |
44 |
Correct |
20 ms |
38536 KB |
Output is correct |
45 |
Correct |
17 ms |
37796 KB |
Output is correct |
46 |
Correct |
522 ms |
93812 KB |
Output is correct |
47 |
Correct |
465 ms |
105928 KB |
Output is correct |
48 |
Correct |
527 ms |
99400 KB |
Output is correct |
49 |
Correct |
541 ms |
99624 KB |
Output is correct |
50 |
Correct |
523 ms |
99780 KB |
Output is correct |
51 |
Correct |
531 ms |
100444 KB |
Output is correct |
52 |
Correct |
601 ms |
99028 KB |
Output is correct |
53 |
Correct |
557 ms |
91724 KB |
Output is correct |
54 |
Correct |
531 ms |
94764 KB |
Output is correct |
55 |
Correct |
532 ms |
97756 KB |
Output is correct |
56 |
Correct |
534 ms |
100128 KB |
Output is correct |
57 |
Correct |
488 ms |
94588 KB |
Output is correct |
58 |
Correct |
513 ms |
102828 KB |
Output is correct |
59 |
Correct |
452 ms |
98084 KB |
Output is correct |
60 |
Correct |
17 ms |
37844 KB |
Output is correct |
61 |
Correct |
632 ms |
102896 KB |
Output is correct |
62 |
Correct |
510 ms |
107528 KB |
Output is correct |
63 |
Correct |
548 ms |
101700 KB |
Output is correct |
64 |
Correct |
562 ms |
104132 KB |
Output is correct |
65 |
Correct |
553 ms |
101856 KB |
Output is correct |
66 |
Correct |
564 ms |
102076 KB |
Output is correct |
67 |
Correct |
633 ms |
101592 KB |
Output is correct |
68 |
Correct |
603 ms |
94440 KB |
Output is correct |
69 |
Correct |
570 ms |
97088 KB |
Output is correct |
70 |
Correct |
547 ms |
98916 KB |
Output is correct |
71 |
Correct |
590 ms |
102752 KB |
Output is correct |
72 |
Correct |
543 ms |
95316 KB |
Output is correct |
73 |
Correct |
553 ms |
104996 KB |
Output is correct |
74 |
Correct |
498 ms |
102304 KB |
Output is correct |