跳到正文

题解

Editorial:P13118 [GCJ 2019 #2] Contransmutation

liangbob约 9 分钟
原文

这是 2025年11月24日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2025年11月24日
收录
2025年12月12日
更新
2026年11月7日
篇幅
约 9 分钟
获赞
8
题目
P13118

版本

  1. v2当前
  2. v1

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 f(x)f(x) denote the maximum value of the total weight of metals that you can get which belong to SCC xx.

If the total weight of metals which belong to SCC uu is bigger than 00, f(v)←−1f(v)\gets -1. There, vv is the immediate successor SCCs of SCC uu 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 vv; since we can traverse the loop indefinitely, f(v)f(v) becomes infinite. In particular, if SCC uu is just a single node, f(v)←f(v)+f(u)f(v) \gets f(v)+f(u), where vv is as defined above.

But what is the value of f(u)f(u) if the total weight of metals which belong to SCC uu is bigger than 00? A classified discussion is required based on the count of vertices szsz and edges bsbs in the SCC:

  • If sz=bssz=bs, which means the SCC is a cycle, apparently f(u)f(u) is equal to the total weight of metals which belong to SCC uu.
  • If sz<bssz<bs, 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.

szsz and bsbs are easy to calculate,the value ff of uu is also easy to calculate when the total weight of metals which belong to SCC uu is 00, so this problem is solved perfectly.

Note that we cannot rely solely on whether f(u)>0f(u) > 0 to determine if the total weight in this SCC is positive, because f(u)f(u) is the result after a modulo operation. It is possible that the actual total weight is a multiple of the modulus, making f(u)f(u) zero.

We need to maintain a boolean value ps(u)ps(u) indicating whether the total weight in SCC uu is positive. This value is updated along with u as we process the topological order, and the update logic is straightforward.

C++
#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;
}