-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10054.cc
66 lines (61 loc) · 1.69 KB
/
10054.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <iterator>
#include <queue>
using namespace std;
bool connect(map<int,vector<int> > list,int start){
bool vis[50] = {0};
int v = 0;
queue<int> steps;
steps.push(start);
while(!steps.empty()){
if(v == list.size())
return true;
else if(!vis[steps.front()-1]){
vis[steps.front()-1] = true;
v++;
for(int x = 0;x < list[steps.front()].size();x++)
steps.push(list[steps.front()][x]);}
steps.pop();}
return false;}
void cycle(map<int,vector<int> >& lst,vector<int>& circ,int u){
while(lst[u].size() > 0){
int temp = lst[u][0];
lst[u].erase(lst[u].begin());
lst[temp].erase(find(lst[temp].begin(),lst[temp].end(),u));
cycle(lst,circ,temp);}
circ.push_back(u);}
int main(){
int t;
cin >> t;
for(int i = 1;i <= t;i++){
int nb,p1,p2;
bool path = true,con;
map<int,vector<int> > vals;
cin >> nb;
for(int j = 0;j < nb;j++){
cin >> p1 >> p2;
vals[p1].push_back(p2);
vals[p2].push_back(p1);}
for(map<int,vector<int> >::iterator it=vals.begin();it != vals.end();it++){
if((it->second.size() % 2) == 1){
path = false;
break;}}
map<int,vector<int> >::iterator iter = vals.begin();
int starter = iter->first;
con = connect(vals,starter);
if(path && con){
vector<int> circuit;
cycle(vals,circuit,starter);
if(i > 1)
cout << endl;
cout << "Case #" << i << endl;
for(int y = 0;y < circuit.size()-1;y++)
cout << circuit[y] << ' ' << circuit[y+1] << endl;}
else{
if(i > 1)
cout << endl;
cout << "Case #" << i << endl << "some beads may be lost" << endl;}}
return 0;}