This is an English editorial.
This article was handwritten with the help of a dictionary. Please forgive any grammar errors.
Upd on 2025.11.24 21:20: Fix some grammar erros and make the expression more formal and rigorous. There are too many low-level errors:(
We observe 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 often intractable, so we consider condensing the original graph into SCCs and performing DP on the resulting DAG in topological order.
Specifically, let denote the maximum attainable total weight of metals within SCC .
If the total weight of metals in SCC is positive, we set . Here, refers to any immediate successor of SCC in the condensation DAG.
This is because an SCC, by definition, allows any node to reach any other, meaning we can traverse a cycle that includes all nodes. Each time we traverse the cycle, we add the sum of the weights of all nodes on it to ; since we can traverse the cycle indefinitely, becomes infinite. In particular, if SCC is a single node, we update , where is defined as above. Note that if , we must also set , as the infinite value propagates to .
But what is the value of if the total weight of metals in SCC is positive? We analyze different cases based on the count of vertices and edges in the SCC:
- Case 1: . This indicates the SCC is a simple cycle. In this case, is equal to the total weight of metals in SCC .
- Case 2: . This means the SCC contains at least one simple cycle and at least one edge that is not part of that cycle. Each time we traverse a cycle, the total weight of the cycle is immediately added via a non-cycle edge to an adjacent node. Since this process can be repeated indefinitely, the value of the adjacent node becomes infinite. This infinite value can then propagate throughout the SCC via other edges, so the value for the entire SCC becomes infinite. Thus, we set .
The parameters and are straightforward to calculate. Moreover, the value of is also easy to calculate when the total weight of metals which belong to SCC is .
Combining these cases, the problem can be efficiently solved.
Note that we cannot rely solely on whether to determine if the total weight in this SCC is positive, because is the result after the 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 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;
}