1. 程式人生 > >CCF-2015-9-13-04

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;
}