// 인접리스트
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
vector<int> a[1001];
bool check[1001];
void dfs(int node) {
check[node] = true;
printf("%d ",node);
for (int i=0; i<a[node].size(); i++) {
int next = a[node][i];
if (check[next] == false) {
dfs(next);
}
}
}
void bfs(int start) {
queue<int> q;
memset(check,false,sizeof(check));
check[start] = true;
q.push(start);
while (!q.empty()) {
int node = q.front();
q.pop();
printf("%d ",node);
for (int i=0; i<a[node].size(); i++) {
int next = a[node][i];
if (check[next] == false) {
check[next] = true;
q.push(next);
}
}
}
}
int main() {
int n, m, start;
scanf("%d %d %d",&n,&m,&start);
for (int i=0; i<m; i++) {
int u,v;
scanf("%d %d",&u,&v);
a[u].push_back(v);
a[v].push_back(u);
}
for (int i=1; i<=n; i++) {
sort(a[i].begin(), a[i].end());
}
dfs(start);
puts("");
bfs(start);
puts("");
return 0;
}
// 간선 리스트
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
struct Edge {
int from, to;
};
Edge edge[20001];
int cnt[1001];
bool check[1001];
bool cmp(const Edge &u, const Edge &v) {
if (u.from == v.from) {
return u.to < v.to;
} else {
return u.from < v.from;
}
}
void dfs(int node) {
check[node] = true;
printf("%d ",node);
for (int i=cnt[node-1]; i<cnt[node]; i++) {
int next = edge[i].to;
if (check[next] == false) {
dfs(next);
}
}
}
void bfs(int start) {
queue<int> q;
q.push(start);
check[start] = true;
while (!q.empty()) {
int node = q.front();
q.pop();
printf("%d ",node);
for (int i=cnt[node-1]; i<cnt[node]; i++) {
int next = edge[i].to;
if (check[next] == false) {
check[next] = true;
q.push(next);
}
}
}
}
int main() {
int n, m, start;
scanf("%d %d %d",&n,&m,&start);
for (int i=0; i<m; i++) {
scanf("%d %d",&edge[i].from, &edge[i].to);
edge[i+m].from = edge[i].to;
edge[i+m].to = edge[i].from;
}
m *= 2;
sort(edge, edge+m, cmp);
for (int i=0; i<m; i++) {
cnt[edge[i].from] += 1;
}
for (int i=1; i<=n; i++) {
cnt[i] += cnt[i-1];
}
dfs(start);
printf("\n");
memset(check,false,sizeof(check));
bfs(start);
printf("\n");
return 0;
}
#include <cstdio>
#include <stack>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
vector<int> a[1001];
bool check[1001];
void dfs(int node) {
stack<pair<int,int>> s;
s.push(make_pair(node,0));
check[node] = true;
printf("%d ",node);
while (!s.empty()) {
int node = s.top().first;
int start = s.top().second;
s.pop();
for (int i=start; i<a[node].size(); i++) {
int next = a[node][i];
if (check[next] == false) {
printf("%d ",next);
check[next] = true;
s.push(make_pair(node,i+1));
s.push(make_pair(next,0));
break;
}
}
}
}
void bfs(int start) {
queue<int> q;
memset(check,false,sizeof(check));
check[start] = true;
q.push(start);
while (!q.empty()) {
int node = q.front();
q.pop();
printf("%d ",node);
for (int i=0; i<a[node].size(); i++) {
int next = a[node][i];
if (check[next] == false) {
check[next] = true;
q.push(next);
}
}
}
}
int main() {
int n, m, start;
scanf("%d %d %d",&n,&m,&start);
for (int i=0; i<m; i++) {
int u,v;
scanf("%d %d",&u,&v);
a[u].push_back(v);
a[v].push_back(u);
}
for (int i=1; i<=n; i++) {
sort(a[i].begin(), a[i].end());
}
dfs(start);
puts("");
bfs(start);
puts("");
return 0;
}
#include <cstdio>
#include <stack>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
vector<int> a[1001];
bool check[1001];
int start[1001];
void dfs(int node) {
stack<int> s;
s.push(node);
check[node] = true;
printf("%d ",node);
while (!s.empty()) {
int node = s.top();
s.pop();
for (int &i=start[node]; i<a[node].size(); i++) {
int next = a[node][i];
if (check[next] == false) {
printf("%d ",next);
check[next] = true;
s.push(node);
s.push(next);
break;
}
}
}
}
void bfs(int start) {
queue<int> q;
memset(check,false,sizeof(check));
check[start] = true;
q.push(start);
while (!q.empty()) {
int node = q.front();
q.pop();
printf("%d ",node);
for (int i=0; i<a[node].size(); i++) {
int next = a[node][i];
if (check[next] == false) {
check[next] = true;
q.push(next);
}
}
}
}
int main() {
int n, m, start;
scanf("%d %d %d",&n,&m,&start);
for (int i=0; i<m; i++) {
int u,v;
scanf("%d %d",&u,&v);
a[u].push_back(v);
a[v].push_back(u);
}
for (int i=1; i<=n; i++) {
sort(a[i].begin(), a[i].end());
}
dfs(start);
puts("");
bfs(start);
puts("");
return 0;
}
'Problem set' 카테고리의 다른 글
[백준] 1707 이분 그래프 (0) | 2021.02.20 |
---|---|
[백준] 11724 연결 요소의 개수 (0) | 2021.02.20 |
[백준] 13023 ABCDE (0) | 2021.02.20 |
[백준] 14391 종이 조각 (0) | 2021.02.20 |
[백준] 1182 부분수열의 합 (0) | 2021.02.20 |