목록DFS (5)
레야몬
#include #include #include #define FOR(i, k, N) for(int i=k; i> V >> E; FOR(i, 1, E) { int a, b; cin >> a >> b; Vertex[a].push_back(b); Vertex[b].push_back(a); } } int dfs(int no, int par) { discovered[no] = ++cnt; //발견된 순서(1부터 시작) int ret = discovered[no]; FOR(i, 1, Vertex[no].size()) { int next = Vertex[no][i-1]; //탐색할 노드 if(next == par) continue; if(!discovered[next]) { //탐색이 아직 되지 않았으면 int s..
#include #include #include #define LOOP(i, N) for(int i=1; i> n; LOOP(i, n) cin >> num[i]; } void dfs(int no) //node { if(fi[no]) return; if(vi[no]) { res += cnt - ord[no]; //사이클을 발생시킬 경우 탐색된 순서의 차이가 사이클을 구성하는 노드의 개수 return; } ord[no]=cnt++; //탐색된 순서 기록 vi[no]=1; dfs(num[no]); fi[no]=1; //탐색이 다끝나면 더 이상 보지 않음 } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> T..
#include #include #define MAX_NODE 100001 using namespace std; vector node[MAX_NODE]; //두 노드 간의 연결 표현 int mom[MAX_NODE]; //한 노드의 부모 노드 메모 int N; //노드의 개수 void f(int no, int mo) //node 넘버와 엄마 넘버 { mom[no]=mo; for(int i=0; i> N; for(int i=0; i> u >> v; node[u].push_back(v); node[v].push_back(u); } f(1, 0); for(int i=2; i
#include #include #define MAX_MAP_SIZE 1002 #define IOF 987654321 using namespace std; typedef struct _loc { int x; int y; int nb_cnt; //부수다: break, 부수지 않다: not break int b_cnt; } loc; int Dist[MAX_MAP_SIZE][MAX_MAP_SIZE][2]; //거리의 최솟값(안 부쉈나? 부쉈나?) char Map[MAX_MAP_SIZE][MAX_MAP_SIZE]; //맵 queue q; //bfs용 큐 int N, M; //맵의 크기 int X_d[4] = {1, 0, -1, 0}; //X direction int Y_d[4] = {0, -1, 0, 1}; i..
#include #define MAX_GRAPH_SIZE 1009 using namespace std; int graph[MAX_GRAPH_SIZE][MAX_GRAPH_SIZE]; //행렬을 이용한 그래프 구현 int N, M; //정점의 개수 N, 간선의 개수 M int cnt; //연결 요소의 개수 void f(int u) { if (!u) { //초기상태인가? for (int i=1; i> v; graph[u][v]=1, graph[v][u]=1; } f(0); for (int i=1; i [C++언어] 11279번 최대 힙 - 우선순위 큐 #include #include #include #include using namespace std; priority_queue , less > max_pq; ..