#include<bits/stdc++.h>
using namespace std;
using LL=long long;
using Pii=pair<int,int>;
const int N=1e5+5;
int n,m,c,d[N],f[N],h[N];
bool p[N];
LL a[N],r;
vector<int>e[N];
vector<Pii>g;
int R(int x){
return f[x]==x?x:f[x]=R(f[x]);
}
void U(int x,int y){
x=R(x),y=R(y);
x!=y&&(f[x]=y);
}
void D(int u,int fa){
h[u]=h[fa]+1;
for(auto v:e[u]){
D(v,u);
}
}
void S(int u){
d[u]=++c;
for(auto v:e[u]){
p[v]&&(S(v),1);
}
}
int main(){
freopen("block.in","r",stdin);
freopen("block.out","w",stdout);
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=1,l,x;i<=n;i++){
for(cin>>l;l;l--){
cin>>x;
e[i].push_back(x);
}
}
D(1,0);
for(int u,v;m;m--){
cin>>u>>v;
g.push_back({u,v});
}
for(int i=0;i<(1<<n);i++){
int m=0,u[25];
fill(p+1,p+n+1,0);
for(int j=1;j<=n;j++){
f[j]=j;
(i>>j-1)&1&&(u[++m]=j,p[j]=1);
}
for(int j=1;j<=m;j++){
for(auto x:e[u[j]]){
p[x]&&(U(u[j],x),1);
}
}
bool f=1;
LL s=0,l=1e9,z;
for(int j=1;j<=m&&f;j++){
f&=R(u[1])==R(u[j]);
s+=a[u[j]];
if(h[u[j]]<l){
l=h[u[j]],z=u[j];
}
}
if(!f){
continue;
}
c=0,S(z);
for(auto v:g){
int x=v.first,y=v.second;
if(p[x]&&p[y]&&abs(d[x]-d[y])==1){
f=0;
break;
}
}
f&&(r=max(r,s));
}
cout<<r;
return 0;
}剪贴板
剪贴板 5yv7vsjd
剪贴板信息
剪贴板信息
- 收录
- 更新