传送门:HDU - 6268
思路分析
考虑点分治,计算包含根节点的所有子图存在的权值和,可以用bitset维护,儿子的状态可以由父亲的状态加上儿子的权值进行转移,加上权值的话就相当于s<<w
AC代码
#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define fun function
#define sz(x) (x).size()
#define lowbit(x) (x)&(-x)
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
namespace FastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
bool IOerror=0;
inline char nc() {
static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
if(p1==pend) {
p1=buf;
pend=buf+fread(buf,1,BUF_SIZE,stdin);
if(pend==p1) {
IOerror=1;
return -1;
}
}
return *p1++;
}
inline bool blank(char ch) {
return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
}
template<class T> inline bool read(T &x) {
bool sign=0;
char ch=nc();
x=0;
for(; blank(ch); ch=nc());
if(IOerror)return false;
if(ch=='-')sign=1,ch=nc();
for(; ch>='0'&&ch<='9'; ch=nc())x=x*10+ch-'0';
if(sign)x=-x;
return true;
}
template<class T,class... U>bool read(T& h,U&... t) {
return read(h)&&read(t...);
}
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace std;
using namespace FastIO;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int INF = 0x3f3f3f3f;
const int M = 1e5+10;
const int N = 3030;
vector<int>e[N];
bitset<M>s[N],ans;
int n,m,siz[N],root,maxp[N],w[N],tot,vis[N];
void getroot(int u,int fa) {
siz[u]=1;
maxp[u]=0;
for(auto v:e[u]) {
if(v==fa || vis[v]) continue;
getroot(v,u);
siz[u]+=siz[v];
maxp[u]=max(maxp[u],siz[v]);
}
maxp[u]=max(tot-siz[u],maxp[u]);
if(maxp[u]<maxp[root]) root=u;
}
void calc(int u,int fa) {
for(auto v:e[u]) {
if(v==fa || vis[v]) continue;
s[v]=(s[u]<<w[v]);
calc(v,u);
s[u]|=s[v];
}
}
void div(int u) {
vis[u]=1;
s[u].reset();
s[u][w[u]]=1;
calc(u,u);
ans|=s[u];
int all = tot;
for(auto v:e[u]) {
if(vis[v]) continue;
tot=siz[v]>siz[u]? all - siz[u]:siz[v];
root=0;
maxp[root] = tot;
getroot(v,v);
div(root);
}
}
int main() {
#ifdef xiaofan
freopen("1.in","r",stdin);
freopen("1.out","w",stdout);
#endif
int T;
cin>>T;
while(T--) {
cin>>n>>m;
tot=n;
ans.reset();
for(int i=1; i<=n; i++) {
e[i].clear();
maxp[i]=0;
vis[i]=0;
}
for(int i=1; i<n; i++) {
int u,v;
cin>>u>>v;
e[u].pb(v);
e[v].pb(u);
}
for(int i=1; i<=n; i++) cin>>w[i];
root=0;
maxp[root]=tot;
getroot(1,1);
div(root);
for(int i=1;i<=m;i++) cout<<ans[i];
cout<<endl;
}
return 0;
}