Timeout answer
#include <stdio.h>
#include <vector>
using namespace std;
int sub(vector<int> v, int N);
int c, n, m;
int in[50][2];
int ans;
int main ()
{
int i,j;
int tmp;
// C = number of test case
scanf("%d", &c);
//printf("%d\n", c);
for (i=0; i<c; i++) {
// n = number of student
// m = number of pair
scanf("%d %d", &n, &m);
//printf("n = %d, m = %d\n", n,m);
for (j=0; j<m; j++) {
// in = array of pair
scanf("%d %d", &in[j][0], &in[j][1]);
if (in[j][0] > in[j][1]) {
tmp = in[j][1];
in[j][1] = in[j][0];
in[j][0] = tmp;
}
// in[][0] < in[][1] is always TRUE
//printf("(%d, %d) ", in[j][0], in[j][1]);
}
vector<int> v;
ans = 0;
sub(v, n);
//printf("\n");
printf("%d", ans);
if (i < c-1) printf("\n");
}
}
int sub(vector<int> v, int N)
{
if (v.size() == N)
{
// Check in-set duplication
int ii=0;
while (ii<N) {
if(v.at(ii)>=v.at(ii+1)) return 0;
ii+=2;
}
// Check inter-set duplication
ii=0;
while (ii+2<N) {
if(v.at(ii)>=v.at(ii+2)) return 0;
ii+=2;
}
// All duplication removed
// Print the set
/*
ii=0;
while (ii<N) {
if (ii%2==0) printf(" (");
printf("%d ", v.at(ii));
if (ii%2==1) printf(")");
ii++;
}
printf("\n");
*/
// compare "in" and "current set"
bool bGood = false;
ii=0;
while (ii<N)
{
for(int goodFriends=0; goodFriends<m; goodFriends++)
{
if(v.at(ii)==in[goodFriends][0] && v.at(ii+1)==in[goodFriends][1])
{
bGood = true;
break;
}
}
if (bGood == false)
break;
else
{
if (ii < N-2) {
bGood = false;
}
ii+=2;
}
}
if (bGood)
{
//printf("ok\n");
ans++;
}
else
{
//printf("not ok\n");
}
return 0;
}
for (int an=0; an<N; an++)
{
bool duplicated = false;
for (int ii=0; ii<v.size(); ii++)
{
if(v.at(ii) == an) { duplicated = true; break; }
}
if (duplicated == false)
{
v.push_back(an);
sub(v, N);
v.pop_back();
}
}
}
댓글 없음:
댓글 쓰기