QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#465193 | #6406. Stage Clear | dengrk | WA | 0ms | 3964kb | C++14 | 3.0kb | 2024-07-06 18:25:14 | 2024-07-06 18:25:16 |
Judging History
answer
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N=75;
struct Edge{
int u,v;
};
Edge edge[N];
bool in_tree[N],ban[N];
int n,m,fa[N],cnt[N],fr[N];
int len_list=0,e[N],ne[N],h[N];
long long a[N],b[N],c[N],d[N];
void add_once(int a,int b){
e[len_list]=b;
ne[len_list]=h[a];
h[a]=len_list;
len_list++,cnt[a]++;
}
bool cmp(int u,int v){
if((c[u]<d[u])!=(c[v]<d[v])) return c[u]<d[u];
if(c[u]<d[u]) return c[u]<c[v];
return d[u]>d[v];
}
void dfs(int u,int from){
fr[u]=from;
for(int i=h[u];i>=0;i=ne[i])
dfs(e[i],i);
}
struct Heap{
int n,a[N],p[N];
void up(int u){
while(u>1&&cmp(a[u],a[u/2])){
swap(p[a[u]],p[a[u/2]]);
swap(a[u],a[u/2]); u/=2;
}
}
void down(int u){
while(u*2<=n&&cmp(a[u*2],a[u])||u*2<n&&cmp(a[u*2+1],a[u]))
if(u*2==n||cmp(a[u*2],a[u*2+1])){
swap(p[a[u]],p[a[u*2]]);
swap(a[u],a[u*2]); u*=2;
}
else{
swap(p[a[u]],p[a[u*2+1]]);
swap(a[u],a[u*2+1]); u=u*2+1;
}
}
void push(int x){
a[++n]=x; p[x]=n; up(n);
}
void pop(){
if(n==1){
p[a[1]]=0;
n=0; return ;
}
p[a[1]]=0,p[a[n]]=1;
a[1]=a[n--]; down(1);
}
void erase(int x){
if(n==1){
p[a[1]]=0;
n=0; return ;
}
p[a[n]]=p[x]; a[p[x]]=a[n--];
up(p[x]),down(p[x]); p[x]=0;
}
bool empty(){
return n==0;
}
int top(){
return a[1];
}
};
Heap hp;
int merge(int u){
int v=fa[u],i,la;
long long nd=b[u]-a[u]+b[v]-a[v];
long long na=a[v]-b[v]+a[u],nb=na+nd;
if(cnt[u]>cnt[v]){
for(i=h[v],la=-1;i>=0;i=ne[i])
if(e[i]==u)
if(la==-1) h[v]=ne[i];
else ne[la]=ne[i];
else fa[e[i]]=u,la=i;
fa[u]=fa[v],fr[u]=fr[v],e[fr[v]]=u;
if(la>=0) ne[la]=h[u],h[u]=h[v],h[v]=-1;
ban[v]=1,cnt[u]+=cnt[v],cnt[v]=0;
c[u]=na,d[u]=nb;
return u;
}
else{
for(i=h[u],la=-1;i>=0;i=ne[i])
fa[e[i]]=v,la=i;
if(la>=0) ne[la]=h[v],h[v]=h[u],h[u]=-1;
ban[u]=1,cnt[v]+=cnt[u],cnt[u]=0;
c[v]=na,d[v]=nb;
return v;
}
}
long long solve(){
int i,s1; len_list=0;
long long ans=0,sum=0;
memset(h,-1,sizeof h);
memset(ban,0,sizeof ban);
memset(cnt,0,sizeof cnt);
for(i=1;i<=m;i++){
if(!in_tree[i]) continue;
fa[edge[i].v]=edge[i].u;
add_once(edge[i].u,edge[i].v);
}
dfs(1,-1);
for(i=1;i<=n;i++)
c[i]=a[i],d[i]=b[i];
for(i=2;i<=n;i++) hp.push(i);
while(!hp.empty()){
s1=hp.top(),hp.pop();
if(fa[s1]==1) continue;
if(!cmp(s1,fa[s1])) continue;
hp.erase(fa[s1]);
hp.push(merge(s1));
}
hp.push(1);
while(!hp.empty()){
s1=hp.top(),hp.pop();
ans=min(ans,sum-c[s1]);
sum=sum-c[s1]+d[s1];
for(i=h[s1];i>=0;i=ne[i])
if(!ban[e[i]]) hp.push(e[i]);
}
return -ans;
}
int main(){
// freopen("HP.in","r",stdin);
// freopen("HP.out","w",stdout);
scanf("%d%d",&n,&m);
for(int i=2;i<=n;i++)
scanf("%lld%lld",&a[i],&b[i]);
for(int i=1;i<=m;i++)
scanf("%d%d",&edge[i].u,&edge[i].v);
for(int i=1;i<n;i++) in_tree[i]=1;
printf("%lld",solve());
// fclose(stdin);
// fclose(stdout);
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3964kb
input:
4 4 4 2 5 3 2 6 1 2 1 3 2 4 3 4
output:
4
result:
ok 1 number(s): "4"
Test #2:
score: -100
Wrong Answer
time: 0ms
memory: 3784kb
input:
15 14 254040392438309 117083115436273 500005748229691 557255157630172 821034233718230 865199673774998 659892147898798 987564141425694 81172575487567 811635577877255 751768357864605 341103322647288 454926350150218 140191090713900 921608121471585 659295670987251 223751724062143 505619245326640 8907765...
output:
2598018957561773
result:
wrong answer 1st numbers differ - expected: '1665396301509143', found: '2598018957561773'