zhoukangyang @ 2020-03-04 22:14:24
桶
#include<bits/stdc++.h>
using namespace std;
int kn,N,n,k[23333],u,v,q,siz[41111],rot,mas,fa[41111],mi[41111],paint[41111],deep[41111],color_id;
int head[41111],last[41111],ans[23333],stackk[41111],dfn;
struct node {
int to,next,val;
} s[233333];
void find_root(int x) {
siz[x]=1,mi[x]=0;
for(int i = head[x]; i != 0; i = s[i].next) {
if(s[i].to==fa[x]) continue;
if(paint[s[i].to]!=paint[x]) continue;
fa[s[i].to]=x;
find_root(s[i].to);
mi[x]=max(mi[x],siz[s[i].to]),siz[x]+=siz[s[i].to];
}
mi[x]=max(mi[x],N-siz[x]);
if(mas>mi[x]) mas=mi[x],rot=x;
}
void cz(int x) {
siz[x]=1;
for(int i = head[x]; i != 0; i = s[i].next) {
if(s[i].to==fa[x]) continue;
if(paint[s[i].to]!=paint[x]) continue;
fa[s[i].to]=x,cz(s[i].to),siz[x]+=siz[s[i].to];
}
}
void low(int x) {
for(int i = head[x]; i != 0; i = s[i].next) {
if(s[i].to==fa[x]) continue;
if(paint[s[i].to]!=paint[x]) continue;
deep[s[i].to]=deep[x]+s[i].val,fa[s[i].to]=x,low(s[i].to),siz[x]+=siz[s[i].to];
}
++dfn,stackk[dfn]=x,paint[x]=color_id;
}
void dfs(int x) {
int ma[11111111];
if(N==1) return;
int yousb[41111],sz[41111],lowbit=0;
fa[x]=rot=0,mas=1e9,find_root(x);
fa[rot]=0,cz(rot),deep[rot]=0;
ma[0]=1;
for(int i = head[rot]; i != 0; i = s[i].next) {
if(paint[s[i].to]!=paint[rot]) continue;
++lowbit,yousb[lowbit]=s[i].to,sz[lowbit]=siz[s[i].to];
dfn=0,++color_id,deep[s[i].to]=s[i].val,low(s[i].to);
for(int j = 1; j <= dfn; j++) for(int kk = 1; kk <= kn; kk++)
if(k[kk]-deep[stackk[j]]>=0) ans[kk]+=ma[k[kk]-deep[stackk[j]]];
for(int j = 1; j <= dfn; j++) if(deep[stackk[j]]<=1e7) ma[deep[stackk[j]]]++;
}
for(int i = 1; i <= lowbit; i++) N=sz[i],dfs(yousb[i]);
}
void add_edge(int id,int U,int V,int VAL) {
if(head[U]==0) head[U]=id;
else s[last[U]].next=id;
last[U]=id,s[id].to=V,s[id].val=VAL;
}
int main() {
scanf("%d%d",&n,&kn);
for(int i = 1; i < n; i++) {
scanf("%d%d%d",&u,&v,&q);
add_edge(i*2-1,u,v,q);
add_edge(i*2,v,u,q);
}
for(int i = 1; i <= kn; i++) scanf("%d",&k[i]);
N=n;
dfs(1);
for(int i = 1; i <= kn; i++) printf("%s\n",ans[i]!=0?"AYE":"NAY");
return 0;
}
map
#include<bits/stdc++.h>
using namespace std;
int kn,N,n,k[23333],u,v,q,siz[41111],rot,mas,fa[41111],mi[41111],paint[41111],deep[41111],color_id;
int head[41111],last[41111],ans[23333],stackk[41111],dfn;
struct node{
int to,next,val;
} s[233333];
void find_root(int x){
siz[x]=1,mi[x]=0;
for(int i = head[x]; i != 0; i = s[i].next){
if(s[i].to==fa[x]) continue;
if(paint[s[i].to]!=paint[x]) continue;
fa[s[i].to]=x;
find_root(s[i].to);
mi[x]=max(mi[x],siz[s[i].to]),siz[x]+=siz[s[i].to];
}
mi[x]=max(mi[x],N-siz[x]);
if(mas>mi[x]) mas=mi[x],rot=x;
}
void cz(int x){
siz[x]=1;
for(int i = head[x]; i != 0; i = s[i].next){
if(s[i].to==fa[x]) continue;
if(paint[s[i].to]!=paint[x]) continue;
fa[s[i].to]=x,cz(s[i].to),siz[x]+=siz[s[i].to];
}
}
void low(int x){
for(int i = head[x]; i != 0; i = s[i].next){
if(s[i].to==fa[x]) continue;
if(paint[s[i].to]!=paint[x]) continue;
deep[s[i].to]=deep[x]+s[i].val,fa[s[i].to]=x,low(s[i].to),siz[x]+=siz[s[i].to];
}
++dfn,stackk[dfn]=x,paint[x]=color_id;
}
void dfs(int x){
map<int,int> ma;
if(N==1) return;
int yousb[41111],sz[41111],lowbit=0;
fa[x]=rot=0,mas=1e9,find_root(x);
fa[rot]=0,cz(rot),deep[rot]=0;
ma[0]=1;
for(int i = head[rot]; i != 0; i = s[i].next) {
if(paint[s[i].to]!=paint[rot]) continue;
++lowbit,yousb[lowbit]=s[i].to,sz[lowbit]=siz[s[i].to];
dfn=0,++color_id,deep[s[i].to]=s[i].val,low(s[i].to);
for(int j = 1; j <= dfn; j++) for(int kk = 1; kk <= kn; kk++) ans[kk]+=ma[k[kk]-deep[stackk[j]]];
for(int j = 1; j <= dfn; j++) ma[deep[stackk[j]]]++;
}
for(int i = 1; i <= lowbit; i++) N=sz[i],dfs(yousb[i]);
}
void add_edge(int id,int U,int V,int VAL){
if(head[U]==0) head[U]=id;
else s[last[U]].next=id;
last[U]=id,s[id].to=V,s[id].val=VAL;
}
int main(){
scanf("%d%d",&n,&kn);
for(int i = 1; i < n; i++){
scanf("%d%d%d",&u,&v,&q);
add_edge(i*2-1,u,v,q);
add_edge(i*2,v,u,q);
}
for(int i = 1; i <= kn; i++) scanf("%d",&k[i]);
N=n;
dfs(1);
for(int i = 1; i <= kn; i++) printf("%s\n",ans[i]!=0?"AYE":"NAY");
return 0;
}
by zhoukangyang @ 2020-03-04 22:31:14
换成了bool,wa4个点
#include<bits/stdc++.h>
using namespace std;
int kn,N,n,k[23333],u,v,q,siz[41111],rot,mas,fa[41111],mi[41111],paint[41111],deep[41111],color_id;
int head[41111],last[41111],ans[23333],stackk[41111],dfn;
struct node {
int to,next,val;
} s[233333];
void find_root(int x) {
siz[x]=1,mi[x]=0;
for(int i = head[x]; i != 0; i = s[i].next) {
if(s[i].to==fa[x]) continue;
if(paint[s[i].to]!=paint[x]) continue;
fa[s[i].to]=x;
find_root(s[i].to);
mi[x]=max(mi[x],siz[s[i].to]),siz[x]+=siz[s[i].to];
}
mi[x]=max(mi[x],N-siz[x]);
if(mas>mi[x]) mas=mi[x],rot=x;
}
void cz(int x) {
siz[x]=1;
for(int i = head[x]; i != 0; i = s[i].next) {
if(s[i].to==fa[x]) continue;
if(paint[s[i].to]!=paint[x]) continue;
fa[s[i].to]=x,cz(s[i].to),siz[x]+=siz[s[i].to];
}
}
void low(int x) {
for(int i = head[x]; i != 0; i = s[i].next) {
if(s[i].to==fa[x]) continue;
if(paint[s[i].to]!=paint[x]) continue;
deep[s[i].to]=deep[x]+s[i].val,fa[s[i].to]=x,low(s[i].to),siz[x]+=siz[s[i].to];
}
++dfn,stackk[dfn]=x,paint[x]=color_id;
}
void dfs(int x) {
bool ma[10000001];
if(N==1) return;
int yousb[41111],sz[41111],lowbit=0;
fa[x]=rot=0,mas=1e9,find_root(x);
fa[rot]=0,cz(rot),deep[rot]=0;
ma[0]=1;
for(int i = head[rot]; i != 0; i = s[i].next) {
if(paint[s[i].to]!=paint[rot]) continue;
++lowbit,yousb[lowbit]=s[i].to,sz[lowbit]=siz[s[i].to];
dfn=0,++color_id,deep[s[i].to]=s[i].val,low(s[i].to);
for(int j = 1; j <= dfn; j++) for(int kk = 1; kk <= kn; kk++)
if(k[kk]-deep[stackk[j]]>=0) ans[kk]|=ma[k[kk]-deep[stackk[j]]];
for(int j = 1; j <= dfn; j++) if(deep[stackk[j]]<=1e7) ma[deep[stackk[j]]]=1;
}
for(int i = 1; i <= lowbit; i++) N=sz[i],dfs(yousb[i]);
}
void add_edge(int id,int U,int V,int VAL) {
if(head[U]==0) head[U]=id;
else s[last[U]].next=id;
last[U]=id,s[id].to=V,s[id].val=VAL;
}
int main() {
scanf("%d%d",&n,&kn);
for(int i = 1; i < n; i++) {
scanf("%d%d%d",&u,&v,&q);
add_edge(i*2-1,u,v,q);
add_edge(i*2,v,u,q);
}
for(int i = 1; i <= kn; i++) scanf("%d",&k[i]);
N=n;
dfs(1);
for(int i = 1; i <= kn; i++) printf("%s\n",ans[i]!=0?"AYE":"NAY");
return 0;
}
by Lice @ 2020-03-04 22:31:25
qndmx
by 1kri @ 2020-03-04 22:52:08
调用经典图片
by zhoukangyang @ 2020-03-05 07:20:04
不会对拍的我瑟瑟发抖
by zhoukangyang @ 2020-03-05 16:19:55
考谷@devinwang
by devinwang @ 2020-03-06 22:44:50
这个写法好迷啊。。。为什么那么多操作都跑了两次
by zhoukangyang @ 2020-03-06 23:01:53
额
by Scean_Tong @ 2024-11-13 15:47:34
@麻省理工学院 我超,预言家,你怎么知道 zky 5 年后能 AK IOI /jk
by WZWZWZWY @ 2024-11-13 16:05:09
(
by __little__Cabbage__ @ 2024-11-13 16:07:58
@麻省理工学院 预言家求问我明年CSP-J能过吗?