QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#212204 | #7502. Painting the Roads | chenxinyang2006 | WA | 2ms | 6108kb | C++14 | 3.9kb | 2023-10-13 11:32:49 | 2023-10-13 11:32:50 |
Judging History
answer
#include <bits/stdc++.h>
#define rep(i,j,k) for(int i=(j);i<=(k);i++)
#define per(i,j,k) for(int i=(j);i>=(k);i--)
#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define db double
#define ldb long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mkp make_pair
#define eb emplace_back
#define SZ(S) (int)S.size()
//#define mod 998244353
//#define mod 1000000007
#define inf 0x3f3f3f3f
#define linf 0x3f3f3f3f3f3f3f3f
using namespace std;
template <class T>
void chkmax(T &x,T y){
if(x < y) x = y;
}
template <class T>
void chkmin(T &x,T y){
if(x > y) x = y;
}
inline int popcnt(int x){
return __builtin_popcount(x);
}
inline int ctz(int x){
return __builtin_ctz(x);
}
/*ll power(ll p,int k = mod - 2){
ll ans = 1;
while(k){
if(k % 2 == 1) ans = ans * p % mod;
p = p * p % mod;
k /= 2;
}
return ans;
}*/
int T,n,m;
int cnt;
int head[5005];
struct eg{
int to,nxt,w,c;
}edge[10005];
void make(int u,int v,int w,int c){
edge[++cnt].to = v;
edge[cnt].w = w;
edge[cnt].c = c;
edge[cnt].nxt = head[u];
head[u] = cnt;
}
int a[5005],inst[5005],dep[5005];
int siz[5005],sum[5005];//siz[u] 子树内 bot 的数量
void dfs(int u,int fa){
inst[u] = a[u];
for(int i = head[u];i;i = edge[i].nxt){
int v = edge[i].to;
if(v == fa) continue;
a[v] = edge[i].c;
dep[v] = dep[u] + edge[i].w;
inst[u] ^= edge[i].c;
dfs(v,u);
}
}
int f[5005][5005],g[5005][5005],tf[5005],tg[5005];//f[u][i],u 子树内,多了 i 个 bot 点.g[u][i] u 子树内,多了 i 个标记点.g[u][0] 不存在
void mrg(int u,int v,int V){
rep(i,0,siz[u] + siz[v]) tf[i] = inf;
rep(i,0,sum[u] + sum[v]) tg[i] = inf;
rep(i,0,siz[u]){
rep(j,0,siz[v]) chkmin(tf[i + j],f[u][i] + f[v][j]);
rep(j,0,sum[v]){
if(i >= j) chkmin(tf[i - j],f[u][i] + g[v][j] - V * j);
else chkmin(tg[j - i],f[u][i] + g[v][j] - V * i);
}
}
rep(i,0,sum[u]){
rep(j,0,sum[v]) chkmin(tg[i + j],g[u][i] + g[v][j]);
rep(j,0,siz[v]){
if(i > j) chkmin(tg[i - j],g[u][i] + f[v][j] - V * j);
else chkmin(tf[j - i],g[u][i] + f[v][j] - V * i);
}
}
rep(i,0,siz[u] + siz[v]) f[u][i] = tf[i];
rep(i,0,sum[u] + sum[v]) g[u][i] = tg[i];
siz[u] += siz[v];
sum[u] += sum[v];
}
void dfs2(int u,int fa){
inst[u] ^= (siz[u] % 2);
sum[u] = inst[u];
fill(f[u],f[u] + siz[u] + 1,inf);
fill(g[u],g[u] + sum[u] + 1,inf);
f[u][siz[u]] = siz[u] * dep[u];
for(int i = head[u];i;i = edge[i].nxt){
int v = edge[i].to;
if(v == fa) continue;
dfs2(v,u);
mrg(u,v,2 * dep[u]);
}
if(u == 1) return;
if(inst[u]){
per(i,sum[u],2) g[u][i] = g[u][i - 1] + dep[u];
g[u][1] = f[u][0] + dep[u];
// printf("?? %d %d %d\n",u,f[u][0],g[u][1]);
per(i,siz[u] - 1,0) f[u][i] = f[u][i + 1] - dep[u];
f[u][siz[u]] = inf;
}
//允许 + 2 标记点
per(i,siz[u],2) chkmin(f[u][i - 2],f[u][i] - 2 * dep[u]);
if(sum[u] && siz[u]) chkmin(g[u][1],f[u][1]);
if(sum[u] >= 2) chkmin(g[u][2],f[u][0] + 2 * dep[u]);
rep(i,1,sum[u] - 2) chkmin(g[u][i + 2],g[u][i] + 2 * dep[u]);
}
void solve(){
scanf("%d%d",&n,&m);
cnt = 0;
fill(head + 1,head + n + 1,0);
fill(siz + 1,siz + n + 1,0);
rep(i,1,n - 1){
int u,v,w,c;
scanf("%d%d%d%d",&u,&v,&w,&c);
make(u,v,w,c);make(v,u,w,c);
}
dfs(1,0);
// rep(u,1,n) printf("%d ",inst[u]);
// printf("\n");
// rep(u,1,n) printf("%d ",dep[u]);
// printf("\n");
rep(i,1,m){
int u;
scanf("%d",&u);
siz[u]++;
}
dfs2(1,0);
/* rep(u,1,n){
printf("node %d siz %d sum %d\n",u,siz[u],sum[u]);
rep(i,0,siz[u]) printf("%d ",f[u][i]);
printf("\n");
rep(i,0,sum[u]) printf("%d ",g[u][i]);
printf("\n");
}*/
int ans = inf;
rep(i,0,m) chkmin(ans,f[1][i]);
if(ans <= 250000000) printf("%d\n",ans);
else printf("-1\n");
}
int main(){
// freopen("test.in","r",stdin);
scanf("%d",&T);
while(T--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 6108kb
input:
5 3 2 1 2 1 1 2 3 2 1 1 3 4 2 1 2 3 1 2 3 1 0 3 4 4 1 1 2 5 4 1 2 3 0 2 3 1 1 3 4 2 0 4 5 2 1 1 1 1 1 5 2 1 2 2 1 1 3 3 0 1 5 2 1 3 4 1 1 1 2 10 5 1 2 10 1 2 3 3 1 3 4 4 0 4 5 4 1 5 6 2 1 2 7 8 0 2 8 9 1 4 9 1 0 1 10 4 0 10 10 2 1 8
output:
3 9 21 -1 42
result:
ok 5 number(s): "3 9 21 -1 42"
Test #2:
score: -100
Wrong Answer
time: 2ms
memory: 6048kb
input:
1000 5 5 1 2 4 1 2 3 9 0 3 4 10 1 3 5 8 1 1 5 2 5 1 5 3 1 2 7 1 1 3 7 0 2 4 9 0 3 5 4 1 3 4 3 5 3 1 2 7 1 2 3 1 0 1 4 7 1 4 5 5 1 4 4 3 5 1 1 2 3 1 1 3 6 0 2 4 10 0 2 5 7 0 1 5 3 1 2 10 1 1 3 10 0 1 4 1 1 3 5 4 0 2 5 2 5 5 1 2 7 0 1 3 5 0 2 4 8 1 2 5 10 0 2 2 3 5 4 5 4 1 2 6 1 1 3 4 0 3 4 4 0 1 5 5 ...
output:
19 -1 19 3 11 -1 -1 0 8 0 38 1 1 0 5 28 12 -1 19 16 -1 13 -1 27 9 -1 16 14 0 12 16 -1 11 -1 7 -1 10 14 27 -1 10 -1 6 6 14 18 46 0 14 6 -1 5 8 22 -1 -1 17 -1 25 6 0 24 6 15 -1 13 22 -1 6 0 -1 10 5 -1 20 0 20 9 18 -1 10 0 16 -1 19 6 -1 11 11 4 15 20 11 0 8 8 31 8 -1 -1 -1 -1 11 -1 9 13 -1 -1 19 6 20 1...
result:
wrong answer 1st numbers differ - expected: '22', found: '19'