Использовать представление графа в виде списков смежности, вывести на экран все вершины, не смежные с данной.
Может кто знает что не так
C++ |
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
| #include<fstream>
#include<string>
#include<iostream>
#include<sstream>
using namespace std;
ifstream in("input.txt");
ofstream out("output.txt");
class DoubleLinkedList
{
struct Element
{
int inf;
Element *next;
Element *prev;
Element(int x) :inf(x), next(0), prev(0)
{
}
};
Element *head;
Element * tail;
int size;
Element *Find(int index) //возвращает указатель на элемент списка с номером index
{
if ((index<1) || (index>size))
{
return NULL;
}
else
{
Element *cur = head;
for (int i = 1; i<index; i++)
{
cur = cur->next;
}
return cur;
}
}
public:
DoubleLinkedList() :head(0), tail(0), size(0)
{
}
~DoubleLinkedList()
{
while (!Empty())
{
Remove(1);
}
}
bool Empty() // проверяет список на пустоту
{
return head == 0;
}
int GetLength() // возвращает количество элементов в списке
{
return size;
}
int Get(int index) // возвращает значение элемента списка по его номеру
{
if ((index<1) || (index>size))
{
out << "error.Get" << endl;
return 0;
}
else
{
Element *r = Find(index);
int i = r->inf;
return i;
}
}
void InsertRight(int index, int data) //осуществляет вставку элемента со значением data справа от эелмента с позицией index
{
Element *newPtr = new Element(data);
size = GetLength() + 1;
Element *cur = Find(index);
if (cur == NULL)
{
head = newPtr;
tail = newPtr;
}
else
{
newPtr->next = cur->next;
newPtr->prev = cur;
cur->next = newPtr;
if (cur == tail)
{
tail = newPtr;
}
else
{
newPtr->next->prev = newPtr;
}
}
}
void Remove(int index) //осуществзяет удаление элемента с номером index из списка
{
Element *cur = Find(index);
--size;
if (size == 0)
{
head = NULL;
tail = NULL;
}
else if (cur == head)
{
head = head->next;
head->prev = NULL;
}
else if (cur == tail)
{
tail = tail->prev;
tail->next = NULL;
}
else
{
cur->prev->next = cur->next;
cur->next->prev = cur->prev;
}
cur->next = NULL;
cur->prev = NULL;
delete cur;
}
void PrintLeftToRight()
{
for (Element *cur = head; cur != NULL; cur = cur->next)
{
out << cur->inf << " ";
}
out << endl;
}
};
int main()
{
int n;
in >> n;
string s;
getline(in, s);
DoubleLinkedList *w = new DoubleLinkedList[n];
for (int i = 0; i <n; i++)
{
getline(in, s);
stringstream sin(s);
int temp;
while (sin >> temp)
{
w[i].InsertRight(w[i].GetLength(), temp);
}
};
int v;
in>>v;
cout << "Vershini ne smeznie s dannoy " << v << ":" << endl;
for (int i = 0; i < n; i++) {
bool f = false;
for (int j = 0; j < w[i].GetLength(); j++)
if (i == w[i].Get(j) || i == v) {
f = true;
break;
}
if (!f)
cout << i + 1 << ' ';
}
;
out.close();
return 0;
} |
|