Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/week4/BOJ13244/BOJ13244_V1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <bits/stdc++.h>
using namespace std;
int visited[10][1001];
vector<vector<vector<int>>> injs;
vector<bool> isTree;
int N;
bool dfs(int here, int num, int prev) {
visited[num][here] = 1;
for (int next : injs[num][here]) {
if (next == prev) continue;
if (visited[num][next]) return false;
if (!dfs(next, num, here)) return false;
}

return true;
}
int main() {

cin >> N;
injs.resize(N,vector<vector<int>>());
isTree.resize(N);
for (int i = 0; i < N; i++) {
int nodes;
int inp_num;
bool tree_flag = true;
cin >> nodes;
cin >> inp_num;
injs[i].resize(nodes+1);
if (inp_num != nodes-1)
tree_flag = false;
for (int j=0; j<inp_num; j++) {
int a,b;
cin >> a >> b;
injs[i][a].push_back(b);
injs[i][b].push_back(a);
}
if (tree_flag && !dfs(1, i, 0))
tree_flag = false;


for (int j=1; j<=nodes; j++) {
if (!visited[i][j]) {
tree_flag = false;
break;
}
}

isTree[i] = tree_flag;
}


for (int i = 0; i < N; i++) {
if (isTree[i])
cout << "tree" << '\n';
else
cout << "graph" << '\n';
}
return 0;
}
57 changes: 57 additions & 0 deletions src/week4/BOJ13244/BOJ13244_V2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <bits/stdc++.h>
using namespace std;
int N,visited[1001];
vector<vector<int>> inj;
vector<bool> ret;
void dfs(int here) {
visited[here] = 1;
for (int next : inj[here]) {
if (!visited[next]) dfs(next);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);

cin >> N;
for (int i = 0; i < N; i++) {
memset(visited, 0, sizeof(visited));
int nodes;
int inp_num;
cin >> nodes >> inp_num;
inj.clear();
inj.resize(nodes + 1);

for (int j = 0; j < inp_num; j++) {
int a, b;
cin >> a >> b;
inj[a].push_back(b);
inj[b].push_back(a);
}

if (inp_num != nodes - 1) {
ret.push_back(false);
continue;
}

int cnt = 0;
for (int j = 1; j <= nodes; j++) {
if (!visited[j]) {
dfs(j);
cnt++;
}
}
if (cnt != 1) {
ret.push_back(false);
continue;
}

ret.push_back(true);
}

for (int i = 0; i < N; i++) {
if (ret[i]) cout << "tree\n";
else cout << "graph\n";
}
}
Loading