QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#91714 | #68. Designated Cities | wolfrevo | 0 | 3ms | 3528kb | C++20 | 8.4kb | 2023-03-29 14:12:46 | 2023-03-29 14:12:52 |
Judging History
answer
//[Author] tuxedcat
//[Date] 2023.03.29
//[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);
auto dpinit=ARR(n,0);
func(int,init,int x,int p){
for(auto [y,w,rw]:g[x])
if(y!=p)
tsz[x]+=init(y,x);
for(auto [y,w,rw]:g[x])
if(y!=p)
dpinit[x]+=dpinit[y]+w;
return tsz[x];
};
init(0,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-k]+(y-k==tsz[R]?wR:0) (L은 x포함)
//
//TODO: 루트에서 한쪽 자식에 몰빵된 케이스 처리
//pv=부모쪽 서브트리에서 하나도 선택안할시의 추가값
//최소한 두개의 자식에 쪼개지도록 강제해야할듯.
//
//dp[x][i]=x서브트리에서 i개 선택안할때 비용, 단 최소한 서로다른 두개의 자식에서 선택이 존재해야함.
//서로 다른 두개의 자식선택 어떻게?
//하나만 사용한 경우의 배열(dp1)+두개이상 사용한경우(dpnew)
auto dpf=ARR(n,n+1,inf<int>());//dp1로 서로다른 두 자식 강제한 값
auto dp1=ARR(n,n+1,inf<int>());//그냥 서로다른 생각안하고 구하는 dp
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;
dp1[x][0]=0;
dp1[x][1]=0;
if(x==0)
int asdaf=0;
int xtsz=1;
for(auto [y,w,rw]:g[x])
if(y!=p){
for(int i=0;i<xtsz;i++)
for(int j=0;j<tsz[y];j++)
dpnew[i+j]=min(dpnew[i+j],dp1[x][i]+dpf[y][j]);
for(int i=0;i<=xtsz-1;i++)
for(int j=0;j<=tsz[y];j++)
dpnew[i+j]=min(dpnew[i+j],dpf[x][i]+dp1[y][j]+(j==tsz[y]?w:0));
dbg(dpnew);
for(int i=0;i<=n;i++)
dpf[x][i]=min(dpf[x][i],dpnew[i]);
dbg(dp1);
dbg(dpnew);
dbg(dpf);
for(int i=xtsz;i>=0;i--)
for(int j=tsz[y];j>=0;j--)
dp1[x][i+j]=min(dp1[x][i+j],dp1[x][i]+dp1[y][j]+(j==tsz[y]?w:0));
xtsz+=tsz[y];
dbg(dp1);
}
};
f(0,0);
dbg(dpf);
func(int,calc,int x,int p,int k,int pv){
int ret=(k==1?dp1[x][tsz[x]-0]:dpf[x][tsz[x]-k])+pv;
for(auto [y,w,rw]:g[x])
if(y!=p)
ret=min(ret,calc(y,x,k,pv+dpinit[x]-dpinit[y]-w+rw));
return ret;
};
int qn; cin>>qn;
Arr<int> q;
for(int i=0;i<qn;i++){
int x; cin>>x;
q.push_back(x);
println(calc(0,0,x,0));
}
}
详细
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 6
Accepted
time: 2ms
memory: 3528kb
input:
2 1 2 781089648 283888890 2 1 2
output:
283888890 0
result:
ok 2 lines
Test #2:
score: -6
Wrong Answer
time: 2ms
memory: 3468kb
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:
-2278565069 -2827396244 -2627577540 -2627577540 -2627577540 -1221137235 -1221137235 -1221137235 -1221137235 -1221137235 0 0 0 0 0 0
result:
wrong answer 1st lines differ - expected: '6311369523', found: '-2278565069'
Subtask #2:
score: 0
Memory Limit Exceeded
Test #12:
score: 7
Accepted
time: 2ms
memory: 3528kb
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: 3ms
memory: 3520kb
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: 0ms
memory: 3452kb
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%