#pragma GCC optimize("Ofast,unroll-loops,O3")
#pragma GCC optimize("avx,avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,fma,tune=native")
#include<bits/stdc++.h>
//#include<bits/extc++.h>
//#pragma pack(1)
#define fast ios::sync_with_stdio(0); cin.tie(0);
#define pii pair<int,int>
#define x first
#define y second
#define N 200015
using namespace std;
//using namespace __gnu_pbds;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update> order_multiset;
//typedef tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> order_set;
long long n,dp[N],fa[N],ans[N],mx[N],pos[N],total=0,s,t;
vector<int>g[N],leaf;
set<pair<long long,long long> >st[N];
map<pii,int>cost;
void count_max(int p){
if (!st[p].empty()){
pii big=*(--st[p].end());
mx[p]=big.x; pos[p]=big.y;
}
else mx[p]=0,pos[p]=p;
}
void Insert(int p,int i,int val){
int cs=mx[i]+val;
st[p].insert({cs,pos[i]});
count_max(p);
}
void Erase(int p,int i,int val){
int cs=mx[i]+val;
st[p].erase({cs,pos[i]});
count_max(p);
}
void dfs1(int p,int lp){
pos[p]=p;
for (auto i:g[p]){
if (i!=lp){
dfs1(i,p);
dp[p]=dp[p]+dp[i]+cost[{i,p}];
Insert(p,i,cost[{p,i}]);
}
}
}
void dfs2(int p,int lp){
dp[p]=(dp[p]+dp[lp]+cost[{lp,p}]);
Insert(p,lp,cost[{p,lp}]);
ans[1]=min(ans[1],total-dp[p]);
if (ans[2]>total-dp[p]-mx[p]){
ans[2]=total-dp[p]-mx[p];
s=p; t=pos[p];
}
for (auto i:g[p]){
if (i!=lp){
Erase(p,i,cost[{p,i}]);
dp[p]=dp[p]-dp[i]-cost[{i,p}];
dfs2(i,p);
dp[p]=dp[p]+dp[i]+cost[{i,p}];
Insert(p,i,cost[{p,i}]);
}
}
Erase(p,lp,cost[{p,lp}]);
dp[p]=(dp[p]-dp[lp]-cost[{lp,p}]);
}
signed main(){
fast
cin>>n;
for (int i=0;i<=n;i++){
ans[i]=1e18;
cost[{0,i}]=0; cost[{i,0}]=0;
}
for (int i=1;i<n;i++){
int a,b,c,d; cin>>a>>b>>c>>d;
total=(total+c+d);
g[a].push_back(b);
g[b].push_back(a);
cost[{a,b}]=c; cost[{b,a}]=d;
}
dfs1(1,0);
dfs2(1,0);
int q; cin>>q;
while (q--){
int c; cin>>c;
cout<<ans[c]<<'\n';
}
}
Compilation message
designated_cities.cpp:2:88: warning: bad option '-favx' to pragma 'optimize' [-Wpragmas]
2 | #pragma GCC optimize("avx,avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,fma,tune=native")
| ^
designated_cities.cpp:2:88: warning: bad option '-favx2' to pragma 'optimize' [-Wpragmas]
designated_cities.cpp:2:88: warning: bad option '-fsse' to pragma 'optimize' [-Wpragmas]
designated_cities.cpp:2:88: warning: bad option '-fsse2' to pragma 'optimize' [-Wpragmas]
designated_cities.cpp:2:88: warning: bad option '-fsse3' to pragma 'optimize' [-Wpragmas]
designated_cities.cpp:2:88: warning: bad option '-fssse3' to pragma 'optimize' [-Wpragmas]
designated_cities.cpp:2:88: warning: bad option '-fsse4' to pragma 'optimize' [-Wpragmas]
designated_cities.cpp:2:88: warning: bad option '-fpopcnt' to pragma 'optimize' [-Wpragmas]
designated_cities.cpp:2:88: warning: bad option '-fabm' to pragma 'optimize' [-Wpragmas]
designated_cities.cpp:2:88: warning: bad option '-fmmx' to pragma 'optimize' [-Wpragmas]
designated_cities.cpp:2:88: warning: bad option '-ffma' to pragma 'optimize' [-Wpragmas]
designated_cities.cpp:2:88: warning: bad option '-ftune=native' to pragma 'optimize' [-Wpragmas]
designated_cities.cpp:20:21: warning: bad option '-favx' to attribute 'optimize' [-Wattributes]
20 | void count_max(int p){
| ^
designated_cities.cpp:20:21: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:20:21: warning: bad option '-fsse' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:20:21: warning: bad option '-fsse2' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:20:21: warning: bad option '-fsse3' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:20:21: warning: bad option '-fssse3' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:20:21: warning: bad option '-fsse4' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:20:21: warning: bad option '-fpopcnt' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:20:21: warning: bad option '-fabm' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:20:21: warning: bad option '-fmmx' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:20:21: warning: bad option '-ffma' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:20:21: warning: bad option '-ftune=native' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:27:32: warning: bad option '-favx' to attribute 'optimize' [-Wattributes]
27 | void Insert(int p,int i,int val){
| ^
designated_cities.cpp:27:32: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:27:32: warning: bad option '-fsse' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:27:32: warning: bad option '-fsse2' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:27:32: warning: bad option '-fsse3' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:27:32: warning: bad option '-fssse3' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:27:32: warning: bad option '-fsse4' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:27:32: warning: bad option '-fpopcnt' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:27:32: warning: bad option '-fabm' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:27:32: warning: bad option '-fmmx' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:27:32: warning: bad option '-ffma' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:27:32: warning: bad option '-ftune=native' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:32:31: warning: bad option '-favx' to attribute 'optimize' [-Wattributes]
32 | void Erase(int p,int i,int val){
| ^
designated_cities.cpp:32:31: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:32:31: warning: bad option '-fsse' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:32:31: warning: bad option '-fsse2' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:32:31: warning: bad option '-fsse3' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:32:31: warning: bad option '-fssse3' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:32:31: warning: bad option '-fsse4' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:32:31: warning: bad option '-fpopcnt' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:32:31: warning: bad option '-fabm' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:32:31: warning: bad option '-fmmx' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:32:31: warning: bad option '-ffma' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:32:31: warning: bad option '-ftune=native' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:37:23: warning: bad option '-favx' to attribute 'optimize' [-Wattributes]
37 | void dfs1(int p,int lp){
| ^
designated_cities.cpp:37:23: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:37:23: warning: bad option '-fsse' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:37:23: warning: bad option '-fsse2' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:37:23: warning: bad option '-fsse3' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:37:23: warning: bad option '-fssse3' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:37:23: warning: bad option '-fsse4' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:37:23: warning: bad option '-fpopcnt' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:37:23: warning: bad option '-fabm' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:37:23: warning: bad option '-fmmx' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:37:23: warning: bad option '-ffma' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:37:23: warning: bad option '-ftune=native' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:47:23: warning: bad option '-favx' to attribute 'optimize' [-Wattributes]
47 | void dfs2(int p,int lp){
| ^
designated_cities.cpp:47:23: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:47:23: warning: bad option '-fsse' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:47:23: warning: bad option '-fsse2' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:47:23: warning: bad option '-fsse3' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:47:23: warning: bad option '-fssse3' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:47:23: warning: bad option '-fsse4' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:47:23: warning: bad option '-fpopcnt' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:47:23: warning: bad option '-fabm' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:47:23: warning: bad option '-fmmx' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:47:23: warning: bad option '-ffma' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:47:23: warning: bad option '-ftune=native' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:67:13: warning: bad option '-favx' to attribute 'optimize' [-Wattributes]
67 | signed main(){
| ^
designated_cities.cpp:67:13: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:67:13: warning: bad option '-fsse' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:67:13: warning: bad option '-fsse2' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:67:13: warning: bad option '-fsse3' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:67:13: warning: bad option '-fssse3' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:67:13: warning: bad option '-fsse4' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:67:13: warning: bad option '-fpopcnt' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:67:13: warning: bad option '-fabm' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:67:13: warning: bad option '-fmmx' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:67:13: warning: bad option '-ffma' to attribute 'optimize' [-Wattributes]
designated_cities.cpp:67:13: warning: bad option '-ftune=native' to attribute 'optimize' [-Wattributes]
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Incorrect |
9 ms |
14420 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
14420 KB |
Output is correct |
2 |
Correct |
1765 ms |
89852 KB |
Output is correct |
3 |
Correct |
1946 ms |
117696 KB |
Output is correct |
4 |
Correct |
1765 ms |
89700 KB |
Output is correct |
5 |
Correct |
1858 ms |
89648 KB |
Output is correct |
6 |
Correct |
1799 ms |
93620 KB |
Output is correct |
7 |
Execution timed out |
2037 ms |
89932 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Incorrect |
1763 ms |
89720 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Incorrect |
9 ms |
14420 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
14420 KB |
Output is correct |
2 |
Correct |
1765 ms |
89852 KB |
Output is correct |
3 |
Correct |
1946 ms |
117696 KB |
Output is correct |
4 |
Correct |
1765 ms |
89700 KB |
Output is correct |
5 |
Correct |
1858 ms |
89648 KB |
Output is correct |
6 |
Correct |
1799 ms |
93620 KB |
Output is correct |
7 |
Execution timed out |
2037 ms |
89932 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Incorrect |
9 ms |
14420 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |