Logo Wy Online Judge

WyOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#531#110. 「TFXOI Round 2」最小价值最大树__vector__100392ms130048kbC++143.6kb2025-06-01 13:12:062025-06-01 13:12:08

answer

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(auto i=(a);i<=(b);i++)
#define REP(i,a,b) for(auto i=(a);i>=(b);i--)
#define FORK(i,a,b,k) for(auto i=(a);i<=(b);i+=(k))
#define REPK(i,a,b,k) for(auto i=(a);i>=(b);i-=(k))
#define pb push_back
#define mkpr make_pair
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
template<class T>
void ckmx(T& a,T b){
    a=max(a,b);
}
template<class T>
void ckmn(T& a,T b){
    a=min(a,b);
}
template<class T>
T gcd(T a,T b){
    return !b?a:gcd(b,a%b);
}
template<class T>
T lcm(T a,T b){
    return a/gcd(a,b)*b;
}
#define gc getchar()
#define eb emplace_back
#define pc putchar
#define ep empty()
#define fi first
#define se second
#define pln pc('\n');
#define islower(ch) (ch>='a'&&ch<='z')
#define isupper(ch) (ch>='A'&&ch<='Z')
#define isalpha(ch) (islower(ch)||isupper(ch))
template<class T>
void wrint(T x){
    if(x<0){
        x=-x;
        pc('-');
    }
    if(x>=10){
        wrint(x/10);
    }
    pc(x%10^48);
}
template<class T>
void wrintln(T x){
    wrint(x);
    pln
}
template<class T>
void read(T& x){
    x=0;
    int f=1;
    char ch=gc;
    while(!isdigit(ch)){
        if(ch=='-')f=-1;
        ch=gc;
    }
    while(isdigit(ch)){
        x=(x<<1)+(x<<3)+(ch^48);
        ch=gc;
    }
    x*=f;
}
typedef __int128 sll;
const int maxn=2005;
int n,lim;
sll a[maxn];
vi gp[maxn];
sll f[maxn][maxn][2];
// u/u exist/u.optimes : minimul xor sum
int siz[maxn];
void dfs(int u,int _fa){
    siz[u]=1;
    for(int& v:gp[u]){
        if(v==_fa)continue;
        dfs(v,u);
        siz[u]+=siz[v];
    }
    sll g[2][maxn];
    {
        FOR(b,0,1){
            FOR(i,0,n)g[b][i]=1e30;
        }
        // u 存在
        g[0][0]=0;
        bool cur=0;
        int sz=0;
        for(int& v:gp[u]){
            if(v==_fa)continue;
            FOR(i,0,sz){
                FOR(j,0,siz[v]){
                    // v 存在
                    ckmn(g[cur^1][i+j],g[cur][i]+f[v][j][1]+(a[u]^a[v]));
                    // v 不存在
                    ckmn(g[cur^1][i+j],g[cur][i]+f[v][j][0]);
                }
            }
            FOR(i,0,sz)g[cur][i]=1e30;
            sz+=siz[v];
            cur^=1;
        }
        FOR(i,0,siz[u]){
            f[u][i][1]=g[cur][i];
        }
    }
    {
        FOR(b,0,1){
            FOR(i,0,n)g[b][i]=1e30;
        }
        // u 不存在
        g[0][1]=0;
        bool cur=0;
        int sz=1;
        for(int& v:gp[u]){
            if(v==_fa)continue;
            FOR(i,0,sz){
                FOR(j,0,siz[v]){
                    // v 存在
                    ckmn(g[cur^1][i+j],g[cur][i]+f[v][j][1]);
                    // v 不存在
                    ckmn(g[cur^1][i+j],g[cur][i]+f[v][j][0]);
                }
            }
            FOR(i,0,sz)g[cur][i]=1e30;
            sz+=siz[v];
            cur^=1;
        }
        FOR(i,1,siz[u]){
            f[u][i][0]=g[cur][i];
        }
    }
}
void solve(int id_of_test){
	read(n);
    read(lim);
    FOR(i,1,n){
        read(a[i]);
    }
    FOR(i,1,n-1){
        int u,v;
        read(u);
        read(v);
        gp[u].eb(v);
        gp[v].eb(u);
    }
    FOR(i,0,n){
        FOR(j,0,n){
            f[i][j][0]=f[i][j][1]=1e30;
        }
    }
    dfs(1,0);
    FOR(k,0,lim){
        sll ans=min(f[1][k][0],f[1][k][1]);
        wrint(ans);
        pc(' ');
    }
    pln
}
int main()
{
	int T;
	T=1;
	FOR(_,1,T){
		solve(_);
	}
	return 0;
}

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 3
Accepted
time: 0ms
memory: 3472kb

