QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#483498 | #6738. Cover | ucup-team052 | WA | 450ms | 207952kb | C++14 | 3.3kb | 2024-07-18 18:04:17 | 2024-07-18 18:04:17 |
Judging History
answer
#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
#define D(...) fprintf(stderr,__VA_ARGS__)
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back
using namespace std;
using LL=long long;
const LL INFLL=0X3F3F3F3F3F3F3F3FLL;
const int N=500005,M=500005,K=20;
int n,m,K0,fa[N][K],dep[N],tin[N],tou[N],idx;
vector<int>e[N];
struct node{
int a,b,w,c;
bool operator<(const node&rhs)const{
return dep[c]!=dep[rhs.c]?dep[c]>dep[rhs.c]:c<rhs.c;
}
}A[M];
void dfs1(int k1,int k2){tin[k1]=++idx,dep[k1]=dep[k2]+1;fa[k1][0]=k2;if(k2)e[k1].erase(find(e[k1].begin(),e[k1].end(),k2));rep(i,1,K-1)fa[k1][i]=fa[fa[k1][i-1]][i-1];for(auto&x:e[k1])if(x!=k2)dfs1(x,k1);tou[k1]=idx;}
int jump(int k1,int k2){
per(i,K-1,0)if(k2>>i&1)k1=fa[k1][i];
return k1;
}
int LCA(int k1,int k2){
if(dep[k1]<dep[k2])swap(k1,k2);
int dt=dep[k1]-dep[k2];
per(i,K-1,0)if(dt>>i&1)k1=fa[k1][i];
if(k1==k2)return k1;
per(i,K-1,0)if(fa[k1][i]!=fa[k2][i])k1=fa[k1][i],k2=fa[k2][i];
return fa[k1][0];
}
struct fenwick{
LL tr[N];
void add(int x,LL y){
for(int i=x;i<N;i+=i&-i)tr[i]+=y;
}
void add(int l,int r,LL x){
add(l,x);
add(r+1,-x);
}
LL query(int x){
LL y=0;
for(int i=x;i;i&=i-1)y+=tr[i];
return y;
}
}fwk;
struct dsu{
int fa[N];
dsu(){rep(i,0,N-1)fa[i]=i;}
int fd(int x){return fa[x]==x?x:fa[x]=fd(fa[x]);}
}o;
LL rem[M];
vector<int>vec[N];
LL ans;
vector<LL>f[N];
int id[N];
void push(int u,int mask,LL dt){
for(int i=mask;i<(1<<SZ(e[u]));++i|=mask){
f[u][i]+=dt;
}
}
int up(int u,LL &dt){
int v=fa[u][0];
LL ret=INFLL;
for(int i=1<<id[u];i<(1<<SZ(e[v]));++i|=1<<id[u]){
ret=min(ret,f[v][i]);
}
LL t=min(dt,ret);
for(int i=1<<id[u];i<(1<<SZ(e[v]));++i|=1<<id[u]){
f[v][i]-=t;
}
dt-=t;
ret-=t;
return t;
}
void work(int u,int v,LL &dt){
while(1){
u=o.fd(u);
if(dep[u]<=dep[v])break;
LL t=up(u,dt);
// D("! %d %lld\n",u,t);
fwk.add(tin[u],tou[u],t);
if(!dt){
break;
}
if(fa[u][0]!=v){
o.fa[u]=fa[u][0];
}else{
break;
}
}
}
LL qsum(int a,int b,int c){
return fwk.query(tin[a])+fwk.query(tin[b])-fwk.query(tin[c])*2;
}
int main(){
#ifdef xay5421
freopen("a.in","r",stdin);
#endif
cin>>n>>m>>K0;
rep(i,2,n){int x,y;cin>>x>>y;e[x].pb(y),e[y].pb(x);}
dfs1(1,0);
rep(i,1,m){
cin>>A[i].a>>A[i].b>>A[i].w;
A[i].c=LCA(A[i].a,A[i].b);
if(A[i].b==A[i].c)swap(A[i].a,A[i].b);
}
rep(i,1,n)f[i].resize(1<<SZ(e[i]));
rep(i,1,n)rep(j,0,SZ(e[i])-1)id[e[i][j]]=j;
sort(A+1,A+m+1);
for(int i=1,j;i<=m;i=j){
j=i+1;
while(j<=m&&A[i].c==A[j].c)++j;
rep(k,i,j-1){
LL s=qsum(A[k].a,A[k].b,A[k].c);
if(s>=A[k].w)continue;
LL dt=A[k].w-s;
work(A[k].a,A[k].c,dt);
work(A[k].b,A[k].c,dt);
ans+=dt;
if(!dt)continue;
if(A[k].a==A[k].c){
int b_=jump(A[k].b,dep[A[k].b]-dep[A[k].c]-1);
push(A[k].c,1<<id[b_],dt);
}else{
int a_=jump(A[k].a,dep[A[k].a]-dep[A[k].c]-1);
int b_=jump(A[k].b,dep[A[k].b]-dep[A[k].c]-1);
push(A[k].c,(1<<id[a_])|(1<<id[b_]),-dt);
push(A[k].c,(1<<id[a_]),dt);
push(A[k].c,(1<<id[b_]),dt);
}
}
}
/*rep(i,2,n)D("i=%d : %lld\n",i,fwk.query(tin[i])-fwk.query(tin[fa[i][0]]));
D("\n");*/
printf("%lld\n",ans);
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 55032kb
input:
5 7 3 1 2 1 3 2 4 2 5 3 2 8 5 4 10 3 1 2 1 2 7 2 1 2 1 2 1 5 2 3
output:
19
result:
ok 1 number(s): "19"
Test #2:
score: -100
Wrong Answer
time: 450ms
memory: 207952kb
input:
100000 500000 12 2 1 3 2 4 2 5 2 6 5 7 2 8 5 9 3 10 2 11 2 12 5 13 1 14 1 15 1 16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 12 25 2 26 2 27 2 28 2 29 2 30 15 31 30 32 23 33 26 34 22 35 30 36 26 37 3 38 3 39 3 40 3 41 3 42 3 43 3 44 3 45 3 46 3 47 20 48 21 49 4 50 4 51 4 52 4 53 4 54 4 55 4 56 4 57 4 5...
output:
691370702862
result:
wrong answer 1st numbers differ - expected: '660925834533', found: '691370702862'