题目:http://poj.org/problem?id=1300
分析:简单的汉密尔顿回路问题,需要注意的是要回到node 0,因此需要判断在有两个奇度node时,start node必须是非0的那个,没有判断连通性,1A,看了discuss之后,发现竟然“判断连通性会WA,不判断就AC”,也是醉了
#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
const int MAX_N = 20;
int m, n, degree[MAX_N];
int main()
{
#ifndef ONLINE_JUDGE
freopen("__in.txt", "r", stdin);
freopen("__out.txt", "w", stdout);
#else
ios::sync_with_stdio(false);
#endif
string s;
while(getline(cin, s), s != "ENDOFINPUT"){
//init
istringstream sin(s);
sin >> s >> m >> n;
for(int i = 0; i < n; ++i) degree[i] = 0;
//input
int tot = 0;
for(int i = 0; i < n; ++i){
getline(cin, s);
if(s.empty()) continue;
istringstream sin(s);
for(int j; sin >> j; ){
++degree[i];
++degree[j];
++tot;
}
}
getline(cin, s);
//check
int x = -1, y = -1, odd = 0;
for(int i = 0; i < n; ++i){
if(degree[i] & 1){
++odd;
if(x == -1) x = i;
else if(y == -1) y = i;
}
}
if(odd != 0 && odd != 2 ||
odd == 0 && m != 0 ||
odd == 2 && (m != x && m != y || m == 0)){
cout << "NO\n";
}
else cout << "YES " << tot << "\n";
}
return 0;
}