CCF-2015-9-13-04

下题为个人做法,只做参考。
二维数组第三个为状态,表示是否已经访问过该条路径。

/*
国王给城市修路,两个城市互相有路则为便利城市,路则为便利路,求便利路的条数。
第一行输入两个数字m,n ;m 为城市数量,n为路的总数;
接下来的n行每行输入两个数i,j : 表示i->j为一条通路
最后为一行输出,表示便利路的条数。
*/


#include "iostream"
using namespace std;

bool myPorcess(int **p, int start, int end, int size, int deep);
int main() {

	int nCity = 0;
	int nRoad = 0;
	
	cin >> nCity;
	cin >> nRoad;

	int  **pRoad = new int*[nRoad]();

	for (int i = 0; i < nRoad; ++i)
		pRoad[i] = new int[3]();
	for (int i = 0; i < nRoad; ++i){
		cin >> pRoad[i][0];
		cin >> pRoad[i][1];
		pRoad[i][2] = 0;
	}

	int count = 0;
	for (int i = 0; i < nRoad; ++i) {
		pRoad[i][2] = 1;
		if (myPorcess(pRoad, pRoad[i][1], pRoad[i][0], nRoad, 0))
			count++;
		for (int j = 0; j < nRoad; ++j)
			pRoad[j][2] = 0;
	}

	cout << count << endl;

	return 0;

}


bool myPorcess(int **p,int start,int end,int size,int deep) {

	if (deep != 0)
		if (start == end)return true;
	for (int i = 0; i < size; ++i)
		if (p[i][0] == start)
			if (p[i][2] == 1)
				continue;
			else {
				p[i][2] = 1;
				return myPorcess(p, p[i][1], end, size, 1);
			}
	return false;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值