首页 > 其他分享 >Not so Mobile UVA - 839

Not so Mobile UVA - 839

时间:2022-08-19 19:24:02浏览次数:77  
标签:wl Mobile 天平 839 tr int wr ans UVA

题目链接

题意

有若干个天平,每个天平是否满足力矩原则,即 \(wl\times dl = wr\times dr\) 。

思路

读入有些麻烦,还得递归,注意细节问题。
我们建树,把每个天平抽象成节点,为了好算我们把一个点的左边或右边是一个子天平,那么这个天平(不是子天平!)的 \(wl\) 是负数,并且子天平在数组里的编号为 \(-wl\) ,右边同理。
最后在判断是否合法时直接深度优先遍历一下就好了,这里我用的返回值数天平的总重量,方便判断,答案是否合法就开一个全局变量即可。
注意为空行细节!!!(我就被坑了)

代码

#include <iostream>
#include <cstdio>
using namespace std;
const int N = 100010;
int h,x;
struct mobile {
	int dl,dr,wl,wr;
}tr[N];
int idx = 0;
bool ans;
int read () {
	int w1,d1,w2,d2;
	cin >> w1 >> d1 >> w2 >> d2;
	tr[idx] = {d1,d2,w1,w2};
	int t = idx++;
	if (w1 == 0) tr[t].wl = -read ();
	if (w2 == 0) tr[t].wr = -read ();
	return t;
}
int dfs (int x) {
	if (!ans) return 0;
	int wl = tr[x].wl,wr = tr[x].wr;
	if (tr[x].wl < 0) {
		wl = dfs (-tr[x].wl);
		if (!ans) return 0;
	}
	if (tr[x].wr < 0) {
		wr = dfs (-tr[x].wr);
		if (!ans) return 0;
	}
	if (wl * tr[x].dl != wr * tr[x].dr) {
		ans = false;
		return 0;
	}
	return wl + wr;
}
int main () {
	int T;
	cin >> T;
	while (T--) {
		ans = true,idx = 0;
		read ();
		dfs (0);
		if (ans) puts ("YES");
		else puts ("NO");
		if (T) cout << endl;
	}
    return 0;
}

标签:wl,Mobile,天平,839,tr,int,wr,ans,UVA
From: https://www.cnblogs.com/incra/p/16603103.html

相关文章

  • UVA1608 不无聊的序列 Non-boring sequences
    https://www.luogu.com.cn/problem/UVA1608如果一个序列的任意连续子序列都至少有一个元素唯一,则称这个序列“不无聊”,否则称这个序列“无聊”。给定T个序列,求是否“无聊......
  • 《GB28395-2012》PDF下载
    《GB28395-2012混凝土及灰浆输送、喷射、浇注机械安全要求》PDF下载《GB28395-2012》简介本标准规定了混凝土及灰浆输送、喷射、浇注机械的安全要求和用以消除或减少......
  • Calling Circles UVA - 247
    原题链接思路把最短路换成是否可达即可代码#include<iostream>#include<cstdio>#include<cstring>#include<unordered_map>#include<vector>usingnamespace......
  • UVA10089 Repackaging
    设第\(i\)种包装中\(j\)尺寸的杯子数量为\(S_{ij}\),第\(i\)种包装选取的数量为\(a_i\),若能够按照要求进行重新打包,有\[\begin{cases}a_1S_{11}+a_2S_{21}......