-
Notifications
You must be signed in to change notification settings - Fork 0
/
二部图.cpp
121 lines (95 loc) · 2.22 KB
/
二部图.cpp
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
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <queue>
using std::cin;
using std::cout;
using std::endl;
using std::getline;
using std::vector;
using std::string;
using std::queue;
using std::stringstream;
using std::swap;
enum COLOR {WHITE, GRAY, BLACK};
enum TWO_COLOR {NONE, RED, BLUE};
void show_edge(const vector<vector<int> >& edge);
void BFS_and_TWOPART(const vector<vector<int> >& edge, vector<COLOR>& color, vector<TWO_COLOR>& two_color);
int main()
{
string input;
vector<vector<int> > edge;
vector<COLOR> color;
vector<TWO_COLOR> two_color;
while (getline(cin, input))
{
vector<int> this_line;
int point;
stringstream input_stream(input);
while (input_stream >> point)
{
this_line.push_back(point);
}
edge.push_back(this_line);
}
for (int i(0); i < edge.size(); ++i)
{
for (int j(0); j < edge.size() - 1; ++j)
{
if (edge[j][0] > edge[j + 1][0])
swap(edge[j], edge[j + 1]);
}
}
for (int i(0); i < edge.size(); ++i)
{
color.push_back(WHITE);
two_color.push_back(NONE);
}
// show_edge(edge);
BFS_and_TWOPART(edge, color, two_color);
cout << "wo yi yue du guan yu chao xi de shuo ming" << endl;
for (int i(0); i < edge.size(); ++i)
{
if (two_color[i] == RED)
cout << i << endl;
}
return 0;
}
void show_edge(const vector<vector<int>>& edge)
{
for (int i(0); i < edge.size(); ++i)
{
for (int j(0); j < edge[i].size(); ++j)
{
cout << edge[i][j] << " ";
}
cout << endl;
}
return;
}
void BFS_and_TWOPART(const vector<vector<int>>& edge, vector<COLOR>& color, vector<TWO_COLOR>& two_color)
{
queue<int> points;
points.push(0); // 从节点零开始进行遍历
color[0] = GRAY;
two_color[0] = RED; // 从RED开始进行染色
while(!points.empty())
{
// 当队列非空
int s(points.front());
for (int i(1); i < edge[s].size(); ++i) // 对于所有与s相连的节点
{
if (color[edge[s][i]] == WHITE)
{
points.push(edge[s][i]);
color[edge[s][i]] = GRAY;
two_color[s] == RED ? two_color[edge[s][i]] = BLUE : two_color[edge[s][i]] = RED;
}
}
points.pop();
color[s] = BLACK;
}
return;
}