| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
稍微剪了个枝,16ms注意遞归还原现场的坑
#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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator