信息学奥赛一本通 3.3:树

1336 找树根和孩子
#include <cstdio>
#include <iostream>
using namespace std;

int n, m;
int f[1005], d[1005];

int main() {
   
	cin >> n >> m;
	
	int x, y;
	while (m--)	 {
   
		cin >> x >> y;
		f[y] = x;
		d[x] ++ ;
	}
	
	int maxx = 0;
	for (int i = 0; i <= 1000; i++ ) {
   
		if (f[i]==0 && d[i]>0)  cout << i << endl;
		if (d[i] > d[maxx]) maxx = i;
	}
	cout << maxx << endl;
	
	for (int i = 0; i <= 1000; i ++ ) {
   
		if (f[i] == maxx) cout << i << ' ';
	}

	return 0;
}
1368 对称二叉树(tree_c)
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
   
	string s;
	cin >> s;
	
	if (s.size()%2==0) {
   
		cout << "No";
		return 0;
	}
	
	for (int i = 1; i < s.size(); i = i + 2 ) {
   
		if(s[i]=='#' && s[i+1]!='#') {
   
			cout << "No";
			return 0;
		}
		if(s[i]!='#' && s[i+1]=='#') {
   
			cout << "No";
			return 0;
		}			
	}
	
	cout << "Yes";
	
	return 0;
}
1363 小球(drop)
#include <iostream>
using namespace std;

const int N = 1e7;
int a[N], d, x;

int main(){
   
	cin >> d >> x;
	
	int t;
	for (int i = 1; i <= x; i ++ ) {
   
		t = 1;
		for (int j = 1; j < d; j ++) {
   
			if (!a[t]) {
   
				a[t] = !a[t];
				t = 2*t;
			}
			else {
   
				a[t] = !a[t];
				t = 2*t + 1;
			}			
		}
	}
	cout << t;

	return 0;
}
1367 查找二叉树(tree_a)
#include <cstdio>
#include <iostream>
using namespace std;

int n, x, cnt;

struct node{
   
	int val, l, r;
}a[105];

void f(int n) {
   
	if (n == 0) return;
	
	f(a[n].l);
	
	cnt ++ ;
	
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值