UVALive 6953 Digi Comp II

本文介绍了一种名为 DigiComp II 的机械装置,该装置通过一系列开关引导小球从顶部到底部的路径,并能实现加法、乘法等算法。文章详细解析了一个相关竞赛题目,提供了解题思路及 C++ 实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The Digi Comp II is a machine where balls enter from the top and find their way to the bottom via a cer- tain circuit defined by switches. Whenever a ball falls on a switch it either goes to the left or to the right depending on the state of the switch and flips this state in the process. Abstractly it can be modelled by a directed graph with a vertex of outdegree 2 for each switch and in addition a designated end vertex of outdegree 0. One of the switch vertices is the start vertex, it has indegree 0. Each switch vertex has an internal state (L/R). A ball starts at the start vertex and follows a path down to the end vertex, where at each switch vertex it will pick the left or right out-going edge based on the internal state of the switch vertex. The internal state of a vertex is flipped after a ball passes through. A ball always goes down and therefore cannot get into a loop.
One can “program” this machine by specifying the graph structure, the initial states of each switch vertex and the number of balls that enter. The result of the computation is the state of the switches at the end of the computation. Interestingly one can program quite sophisticated algorithms such as addition, multiplication, division and even the stable marriage problem. However, it is not Turing complete.

Input
The input file contains several test cases, each of them as described below.
The input consists of:
• one line with two integers
n(0<=n<=1018) andm(1<=m<=500000), the number of balls and the number of switches of the graph;
• m lines describing switches 1 to m in order. Each line consists of a single character c('L' or 'R') and two integers L and R (0<=L,R<=m),describing the initial state (c) of the switch and the destination vertex of the left (L) and right (R) outgoing edges.L and R can be equal.
Vertex number 0 is the end vertex and vertex 1 is the start vertex. There are no loops in the graph, i.e., after going through a switch a ball can never return to it.

Output
For each test case, output one line with a string of length m consisting of the characters 'L' and 'R',describing the final state of the switches (1 to m in order).
Sample Input
5 3
L 2 3
R 0 3
L 0 0

Sample Output

RLL


题目大意: 球从1号结点开始下落,每次根据节点的状态是L还是R而落到它的左节点或者右节点,每次经过一个节点之后会改变结点的状态(L变R ,R变L),问下落N个球之后所有节点的状态。


题解:坑了差不多一天。。。

           对于每个结点,递归计算出所有会经过它的父亲节点的球的个数,对于经过它父亲节点的球,有一半会掉向它,所以可以根据状态来判断是+num/2还是+num/2+1。

           但是,我用递归写的,节点数量50万,爆了。于是改成stack写的DFS。

           再还有需要注意的是,一个节点的左右子节点可能是同一个节点,要特别注意(就是这里坑了我一天!!)



#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
long long n;
long long dp[500005];
bool f;
vector<int>v[500005];
stack<int>s;
struct point
{
	char c;
	int l;
	int r;
}p[500005];
void dfs()
{
	while (!s.empty())s.pop();
	s.push(0);
	long long tem;
	dp[1]=n;
	int num;
	while (!s.empty())
	{
		num=s.top();
		s.pop();
		if (dp[num]!=-1) continue;
		f=false;
		for (int i=0;i<v[num].size();i++)
		{
			if (dp[v[num][i]]==-1)
			{
				if (!f) s.push(num);
				s.push(v[num][i]);
				f=true;
			}
		}
		if (!f)
		{
			dp[num]=0;
			for (int i=0;i<v[num].size();i++)
			{
				if (i&&v[num][i]==v[num][i-1]) continue;
				tem=dp[v[num][i]];
				if (p[v[num][i]].l==num&&p[v[num][i]].r==num) dp[num]+=tem;
				else if (tem%2==0) dp[num]+=tem/2;
				else
				{
					if (p[v[num][i]].c=='L'&&p[v[num][i]].l==num) dp[num]+=tem/2+1; 
					if (p[v[num][i]].c=='R'&&p[v[num][i]].r==num) dp[num]+=tem/2+1;
					if (p[v[num][i]].c=='L'&&p[v[num][i]].r==num) dp[num]+=tem/2;
					if (p[v[num][i]].c=='R'&&p[v[num][i]].l==num) dp[num]+=tem/2;
				}
			}
		}
	}
	return ;
}
int main()
{
	int m;
	while (cin>>n>>m)
	{
		for (int i=0;i<=m;i++) v[i].clear();
		memset(dp,-1,sizeof(dp));
		for (int i=1;i<=m;i++)
		{
			scanf(" %c%d%d",&p[i].c,&p[i].l,&p[i].r);
			v[p[i].l].push_back(i);
			v[p[i].r].push_back(i);
		}
		for (int i=0;i<=m;i++) sort(v[i].begin(),v[i].end());
		dfs();
		for (int i=1;i<=m;i++)
		{
			if (dp[i]%2==0) printf("%c",p[i].c);
			else 
			{
				if (p[i].c=='L') printf("R");
				else printf("L");
			}
		}
		printf("\n");
		
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值