This is an English editorial.
This article was handwritten with the help of a dictionary. Please forgive any grammar errors.
We find that this problem requires calculating values derived recursively from other nodes, which is precisely what DP is designed to solve.
DP on general graphs is extremely challenging, so we consider condensing the original graph into SCC and performing DP on the resulting DAG in topological order.
Specifically, let denote the maximum value of the total weight of metals that you can get which belong to SCC .
If the total weight of metals which belong to SCC is bigger than , . There, is the immediate successor SCCs of SCC in the condensation DAG. This is because we can see an SCC as a cycle. Each time we traverse the loop, we add the sum of the weights of all nodes on it to ; since we can traverse the loop indefinitely, becomes infinite. In particular, if SCC is just a single node, , where is as defined above.
But what is the value of if the total weight of metals which belong to SCC is bigger than ? A classified discussion is required based on the count of vertices and edges in the SCC:
- If , which means the SCC is a cycle, apparently is equal to the total weight of metals which belong to SCC .
- If , each time we traverse any cycle within the SCC, the sum of the weights from the nodes in that cycle is added to the weight of any node reachable via a non-cycle edge. Since this traversal can be repeated indefinitely, the infinite value propagates to the entire SCC.
and are easy to calculate,the value of is also easy to calculate when the total weight of metals which belong to SCC is , so this problem is solved perfectly.
Note that we cannot rely solely on whether to determine if the total weight in this SCC is positive, because is the result after a modulo operation. It is possible that the actual total weight is a multiple of the modulus, making zero.
We need to maintain a boolean value indicating whether the total weight in SCC is positive. This value is updated along with u as we process the topological order, and the update logic is straightforward.
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define IL inline
using namespace std;
const int N=1e6+10,mod=1e9+7;
const int INF=0x3f3f3f3f;
IL int read()
{
int x=0,f=1;
char c=getchar();
while(c<'0'||c>'9'){if(c=='-') f=-1;c=getchar();}
while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+(c-'0'),c=getchar();
return x*f;
}
void write(int x)
{
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10+'0');
}
int n,a[N],c[N],f[N],eu[N],ev[N],in[N],sz[N],bs[N],ps[N];
int cnt,tot,top,dfn[N],low[N],stk[N],scc[N];
int cxt,h[N],nxt[N<<1],to[N<<1];
void clear()
{
cnt=tot=top=cxt=0;
for(int i=1;i<=n;i++) eu[i]=ev[i]=a[i]=c[i]=dfn[i]=low[i]=scc[i]=f[i]=in[i]=sz[i]=bs[i]=ps[i]=h[i]=0;
}
void save(int u,int v)
{
to[++cxt]=v;
nxt[cxt]=h[u];
h[u]=cxt;
}
void tarjan(int u)
{
dfn[u]=low[u]=++tot;
stk[++top]=u;
for(int i=h[u];i;i=nxt[i])
{
int v=to[i];
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(!scc[v]) low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u])
{
(c[scc[u]=++cnt]+=a[u])%=mod;
if(a[u]) ps[cnt]=1;
while(stk[top]!=u) top--,(c[scc[stk[top+1]]=cnt]+=a[stk[top+1]])%=mod,ps[cnt]|=bool(a[stk[top+1]]);
top--;
}
}
queue<int>q;
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T;cin>>T;
for(int id=1;id<=T;id++)
{
cin>>n;
clear();
for(int i=1;i<=n;i++) {cin>>eu[i]>>ev[i];save(i,eu[i]);save(i,ev[i]);}
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=1;i<=n;i++) if(!dfn[i]) tarjan(i);
for(int i=1;i<=cnt;i++) f[i]=c[i];
cxt=0;for(int i=1;i<=n;i++) sz[scc[i]]++,h[i]=0;
for(int i=1;i<=n;i++)
{
if(scc[i]!=scc[eu[i]]) save(scc[i],scc[eu[i]]),in[scc[eu[i]]]++;
else bs[scc[i]]++;
if(scc[i]!=scc[ev[i]]) save(scc[i],scc[ev[i]]),in[scc[ev[i]]]++;
else bs[scc[i]]++;
}
for(int i=1;i<=cnt;i++) if(!in[i]) q.push(i);
while(q.size())
{
int u=q.front();
q.pop();
if(bs[u]>sz[u]&&ps[u]) f[u]=-1;
for(int i=h[u];i;i=nxt[i])
{
int v=to[i];
if(ps[u]) ps[v]=1;
f[v]=(f[u]==-1||f[v]==-1||(bs[u]==sz[u]&&ps[u])?-1:(f[u]+f[v])%mod);
in[v]--;
if(!in[v]) q.push(v);
}
}
cout<<"Case #"<<id<<": "<<(f[scc[1]]!=-1?to_string(f[scc[1]]):"UNBOUNDED")<<endl;
}
return 0;
}