input:

5 0
510175439626612034 6596952067756145189 5779497631883447415 8185240559919604254 43468913184946877...

output:

14212939962541038478 

result:

ok "14212939962541038478"

Test #2:

score: 3
Accepted
time: 0ms
memory: 3452kb

input:

8 0
4559950977633133606 2542004663776996878 3465276866487769631 937567271210899036 70079796349124845...

output:

33914127674312087447 

result:

ok "33914127674312087447"

Test #3:

score: 3
Accepted
time: 1ms
memory: 3316kb

input:

5 0
7174119998432138114 6934341933311875478 6305886798469309992 3853618993920233569 4501830989578771...

output:

18061976281863573091 

result:

ok "18061976281863573091"

Test #4:

score: 3
Accepted
time: 0ms
memory: 3416kb

input:

10 0
1189750870287790481 4385190558591326584 1758518040621862194 8706594208234159506 703632637133167...

output:

40104727041383211951 

result:

ok "40104727041383211951"

Test #5:

score: 3
Accepted
time: 0ms
memory: 3436kb

input:

6 0
7032382021800626318 870327641286605176 1725015830041916645 4650381066679235502 86737063043511396...

output:

26349681581844445795 

result:

ok "26349681581844445795"

Test #6:

score: 3
Accepted
time: 0ms
memory: 3556kb

input:

18 0
1689119273816819179 1794383924810453388 6613681571852149166 868900023103027883 6845418350614405...

output:

65438479531370435851 

result:

ok "65438479531370435851"

Test #7:

score: 3
Accepted
time: 0ms
memory: 3544kb

input:

20 0
2713465273963823755 2573754894443784500 6578752932162594288 8919460384770799480 677707207806788...

output:

91496934421306502830 

result:

ok "91496934421306502830"

Test #8:

score: 3
Accepted
time: 0ms
memory: 3368kb

input:

19 0
996954429227373972 1450934991951040336 1706933964755561002 1585589109036021549 7345647226073144...

output:

71135725884905055808 

result:

ok "71135725884905055808"

Test #9:

score: 3
Accepted
time: 0ms
memory: 3568kb

input:

20 0
3196123577319914210 3026383263644213730 4309615516823812565 7661772122729470942 165444389731327...

output:

73503589837812386151 

result:

ok "73503589837812386151"

Test #10:

score: 3
Accepted
time: 0ms
memory: 3416kb

input:

17 0
3440172044107181215 2363096865149507 8809797196834309228 791679778298586624 1998354356566922914...

output:

83412683568438719317 

result:

ok "83412683568438719317"

Test #11:

score: 3
Accepted
time: 39ms
memory: 129776kb

input:

1997 0
2413967162314441723 4627983814746725354 7204389880383585235 9022774696289620434 1039606360994...

output:

9190379135851168563753 

result:

ok "9190379135851168563753"

Test #12:

score: 3
Accepted
time: 36ms
memory: 129900kb

input:

1999 0
3190075001042825174 6278470195393803385 4144899155020314836 5954203197398491731 7794304878290...

output:

9398156537433728265991 

result:

ok "9398156537433728265991"

Test #13:

score: 3
Accepted
time: 37ms
memory: 129596kb

input:

1996 0
4645498754327397687 3259195174448393133 5477770443259531231 7670747834315382176 1960371513551...

output:

9227079380454376042296 

result:

ok "9227079380454376042296"

Test #14:

score: 3
Accepted
time: 37ms
memory: 129816kb

input:

2000 0
5461446565555594869 3520408511021008361 5439527800335748061 8960418061787902174 4079454053506...

output:

9194738917543477021189 

result:

ok "9194738917543477021189"

Test #15:

score: 3
Accepted
time: 39ms
memory: 129608kb

input:

1999 0
3638275256051318682 946978570246069406 603499494167642579 7934345094229062189 866624666788802...

output:

9216716696627236664872 

result:

ok "9216716696627236664872"

Test #16:

score: 3
Accepted
time: 1ms
memory: 3472kb

input:

6 6
319128050392344538 5745332412338211947 880678554096491491 7589566123902595099 836882036650554225...

output:

21727620983085732720 8391183137008816126 1403051177067169279 0 0 0 0 

result:

ok 7 tokens

Test #17:

