-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add graph_util #97
Merged
Merged
add graph_util #97
Changes from 3 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
88abce5
add graph_util
shogo314 3b03735
change test
shogo314 33267e1
update doxygen
shogo314 9971570
fix doxygen
shogo314 3f55cfb
add connected_components
shogo314 8ccc0ab
change testcase
shogo314 e6d61f4
add testcase
shogo314 3fa3297
remove testcase
shogo314 40375ef
[auto-verifier] verify commit 3fa32974801ce23f533e228445d106aef500a0ef
web-flow 1798c11
add is_connected
shogo314 657d420
[auto-verifier] verify commit 1798c118664597324ea5d438ed0d2b1fecbee8da
web-flow 480e151
add is_tree
shogo314 6788bb5
[auto-verifier] verify commit 480e1519a6646cec9d06092cfeb187b3e239521d
web-flow dd92a30
mv topo
shogo314 8639ed0
[auto-verifier] verify commit dd92a30844b2d1f7abec98023c280e1fb6a2ba5f
web-flow 2cd4391
delete settings.json
shogo314 ed92062
Merge branch 'main' into graph_util
KowerKoint ae253f1
[auto-verifier] verify commit ed920623f9bf122d6f083036b5701b3440964009
web-flow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#pragma once | ||
|
||
/** | ||
* @file graph_util.hpp | ||
* @brief グラフに関する関数 | ||
*/ | ||
|
||
#include <stack> | ||
|
||
#include "graph.hpp" | ||
|
||
/** | ||
* @brief 無向グラフについて、二部グラフなら0と1に彩色した結果をひとつ返し、二部グラフでないなら空のvectorを返す。 | ||
* 連結成分のうち、インデックスの小さいものを0にする。 | ||
* @return std::vector<int> 各頂点の彩色結果 | ||
*/ | ||
template <typename Cost = int> | ||
std::vector<int> bipartite_coloring(const Graph<Cost>& graph) { | ||
std::vector<int> color(graph.n, -1); | ||
for (int i = 0; i < graph.n; i++) { | ||
if (color[i] != -1) continue; | ||
std::stack<int> stk; | ||
stk.push(i); | ||
color[i] = 0; | ||
while (!stk.empty()) { | ||
int u = stk.top(); | ||
stk.pop(); | ||
for (int v : graph[u]) { | ||
if (color[v] == -1) { | ||
color[v] = color[u] ^ 1; | ||
stk.push(v); | ||
} else { | ||
if (color[u] == color[v]) return {}; | ||
} | ||
} | ||
} | ||
} | ||
return color; | ||
} | ||
|
||
/** | ||
* @brief 無向グラフについて、二部グラフかどうかを判定する。 | ||
* @return bool 二部グラフならtrue、二部グラフでないならfalseを返す。 | ||
*/ | ||
template <typename Cost = int> | ||
bool is_bipartite(const Graph<Cost>& graph) { | ||
return !bipartite_coloring(graph).empty(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#define PROBLEM "https://atcoder.jp/contests/abc126/tasks/abc126_d" | ||
|
||
#include "../cpp/graph_util.hpp" | ||
|
||
int main() { | ||
int N; | ||
std::cin >> N; | ||
Graph<int> graph(2 * N); | ||
int tmp = N; | ||
for (int i = 0; i < N - 1; i++) { | ||
int u, v, w; | ||
std::cin >> u >> v >> w; | ||
u--; | ||
v--; | ||
if (w % 2) { | ||
graph.add_edge(u, tmp); | ||
graph.add_edge(v, tmp); | ||
tmp++; | ||
} else { | ||
graph.add_edge(u, v); | ||
} | ||
} | ||
std::vector<int> color = bipartite_coloring(graph); | ||
for (int i = 0; i < N; i++) { | ||
std::cout << color[i] << std::endl; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
スペシャルジャッジの問題をverifyする手段が今のところないので、固定入出力の問題にするか
#define IGNORE
をつけてください。