1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
| #include<bits/stdc++.h> #define fo(i,a,b) for(int i=a;i<=b;i++) using namespace std;
typedef long long LL;
const int maxn=505; const LL inf=2139062143;
int n,m,N,c[maxn]; vector<int> e[maxn],em[maxn]; map<pair<int,int>,int> M;
LL lx[maxn],ly[maxn],slack[maxn],mp[maxn][maxn]; int f[maxn],pre[maxn]; bool vis[maxn]; LL KM(int nl,int nr) { fo(i,1,nl) { lx[i]=-inf; fo(j,1,nr) lx[i]=max(lx[i],mp[i][j]); } memset(ly,0,sizeof(LL)*(nr+1)); memset(f,0,sizeof(int)*(nr+1)); memset(pre,0,sizeof(int)*(nr+1)); fo(i,1,nl) { memset(slack,127,sizeof(LL)*(nr+1)); memset(vis,0,sizeof(bool)*(nr+1)); f[0]=i; int py=0, nextpy; for(; f[py]; py=nextpy) { int px=f[py]; LL d=inf<<3; vis[py]=1; fo(j,1,nr) if (!vis[j]) { if (lx[px]+ly[j]-mp[px][j]<slack[j]) slack[j]=lx[px]+ly[j]-mp[px][j], pre[j]=py; if (slack[j]<d) d=slack[j], nextpy=j; } fo(j,0,nr) if (vis[j]) lx[f[j]]-=d, ly[j]+=d; else slack[j]-=d; } for(; py; py=pre[py]) f[py]=f[pre[py]]; } LL re=0; fo(i,1,nl) re+=lx[i]; fo(i,1,nr) re+=ly[i]; return re; }
LL dis[maxn][maxn],aug[maxn]; int ff[maxn]; void floyd(int nl,int nr,bool ty) { fo(y,1,nr) if (f[y]) ff[f[y]]=y; fo(i,1,nl) fo(j,1,nl) dis[i][j]=(i==j) ?0 :mp[j][ff[i]]-mp[i][ff[i]]; fo(k,1,nl) fo(i,1,nl) if (i!=k) fo(j,1,nl) if (j!=i && j!=k) dis[i][j]=max(dis[i][j],dis[i][k]+dis[k][j]); if (ty) { fo(x,1,nl) aug[x]=dis[x][f[nr]]; } else { fo(x,1,nl) { aug[x]=-mp[x][ff[x]]; fo(y,1,nl) aug[x]=max(aug[x],dis[x][y]-mp[y][ff[y]]); } } }
LL dp[maxn][3*maxn],g[maxn]; int s0,s[maxn]; void dfs(int k,int last) { for(int son:e[k]) if (son!=last) dfs(son,k); s0=0; LL gsum=0; for(int son:e[k]) if (son!=last) s[++s0]=son, gsum+=g[son]; g[k]=inf; for(int i=1, cnt=0, sz; i<=N; i++, cnt+=sz) { sz=em[i].size(); if (sz-1>s0) { fo(j,0,sz-1) dp[k][cnt+j]=inf; continue; } fo(x,0,sz-1) { int id=M[make_pair(em[i][x],i)]; fo(y,1,s0) mp[x+1][y]=g[s[y]]-dp[s[y]][id]; } if (sz>s0) { s[++s0]=0; fo(x,1,sz) mp[x][s0]=0; } LL ans=gsum-KM(sz,s0); if (s[s0] || !s0) g[k]=min(g[k],c[i]+ans); floyd(sz,s0,(s[s0]==0 && s0>0)); fo(x,0,sz-1) dp[k][cnt+x]=min(ans-aug[x+1],inf); s0-=(s0>0 && s[s0]==0); } }
int main() { scanf("%d",&n); fo(i,2,n) { int x,y; scanf("%d %d",&x,&y); e[x].push_back(y), e[y].push_back(x); } scanf("%d",&m); fo(i,1,m) { int tn,tc; scanf("%d %d",&tn,&tc); fo(j,2,tn) { int x,y; scanf("%d %d",&x,&y); em[N+x].push_back(N+y), em[N+y].push_back(N+x); } fo(j,1,tn) c[N+j]=tc; N+=tn; } int tot=0; fo(i,1,N) for(int go:em[i]) M[make_pair(i,go)]=tot++; dfs(1,0); if (g[1]>=inf) puts("impossible"); else printf("%lld\n",g[1]); }
|