跳到正文

题解

Editorial:P13118 [GCJ 2019 #2] Contransmutation

liangbob约 10 分钟
原文
文章信息与版本

文章信息

发布
2025年11月24日
收录
2025年12月12日
更新
2026年11月7日
篇幅
约 10 分钟
获赞
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.

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 f(x)f(x) denote the maximum attainable total weight of metals within SCC xx.

If the total weight of metals in SCC uu is positive, we set f(v)←−1f(v)\gets -1. Here, vv refers to any immediate successor of SCC uu 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 vv; since we can traverse the cycle indefinitely, f(v)f(v) becomes infinite. In particular, if SCC uu is a single node, we update f(v)←f(v)+f(u)f(v) \gets f(v) + f(u), where vv is defined as above. Note that if f(u)=−1f(u) = -1, we must also set f(v)=−1f(v) = -1, as the infinite value propagates to vv.

But what is the value of f(u)f(u) if the total weight of metals in SCC uu is positive? We analyze different cases based on the count of vertices szsz and edges bsbs in the SCC:

  • Case 1: sz=bssz=bs. This indicates the SCC is a simple cycle. In this case, f(u)f(u) is equal to the total weight of metals in SCC uu.
  • Case 2: sz<bssz<bs. 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 f(u)←−1f(u) \gets -1.

The parameters szsz and bsbs are straightforward to calculate. Moreover, the value ff of uu is also easy to calculate when the total weight of metals which belong to SCC uu is 00.

Combining these cases, the problem can be efficiently solved.

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 the 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 uu 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;
}
原文
存档评论5 条抓取于