QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#91389 | #68. Designated Cities | wolfrevo | 0 | 2ms | 3552kb | C++20 | 8.6kb | 2023-03-28 19:33:20 | 2023-03-28 19:33:21 |
Judging History
answer
//[Author] tuxedcat
//[Date] 2023.03.28
//[File] src/a.cpp
//[Library] https://github.com/tuxedcat/pslib
#pragma GCC optimize("O3")
// #pragma GCC optimize("O3")
// #pragma GCC target("avx2")
#include <bits/stdc++.h>
#define _EXT_CODECVT_SPECIALIZATIONS_H 1
#define _EXT_ENC_FILEBUF_H 1
using namespace std;
#pragma GCC diagnostic ignored "-Wparentheses"
#define SYSCALL_ALLOWED 1
#if SYSCALL_ALLOWED
#include <bits/extc++.h>
#include <ext/rope>
#endif
#define int i64
using fp=double; //long double,__float128
#define COUT_FP 10
#define FP_EPS 1e-11
#define CPP20 1
#define DBG_SETW 3
#define fi first
#define se second
#define mkp make_pair
#define mkt make_tuple
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define itos to_string
#define head(x) (x.begin())
#define tail(x) prev(x.end())
#define dbg(...) void(0)
#define dbgif(...) void(0)
#define dbg1(...) void(0)
#define dbg1if(...) void(0)
#define dbgprint(...) void(0)
#define dbgprintln(...) void(0)
using i64=long long;using u64=unsigned long long;using u32=unsigned;
using pint=pair<int,int>;using tint=tuple<int,int,int>;
template<class T>using Str=basic_string<T>;
template<class T,class CMP=less<>>using PQ=std::priority_queue<T,vector<T>,CMP>;
#if CPP20
template<class T>concept Printable=requires(T x){cout<<x;};
template<class T>concept NotPrintable=not Printable<T>;
template<class T>concept Iterable=requires(T x){x.begin();x.end();begin(x);end(x);};
#else
#define Printable class
#define NotPrintable class
#define Iterable class
#endif
//functions before include <arr.h>
//Math
#if !(CPP20)
#define countl_zero(x) __builtin_clzll(x)
#define popcount(x) __builtin_popcountll(x)
#define bit_ceil(x) (1<<clg2(x))
#endif
#if CPP20
#define sz ssize
#else
template<class T>int sz(const T& x){return x.size();}
#endif
int fdiv(int a,int b){if(b<0)a=-a,b=-b;return (a>0)?a/b:(a-b+1)/b;}
int cdiv(int a,int b){if(b<0)a=-a,b=-b;return (a>0)?(a+b-1)/b:a/b;}
i64 flg2(u64 x){return 63-countl_zero(x);}
i64 clg2(u64 x){return x-1==0?0:64-countl_zero(x-1);}
int fsqrt(i64 n) {i64 i=1;while(i*i<=n)i++;return i-1;}
int csqrt(i64 n) {i64 i=1;while(i*i<n)i++;return i;}
template<class T>T sq(T x){return x*x;}
template<class T>constexpr T inf(){return numeric_limits<T>::max()/2;}
template<class T> [[deprecated("use optional")]] constexpr T nan(){return inf<T>()+1;}
#if CPP20
template<typename T> concept MemberInf=requires(T t){t.inf();};
template<typename T> concept MemberNan=requires(T t){t.nan();};
template<MemberInf T>T inf(){return T().inf();}
template<MemberNan T> [[deprecated("use optional")]] T nan(){return T().nan();}
#endif
//IO & misc
template<class...A>ostream& osprint(ostream& os, A...a){return ((os<<a),...);}
#define print(...) osprint(cout,__VA_ARGS__)
#define println(...) osprint(cout,__VA_ARGS__,'\n')
template<class T,class U>bool assmin(T& a,U&& b){return a>b?a=b,true:false;}
template<class T,class U>bool assmax(T& a,U&& b){return a<b?a=b,true:false;}
void MUST(bool expr){
#if DEBUG
#include <csignal>
if(!expr)
raise(SIGINT);
#endif
}
#define ARG(...) __VA_ARGS__
#define func(RetT,fname,...) function<RetT(__VA_ARGS__)> fname=[&](__VA_ARGS__)->RetT
#define lam(expr,...) [&](__VA_ARGS__){return expr;}
#define lamp(expr,...) [](__VA_ARGS__){return expr;}
auto val2cmp(auto val){return [val](auto x, auto y){return mkp(val(x),x)<mkp(val(y),y);};}
template<class T, class P=vector<T>>
struct Arr:public P{
Arr(){P::shrink_to_fit();}
explicit Arr(signed n):P(n){}
explicit Arr(signed n,T init):P(n,init){}
Arr(initializer_list<T>il):P(il){}
Arr(auto its, auto ite):P(its,ite){}
inline T& operator[](signed i){return P::operator[](i<0?i+sz(*this):i);}
const T& operator[](signed i)const{return P::operator[](i<0?i+sz(*this):i);}
T& at(signed i){return *this[i];}
const T& at(signed i)const{return *this[i];}
};
template<class... A> auto ARR(auto n,A&&... a)
{if constexpr(!sizeof...(a)) return n; else return Arr(n,ARR(a...));}
//Consts
// const fp pi=numbers::pi_v<fp>,eps=FP_EPS;
const fp pi=acos(-1),eps=FP_EPS;
const int dir4[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
const int dir8[8][2]={{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1},{1,0},{1,1}};
//functions after include <arr.h>
template<class T>int argmin(const Arr<T>& a){return min_element(a.begin(),a.end())-a.begin();}
template<class T>int argmax(const Arr<T>& a){return max_element(a.begin(),a.end())-a.begin();}
Arr<int> permuinv(const Arr<int>& a){Arr<int> r(sz(a));for(int i=0;i<sz(a);i++)r[a[i]]=i;return r;}
template<class T,class U=Arr<int>>map<T,U> classify(const Arr<T>& a){
map<T,U> r;
for(int i=0;i<sz(a);i++)
r[a[i]].push_back(i);
return r;
}
#if CPP20
template<class T=int,class... Ts> requires (!Iterable<T>) tuple<T,Ts...> input(){
T x; cin>>x;
if constexpr (sizeof...(Ts)==0) return mkt(x);
else return tuple_cat(mkt(x),input<Ts...>());
}
template<class T=int,int n>std::array<T,n> input(){
std::array<T,n> a;
for(auto&i:a)
i=get<0>(input<T>());
return a;
}
string input(string& str){ cin>>str; return str; }
template<class T> requires (!Iterable<T>) T& input(T& a){ cin>>a; return a;}
template<class T> requires Iterable<T> T& input(T& a){ for(auto&i:a)input(i); return a; }
#else
#define input() [](){int x;cin>>x;return mkt(x);}()
// #define input(n) [](){Arr<int> a(n);for(auto&i:a)cin>>i;return a;}()
#endif
//Pre-runs
auto __PR=(cout<<fixed<<setprecision(COUT_FP),0);
#if !(DEBUG)
auto __PR_NDB=(ios::sync_with_stdio(0),cin.tie(0),cout.tie(0));
#endif
#define endl '\n'//Remove it when interactive
#define CHECK_INPUT 1
#define TC 1
#define TC_OUT_PREFIX ""//"Case #",ti,": "
signed main(){
void solve();
for(int ti=1,t=TC;ti<=t;ti++)
print(TC_OUT_PREFIX),
solve();
#if CHECK_INPUT
assert(cin.get()=='\n');
assert(cin.get()==EOF);
#endif
}
void solve(){
int n; cin>>n;
Arr<Arr<tint>> g(n);
for(int i=0;i<n-1;i++){
int a,b,c,d; cin>>a>>b>>c>>d;
a--,b--;
g[a].emplace_back(b,c,d);
g[b].emplace_back(a,d,c);
}
auto tsz=ARR(n,1);
func(int,inittsz,int x,int p){
for(auto [y,w,rw]:g[x])
if(y!=p)
tsz[x]+=inittsz(y,x);
return tsz[x];
};
inittsz(0,0);
auto dpf=ARR(n,n+1,inf<int>());
// func(int,init0,int x,int p){
// dpf[x][0]=0;
// for(auto [y,w]:g[x])
// if(y!=p)
// dpf[x][0]+=init0(y,x)+w;
// return dpf[x][0];
// };
// init0(0,0);
// dbg(dpf);
// func(int,init1,int x,int p){
// int longest=0;
// for(auto [y,w]:g[x])
// if(y!=p)
// longest=max(longest,init1(y,x)+w);
// dpf[x][1]=dpf[x][0]-longest;
// return longest;
// };
// init1(0,0);
// dbg(dpf);
//지금 dp정의가 선택/선택안함이 꼬였다..
//dp[x][i]=x서브트리에서 i개 선택안할때 비용이라고 풀어보자.
//NOTE:선택안한 리프는 비용이 듦
//dp[x][i>=sz(x)] = 0개선택 = 모든 edge합
//dp[x][i=sz(x)-1] = 1개선택 = 모든 edge합 - 제일 긴 경로값
//
//근데 이러면 infimal convolution꼴이랑 살짝 달라서 인덱스 조정을 위해 아래처럼 정의를 변형하자.
//dp[x][i] = x서브트리에서 i개 선택할때 비용
//dp[x][0] = 모든 edge합
//dp[x][1] = 모든 edge합 - 제일 긴 경로값
//dp[x][2] = ? dp[x][0~1]과아무 관계가 없는듯...
//
//처음의 정의를 사용해서 dp[x][0]을 구할 수 있을까? 있다.
//dp[x][i]=x서브트리에서 i개 선택안할때 비용
//dp[x][0~tsz[x]-leaf[x]] = 모든 리프 선택 = 0
//dp[x][tsz[x]-leaf[x]+1] = 제일 가까운 리프 선택취소
//복잡하니까 그냥 dp[x][0]=0만 둬도 될듯? 아래 점화식이 가능하면.
//dp[x][y] = dp[L][k]+dp[R][y-1-k]+(k==tsz[L]?wL:0)+(y-1-k==tsz[R]?wR:0)
//k+(y-1-k)=y-1 이지만 dp[R]을 평행이동하면 y로 맞춰줄 수 있다. 평행이동 필요한 슬로프트릭?
func(void,f,int x,int p){
auto dpnew=ARR(n+1,inf<int>());
for(auto [y,w,rw]:g[x])
if(y!=p)
f(y,x);
dpf[x][0]=0;
dpf[x][1]=0;
for(auto [y,w,rw]:g[x])
if(y!=p){
for(int i=0;i<=n;i++)
for(int j=0;j<=n;j++)
if(i+j<=n){
int val=dpf[x][i]+dpf[y][j];
if(j==tsz[y])
val+=w;
dpnew[i+j]=min(dpnew[i+j],val);
}
for(int i=0;i<=n;i++)
dpf[x][i]=min(dpf[x][i],dpnew[i]);
dbg(dpf);
}
};
f(0,0);
dbg(dpf);
func(int,calc1,int x,int p,int pv){
int ret=pv+dpf[x][tsz[x]];
for(auto [y,w,rw]:g[x])
if(y!=p)
ret=min(ret,calc1(y,x,pv+dpf[x][tsz[x]]-dpf[y][tsz[y]]-w+rw));
return ret;
};
int val1=calc1(0,0,0);
int qn; cin>>qn;
Arr<int> q;
for(int i=0;i<qn;i++){
int x; cin>>x;
q.push_back(x);
println(x==1?val1:dpf[0][n-x]);
}
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 6
Accepted
time: 0ms
memory: 3552kb
input:
2 1 2 781089648 283888890 2 1 2
output:
283888890 0
result:
ok 2 lines
Test #2:
score: 0
Accepted
time: 2ms
memory: 3428kb
input:
16 6 10 160848335 124052868 7 1 241203243 110601447 14 6 290972019 163072373 11 15 938517011 154373610 12 1 138651641 741445657 7 8 60218933 280830068 16 15 203079209 633547400 11 7 199606763 919756826 14 12 266702877 916493997 15 13 905937802 481991969 2 10 234605456 722866810 3 5 366455156 4966982...
output:
6311369523 2092762454 966156735 60218933 0 0 0 0 0 0 0 0 0 0 0 0
result:
ok 16 lines
Test #3:
score: 0
Accepted
time: 0ms
memory: 3424kb
input:
16 3 4 204022014 914663555 9 10 11007340 458844696 2 7 605164817 895349276 14 11 434326485 550918606 14 7 712866927 489761842 6 1 356406033 534499656 16 8 942553720 855217399 5 10 865707145 586883622 6 13 108330979 234031340 15 8 769531307 948036095 3 16 358448538 363203546 5 2 419315988 76297418 12...
output:
7413430560 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
result:
ok 16 lines
Test #4:
score: -6
Wrong Answer
time: 0ms
memory: 3420kb
input:
16 4 5 1000000000 1000000000 10 5 1000000000 1000000000 8 5 1000000000 1000000000 11 13 999999646 999999646 15 16 999998089 999998089 7 5 929 929 14 7 898 898 16 9 159 159 6 12 603 603 9 2 999997930 999997930 3 6 999999242 999999242 5 12 155 155 16 14 84 84 14 1 999998173 999998173 12 13 199 199 16 ...
output:
7999996107 4999996107 3999996107 2999996107 1999996262 999998089 0 0 0 0 0 0 0 0 0 0
result:
wrong answer 2nd lines differ - expected: '5999996107', found: '4999996107'
Subtask #2:
score: 0
Memory Limit Exceeded
Test #12:
score: 7
Accepted
time: 0ms
memory: 3524kb
input:
2 1 2 683402985 526289818 1 1
output:
526289818
result:
ok single line: '526289818'
Test #13:
score: -7
Memory Limit Exceeded
input:
200000 30498 170310 456566691 649436035 88553 73637 443596936 376869783 157116 8270 670934073 119072463 24742 48732 237943289 398782798 118620 71333 841086509 861755957 91523 118037 345609124 755508586 182978 92078 999023640 247489369 57480 73002 550952716 31090859 85037 151494 615937607 181113974 8...
output:
result:
Subtask #3:
score: 0
Memory Limit Exceeded
Test #21:
score: 9
Accepted
time: 2ms
memory: 3412kb
input:
2 2 1 92722556 873785501 1 2
output:
0
result:
ok single line: '0'
Test #22:
score: -9
Memory Limit Exceeded
input:
200000 99982 83075 709942852 92942003 168325 12929 879930937 637190556 85628 123672 784369088 731448156 34917 117619 569166498 663184347 92257 112058 369526210 824568522 32464 109884 258245678 691717157 129594 115097 627894556 937225369 54700 187473 81636213 510866047 52020 197198 577461848 47343465...
output:
result:
Subtask #4:
score: 0
Skipped
Dependency #1:
0%
Subtask #5:
score: 0
Memory Limit Exceeded
Test #45:
score: 17
Accepted
time: 1ms
memory: 3360kb
input:
2 1 2 543195986 144983073 1 1
output:
144983073
result:
ok single line: '144983073'
Test #46:
score: -17
Memory Limit Exceeded
input:
200000 73974 46059 151001152 42729969 112523 175399 580450366 914798605 65645 46109 848220487 698683602 63048 106502 596698349 144038980 98888 11174 948423025 972032422 115490 95315 788936645 231068151 5185 187319 690370465 616111588 10331 161483 606127487 195799307 133831 170948 694137989 490575964...
output:
result:
Subtask #6:
score: 0
Skipped
Dependency #1:
0%