Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

判断连通的时猴,嫑㞎没有開着的門的點祘进去

Posted by KatrineYang at 2016-09-12 03:34:35 on Problem 1300
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;

vector<int> adjList[25];

void dfs(int m, bool *used){
	used[m] = 1;
	int sz = adjList[m].size();
	for(int i = 0; i < sz; i++){
		if(used[adjList[m][i]]) continue;
		dfs(adjList[m][i], used);
	}
}

int main() {
	while(1){
		string s;
		getline(cin, s);
		if(s == "ENDOFINPUT") break;
		stringstream ss;
		ss << s;
		string fei; int M, N;
		ss >> fei >> M >> N;
		//cout << M << " " << N << endl;
		int cnt = 0;
		for(int i = 0; i < N; i++) adjList[i].clear();
		bool ymkz[25] = {0};
		for(int i = 0; i < N; i++){
			getline(cin, s);
			//cout << s << endl;
			stringstream sss;
			sss << s;
			int temp;
			while(sss >> temp){
				cnt++;
				adjList[i].push_back(temp);
				adjList[temp].push_back(i);
				ymkz[i] = 1;
				ymkz[temp] = 1;
			}
		}
		getline(cin, fei);
		bool used[25] = {0};
		dfs(M, used);
		bool ky = 1;
		for(int i = 0; i < N; i++){
			if(ymkz[i] && !used[i]){
				ky = 0;
				break;
			}
		}
		if(!ky){
			cout << "NO" << endl;
			continue;
		}
		if(M == 0){
			for(int i = 0; i < N; i++){
				if(adjList[i].size()%2 != 0){
					ky = 0;
					break;
				}
			}
		}
		else{
			if(adjList[0].size()%2==0 || adjList[M].size()%2==0){
				ky = 0;
			}
			else{
				for(int i = 1; i < N; i++){
					if(i == M) continue;
					if(adjList[i].size()%2){
						ky = 0;
						break;
					}
				}
			}
		}
		if(ky){
			cout << "YES " << cnt << endl;
		}
		else cout << "NO" << endl;
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator