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

稍微剪了个枝,16ms

Posted by KatrineYang at 2016-10-22 14:42:40 on Problem 1419
注意遞归还原现场的坑
#include <iostream>
#include <stdio.h>
using namespace std;

int adj[101][101];
int adjNo[101];
int gs, mx;
bool used[101];
bool set[101];
bool tempSet[101];

int n, m, e;

bool init(){
	if(!m) return 0;
	m--;
	scanf("%d%d", &n, &e);
	for(int i = 1; i <= n; i++) adjNo[i] = 0;
	for(int i = 1; i <= e; i++){
		int a,b;
		scanf("%d%d", &a, &b);
		if(a < b){
			adj[a][adjNo[a]] = b;
			adjNo[a]++;
		}
		else{
			adj[b][adjNo[b]] = a;
			adjNo[b]++;
		}
	}
	for(int i = 1; i <= n; i++) used[i] = 0;
	for(int i = 1; i <= n; i++) set[i] = 0;
	for(int i = 1; i <= n; i++) tempSet[i] = 0;
	gs = 0;
	mx = 0;
	return 1;
}

void getMax(int start){
	if(start == n){
		if(gs > mx){
			mx = gs;
			for(int i = 1; i <= n; i++) set[i] = tempSet[i];
		}
		return;
	}
	if(gs+n-start <= mx) return;
	tempSet[start+1] = 0;
	getMax(start+1);
	if(!used[start+1]){
		used[start+1] = 1;
		tempSet[start+1] = 1;
		bool alreadyUsed[101] = {0};
		for(int i = 0; i < adjNo[start+1]; i++){
			if(used[adj[start+1][i]]) alreadyUsed[adj[start+1][i]] = 1;
			used[adj[start+1][i]] = 1;
		}
		gs++;
		getMax(start+1);
		gs--;
		for(int i = 0; i < adjNo[start+1]; i++){
			if(!alreadyUsed[adj[start+1][i]]) used[adj[start+1][i]] = 0;
		}
		used[start+1] = 0;
	}
}

int main() {
	scanf("%d", &m);
	while(init()){
		getMax(0);
		printf("%d\n", mx);
		bool first = 1;
		for(int i = 1; i <= n; i++){
			if(set[i]){
				if(!first) {
					printf(" %d", i);
				}
				else{
					first = 0;
					printf("%d", i);
				}
			}
		}
		printf("\n");
	}
	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