목록C++언어 (27)
레야몬
#include #include #include #include #define MAX_VERTEX 10001 #define MAX_TRUNK 100001 using namespace std; typedef long long int lld; int V, E; //정점의 개수 V, 간선의 개수 E vector map[MAX_TRUNK]; //정점의 연결관계 표시 맵 int root[MAX_VERTEX]; //union-find 용 lld w; //가중치의 합 int find(int x) //x의 루트 찾기 { if(root[x]==x) return x; else return root[x]=find(root[x]); } void _union(int x, int y) { x=find(x); y=find(y); ..
#include #include #define MAX_N 9 using namespace std; bool visit[MAX_N]; //방문하였는가? int map[MAX_N]; //입력받은 숫자들 int b[MAX_N]; //탐색한 숫자가 저장되는 벡터 int N, M; //N개의 자연수와 길이가 M인 수열 void f(int m) //m개를 저장함 { if(m==M) { for(int i=0; i map[i]; sort(map, map+N); //오름차순으로 정렬하기 f(0); return 0; } 백트래킹이 맞는 것 같네요. 문제를 풀고나니 왜 이게 백트래킹이지 싶었는데... 암튼 밑에 참고한 사이트가 있어요 https://blockdmask.tistory.com/70 [C++] vector con..
#include #include using namespace std; int N, M; //자연수 N, 길이가 M인 수열 void f(int n, int m, vector v) //현재 숫자와 몇 개 출력했음? { if(m==M) { //다 선택했으면 출력 for(int i=0; i
#include #include #include #define MAX_LOC 200003 using namespace std; int N, K; //수빈이의 위치, 동생의 위치 vector c(MAX_LOC); //이미 탐색했는지 확인 deque q; //갈 예정인 경로 저장 int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> N >> K; q.push_back(N); c[N]=1; while(!q.empty()) { int n=q.front(); q.pop_front(); //앞에 있는게 우선 if(n==K) break; if(2*n
#include #include #include #define MAX_NUM 101 #define MAX_WEIGHT 100001 using namespace std; //물품의 수 N, 준서가 버틸 수 있는 무게 K, 물건의 무게 W, 물건의 가치 V int N, K, W, V; int map[MAX_NUM][MAX_WEIGHT]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> N >> K; for(int i=1; i> W >> V; for(int j=1; j
#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 #define MAX_SIZE 1025 using namespace std; int N, M; //표의 크기 N, 합을 구해야 하는 횟수 M int x1, y1, x2, y2; int memo[MAX_SIZE][MAX_SIZE]; //메모리제이션 누적합 int map[MAX_SIZE][MAX_SIZE]; //맵 int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> N >> M; for(int i=1; i map[i][j]; sum+=map[i][j]; memo[i][j] = memo[i-1][j]+sum; //(0, 0)에서 (x, y)까지의 누적합 } } for(int i=0; i> x1 ..
#include #include #define MAX_N 101 #define INF 987654321 using namespace std; int n, m; //도시의 개수 n, 버스의 개수 m int map[MAX_N][MAX_N]; //각 도시에서 도시로 가는 최소 비용 int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> m; for(int i=1; i> v >> w; map[u][v]=min(map[u][v], w); } for(int k=1; k