#include<iostream>
#include<stdio.h>
#include<sys/timeb.h>
using namespace std;
int V, E;
int E1[1500], E2[1500];
int Answer1, Answer2[3];
bool city[50];
bool A[50][50];
void DFS(int i)
{
int j;
city[i] = true;
for(j = 0; j < 50; j++)
{
if(A[i][j] == 1 && !city[j])
{
DFS(j);
}
}
}
int main(int argc, char** argv)
{
struct timeb startTime;
struct timeb endTime;
ftime(&startTime);
int test_case;
/*
freopen function below opens input.txt file in read only mode, and afterward,
the program will read from input.txt file instead of standard(keyboard) input.
To test your program, you may save input data in input.txt file,
and use freopen function to read from the file when using cin function.
You may remove the comment symbols(//) in the below statement and use it.
Use #include<cstdio> or #include<stdio.h> to use the function in your program.
But before submission, you must remove the freopen function or rewrite comment symbols(//).
*/
//freopen("input.txt", "r", stdin);
/*
Your program should handle 15 test cases given.
*/
//for(test_case = 1; test_case <= 15; ++test_case)
//{
int i,ii,iii;
int j;
int k;
/*
Read each test case from standard input.
The number of cities and edges are stored in V and E, respectively.
Cities connected by i-th edge is E1[i] and E2[i].
*/
cin >> V >> E;
for(i = 0; i < E; i++)
{
cin >> E1[i] >> E2[i];
}
/
bool flag = false;
for(i = 0; !flag && i < V; i++)
{
for(j = 0; j < V; j++)
{
city[j] = false;
}
for(j = 0; j < V; j++)
{
for(k = 0; k < V; k++)
{
A[j][k] = false;
}
}
for(j = 0; j < E; j++)
{
A[E1[j]-1][E2[j]-1] = true;
A[E2[j]-1][E1[j]-1] = true;
}
city[i] = true;
j = 0;
while(city[j])
{
j++;
}
DFS(j);
for(j = 0; j < V; j++)
{
if(!city[j])
{
Answer1 = 1;
Answer2[0] = i + 1;
flag = true;
break;
}
}
}
for(i = 0; !flag && i < V; i++)
{
for(ii = i + 1; !flag && ii < V; ii++)
{
for(j = 0; j < V; j++)
{
city[j] = false;
}
for(j = 0; j < V; j++)
{
for(k = 0; k < V; k++)
{
A[j][k] = false;
}
}
for(j = 0; j < E; j++)
{
A[E1[j]-1][E2[j]-1] = true;
A[E2[j]-1][E1[j]-1] = true;
}
city[i] = true;
city[ii] = true;
j = 0;
while(city[j])
{
j++;
}
DFS(j);
for(j = 0; j < V; j++)
{
if(!city[j])
{
Answer1 = 2;
Answer2[0] = i + 1;
Answer2[1] = ii + 1;
flag = true;
break;
}
}
}
}
for(i = 0; !flag && i < V; i++)
{
for(ii = i + 1; !flag && ii < V; ii++)
{
for(iii = ii + 1; !flag && iii < V; iii++)
{
for(j = 0; j < V; j++)
{
city[j] = false;
}
for(j = 0; j < V; j++)
{
for(k = 0; k < V; k++)
{
A[j][k] = false;
}
}
for(j = 0; j < E; j++)
{
A[E1[j]-1][E2[j]-1] = true;
A[E2[j]-1][E1[j]-1] = true;
}
city[i] = true;
city[ii] = true;
city[iii] = true;
j = 0;
while(city[j])
{
j++;
}
DFS(j);
for(j = 0; j < V; j++)
{
if(!city[j])
{
Answer1 = 3;
Answer2[0] = i + 1;
Answer2[1] = ii + 1;
Answer2[2] = iii + 1;
flag = true;
break;
}
}
}
}
//}
if(!flag)
{
Answer1 = -1;
}
// Print the answer to standard output(screen).
cout << "#" << test_case << " " << Answer1;
for (i = 0; i < Answer1; i++)
cout << " " << Answer2[i];
cout << endl;
}
ftime(&endTime);
cout << "TotalTime is " << (endTime.time - startTime.time) * 1000 + endTime.millitm - startTime.millitm << "ms" << endl;
return 0;//Your program should return 0 on normal termination.
}
#include<stdio.h>
#include<sys/timeb.h>
using namespace std;
int V, E;
int E1[1500], E2[1500];
int Answer1, Answer2[3];
bool city[50];
bool A[50][50];
void DFS(int i)
{
int j;
city[i] = true;
for(j = 0; j < 50; j++)
{
if(A[i][j] == 1 && !city[j])
{
DFS(j);
}
}
}
int main(int argc, char** argv)
{
struct timeb startTime;
struct timeb endTime;
ftime(&startTime);
int test_case;
/*
freopen function below opens input.txt file in read only mode, and afterward,
the program will read from input.txt file instead of standard(keyboard) input.
To test your program, you may save input data in input.txt file,
and use freopen function to read from the file when using cin function.
You may remove the comment symbols(//) in the below statement and use it.
Use #include<cstdio> or #include<stdio.h> to use the function in your program.
But before submission, you must remove the freopen function or rewrite comment symbols(//).
*/
//freopen("input.txt", "r", stdin);
/*
Your program should handle 15 test cases given.
*/
//for(test_case = 1; test_case <= 15; ++test_case)
//{
int i,ii,iii;
int j;
int k;
/*
Read each test case from standard input.
The number of cities and edges are stored in V and E, respectively.
Cities connected by i-th edge is E1[i] and E2[i].
*/
cin >> V >> E;
for(i = 0; i < E; i++)
{
cin >> E1[i] >> E2[i];
}
/
bool flag = false;
for(i = 0; !flag && i < V; i++)
{
for(j = 0; j < V; j++)
{
city[j] = false;
}
for(j = 0; j < V; j++)
{
for(k = 0; k < V; k++)
{
A[j][k] = false;
}
}
for(j = 0; j < E; j++)
{
A[E1[j]-1][E2[j]-1] = true;
A[E2[j]-1][E1[j]-1] = true;
}
city[i] = true;
j = 0;
while(city[j])
{
j++;
}
DFS(j);
for(j = 0; j < V; j++)
{
if(!city[j])
{
Answer1 = 1;
Answer2[0] = i + 1;
flag = true;
break;
}
}
}
for(i = 0; !flag && i < V; i++)
{
for(ii = i + 1; !flag && ii < V; ii++)
{
for(j = 0; j < V; j++)
{
city[j] = false;
}
for(j = 0; j < V; j++)
{
for(k = 0; k < V; k++)
{
A[j][k] = false;
}
}
for(j = 0; j < E; j++)
{
A[E1[j]-1][E2[j]-1] = true;
A[E2[j]-1][E1[j]-1] = true;
}
city[i] = true;
city[ii] = true;
j = 0;
while(city[j])
{
j++;
}
DFS(j);
for(j = 0; j < V; j++)
{
if(!city[j])
{
Answer1 = 2;
Answer2[0] = i + 1;
Answer2[1] = ii + 1;
flag = true;
break;
}
}
}
}
for(i = 0; !flag && i < V; i++)
{
for(ii = i + 1; !flag && ii < V; ii++)
{
for(iii = ii + 1; !flag && iii < V; iii++)
{
for(j = 0; j < V; j++)
{
city[j] = false;
}
for(j = 0; j < V; j++)
{
for(k = 0; k < V; k++)
{
A[j][k] = false;
}
}
for(j = 0; j < E; j++)
{
A[E1[j]-1][E2[j]-1] = true;
A[E2[j]-1][E1[j]-1] = true;
}
city[i] = true;
city[ii] = true;
city[iii] = true;
j = 0;
while(city[j])
{
j++;
}
DFS(j);
for(j = 0; j < V; j++)
{
if(!city[j])
{
Answer1 = 3;
Answer2[0] = i + 1;
Answer2[1] = ii + 1;
Answer2[2] = iii + 1;
flag = true;
break;
}
}
}
}
//}
if(!flag)
{
Answer1 = -1;
}
// Print the answer to standard output(screen).
cout << "#" << test_case << " " << Answer1;
for (i = 0; i < Answer1; i++)
cout << " " << Answer2[i];
cout << endl;
}
ftime(&endTime);
cout << "TotalTime is " << (endTime.time - startTime.time) * 1000 + endTime.millitm - startTime.millitm << "ms" << endl;
return 0;//Your program should return 0 on normal termination.
}