score: 3
Accepted
time: 1ms
memory: 3536kb

input:

6 5
6758146749941425098 96450801659085260 614235295196028281 9008634755880495616 6464793807658537143...

output:

31070397624263339381 5850664135373311438 0 0 0 0 

result:

ok 6 tokens

Test #18:

score: 3
Accepted
time: 0ms
memory: 3328kb

input:

6 6
5477851248062346249 4465397873810863017 1071081361639626894 286148143844136820 35444413912059117...

output:

36830799332291851346 9014173070118233074 0 0 0 0 0 

result:

ok 7 tokens

Test #19:

score: 3
Accepted
time: 1ms
memory: 3440kb

input:

6 4
1089206109644109717 6619511944265926706 956014605204581792 6333224956825489770 18703732445076454...

output:

27526148394647596865 7081477758161641532 0 0 0 

result:

ok 5 tokens

Test #20:

score: 3
Accepted
time: 0ms
memory: 3384kb

input:

6 6
374004826914604001 5421725174823136160 2335452219378089407 2553473276182144459 33195442486784912...

output:

24117647562538642491 11080667359946427593 0 0 0 0 0 

result:

ok 7 tokens

Test #21:

score: 3
Accepted
time: 0ms
memory: 9616kb

input:

100 100
2812876381686415562 4735723303340919337 9085614385668405324 2972723284624302820 218809979005...

output:

389785298544786591066 363337309122209099182 337363100540903807899 312363989738234789028 288108820193...

result:

ok 101 tokens

Test #22:

score: 3
Accepted
time: 1ms
memory: 9820kb

input:

100 98
1835194717493038361 441361085187825799 5426618863386181539 9010072601395127516 14696650760210...

output:

449362667788454803395 420209796235279982453 392558724105730950954 367147388791329383172 345485062376...

result:

ok 99 tokens

Test #23:

score: 3
Accepted
time: 0ms
memory: 9984kb

input:

100 99
3275729385455006480 1313012606525101028 4252825130770802051 2926176412872778598 4022852443475...

output:

475953107732520637574 431401037750861209837 400255421090987720405 369840924538889292206 345810258212...

result:

ok 100 tokens

Test #24:

score: 3
Accepted
time: 0ms
memory: 9832kb

input:

100 99
1787976370943210744 4783361358963911473 7067607835671299505 6819648265660794439 4932582752726...

output:

502701273534797122053 456394341966066648912 417905658259709434641 385410506293683890147 360620937413...

result:

ok 100 tokens

Test #25:

score: 3
Accepted
time: 0ms
memory: 9808kb

input:

100 100
8406737168567537231 1182864760583185957 7468757832081004585 4432995763781422035 916903793353...

output:

438868446889222473574 403867940752266335408 372686170221036876712 347010691149399376674 322267912701...

result:

ok 101 tokens

Test #26:

score: 3
Accepted
time: 38ms
memory: 129788kb

input:

2000 1999
4820601793876374302 8032446208946135445 6354733612084503538 3280770731041913463 1322480481...

output:

9129969522249841595099 9065881565924504756074 9008131000682841649049 8956357017181790349604 89056449...

result:

ok 2000 tokens

Test #27:

score: 3
Accepted
time: 40ms
memory: 129468kb

input:

2000 1999
4358947279043860955 8165567931503929444 4783060622705130210 9147169837145643732 7194271155...

output:

9227315711762459012413 9170330564874262236703 9116521348561757271730 9064498475718181884222 90151539...

result:

ok 2000 tokens

Test #28:

score: 3
Accepted
time: 41ms
memory: 129856kb

input:

2000 1999
7163003834541799495 9102243778203669083 8914859618403495223 581238758398098538 55015305205...

output:

9119168866130322225548 9057881854851474890732 8999711884133740368528 8950406942229272735283 89012998...

result:

ok 2000 tokens

Test #29:

score: 3
Accepted
time: 41ms
memory: 130048kb

input:

2000 1998
6394992736571065989 6580223237211957844 7659662379459106935 5968862074870117304 5194420454...

output:

9359540281014487940695 9295132733934757427098 9238048875608946260810 9183592279212166359044 91325605...

result:

ok 1999 tokens

Test #30:

score: 3
Accepted
time: 39ms
memory: 129836kb

input:

2000 1998
9068940048516030253 1766322685404958036 7891130061408936557 7946950238895421222 6226814337...

output:

9247270992727747956921 9190947583651200143831 9136473885217318634391 9085269136233207749435 90343943...

result:

ok 1999 tokens