Last active
November 17, 2018 15:31
-
-
Save Tdrj2716/fe100341153cb4d65ad401f0455aa336 to your computer and use it in GitHub Desktop.
グラフ関連のアルゴリズム集でuf.cppはatcoderのUnionFind(https://beta.atcoder.jp/contests/atc001/tasks/unionfind_a )、bfs.cppはatcoderの幅優先探索(https://beta.atcoder.jp/contests/atc002/tasks/abc007_3 )の答え)でdfs.cppはAOJの深さ優先探索(https://onlinejudge.u-aizu.ac.jp/courses/lesson/1/ALDS1/11/ALDS1_11_B )
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
#include <iostream> | |
#include <vector> | |
#include <queue> | |
#include <algorithm> | |
using namespace std; | |
#define rep(i, n) for(int (i) = 0; (i) < (n); (i)++) | |
int main(){ | |
int r, c; cin >> r >> c; | |
int sy, sx, gy, gx; cin >> sy >> sx >> gy >> gx; | |
sy--, sx--, gy--, gx--; | |
int cost[50][50]; | |
int maze[50][50]; | |
char cij; | |
rep(i, r){ | |
rep(j, c){ | |
cin >> cij; | |
maze[i][j] = (cij == '#') ? -1 : 0; | |
cost[i][j] = 0; | |
} | |
} | |
const int dx[4] = { 1, 0, -1, 0 }; | |
const int dy[4] = { 0, 1, 0, -1 }; | |
struct Point { int x, y; }; | |
queue<Point> q; q.push({ sx, sy }); | |
maze[sy][sx] = 1; | |
while(!q.empty()){ | |
Point now = q.front(); q.pop(); | |
rep(i, 4){ | |
int _x = now.x + dx[i]; | |
int _y = now.y + dy[i]; | |
if(!maze[_y][_x]){ | |
maze[_y][_x] = 1; // visited | |
cost[_y][_x] = cost[now.y][now.x] + 1; | |
q.push({ _x, _y }); | |
} | |
} | |
} | |
cout << cost[gy][gx] << endl; | |
return 0; | |
} |
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
#include <stdio.h> | |
#include <vector> | |
#include <algorithm> | |
using namespace std; | |
#define rep(i, n) for(int (i) = 0; (i) < (n); (i)++) | |
const int N = 100; | |
vector<int> edges[N]; | |
int vsted[N] = { 0 }, t[N][2], cnt = 1; | |
void dfs(int node){ | |
vsted[node] = 1; t[node][0] = cnt; | |
rep(i, edges[node].size()){ | |
int v = edges[node][i]; | |
if(!vsted[v]){ | |
cnt++; | |
dfs(v); | |
} | |
} | |
cnt++; t[node][1] = cnt; | |
} | |
int main() | |
{ | |
int n, k, v; | |
scanf("%d", &n); | |
rep(i, n){ | |
scanf("%d", &k); scanf("%d", &k); | |
rep(j, k){ | |
scanf("%d", &v); | |
edges[i].push_back(v-1); | |
} | |
} | |
dfs(0); | |
rep(i,n) printf("%d %d %d\n", i+1, t[i][0], t[i][1]); | |
return 0; | |
} |
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
#include <iostream> | |
#include <string> | |
#include <vector> | |
#include <algorithm> | |
using namespace std; | |
#define rep(i, n) for(int (i) = 0; (i) < (n); (i)++) | |
struct uf{ | |
vector<int> parent, rank; | |
uf(int n) : rank(n, 0) | |
{ | |
rep(i, n) parent.push_back(i); | |
} | |
int root(int x) | |
{ | |
return x == parent[x] ? x : root(parent[x]); | |
} | |
bool isSame(int x, int y) { return root(x) == root(y); } | |
void unite(int x, int y) | |
{ | |
x = root(x); y = root(y); | |
if(x == y) return; | |
if(rank[x] < rank[y]) parent[x] = y; | |
else{ | |
parent[y] = x; | |
if(rank[x] == rank[y]) rank[x]++; | |
} | |
} | |
}; | |
int main(){ | |
int n, q; cin >> n >> q; | |
int p, a, b; | |
vector<string> ans; | |
uf g(n); | |
rep(i, q){ | |
cin >> p >> a >> b; | |
if(!p) g.unite(a, b); | |
else | |
ans.push_back(g.isSame(a, b) ? "Yes" : "No"); | |
} | |
rep(i, ans.size()) cout << ans[i] << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment