디시인사이드 갤러리

마이너 갤러리 이슈박스, 최근방문 갤러리

갤러리 본문 영역

1129. Shortest Path with Alternating Col

이벤트(1.226) 2023.02.12 05:25:41
조회 17 추천 0 댓글 0
														
class Solution {
public:
int RED = 0;
int BLUE = 1;
vector<vector<pair<int, int>>> graph;

vector<int> shortestAlternatingPaths(int n, vector<vector<int>>& redEdges, vector<vector<int>>& blueEdges) {
graph.clear();
graph.resize(n);
for(auto& v: redEdges) {
graph[v[0]].emplace_back(RED, v[1]);
}
for(auto& v: blueEdges) {
graph[v[0]].emplace_back(BLUE, v[1]);
}

// color, current node
queue<pair<int,int>> q;
vector<vector<int>> distance(2, vector<int>(n, 1e9));
distance[BLUE][0] = distance[RED][0] = 0;
q.push({RED, 0});
q.push({BLUE, 0});

while(!q.empty()) {
auto [color, idx] = q.front();
q.pop();
int dist = distance[color][idx];

for(int i = 0 ; i < graph[idx].size(); i++) {
auto [ncolor, nidx] = graph[idx][i];
if(ncolor != color && distance[ncolor][nidx] == 1e9) {
distance[ncolor][nidx] = dist + 1;
q.push({ncolor, nidx});
}
}
}

vector<int> ret = distance[RED];
for(int i = 0; i < n; i++) {
ret[i] = min(ret[i], distance[BLUE][i]);
if(ret[i] == 1e9) {
ret[i] = -1;
}
}

return ret;
}
};


잊어먹고 있다가

자고 일어나서 품..

추천 비추천

0

고정닉 0

0

댓글 영역

전체 댓글 0
본문 보기

하단 갤러리 리스트 영역

왼쪽 컨텐츠 영역

갤러리 리스트 영역

갤러리 리스트
번호 제목 글쓴이 작성일 조회 추천
설문 2025년 가장 기억에 남는 인터넷 이슈는? 운영자 25/12/22 - -
214 1675. Minimize Deviation in Array 개발뉴비갤로그로 이동합니다. 23.02.25 27 0
212 1675. Minimize Deviation in Array chromate00갤로그로 이동합니다. 23.02.25 32 0
211 502. IPO 이벤트(1.226) 23.02.23 26 0
209 502. IPO 개발뉴비갤로그로 이동합니다. 23.02.23 30 0
208 502. IPO chromate00갤로그로 이동합니다. 23.02.23 31 0
207 1011. Capacity To Ship Packages Within D 이벤트(1.226) 23.02.22 16 0
206 1011. Capacity To Ship Packages Within D 개발뉴비갤로그로 이동합니다. 23.02.22 28 0
205 1011. Capacity To Ship Packages Within D chromate00갤로그로 이동합니다. 23.02.22 20 0
204 540. Single Element in a Sorted Array 개발뉴비갤로그로 이동합니다. 23.02.21 26 0
203 540. Single Element in a Sorted Array 이벤트(1.226) 23.02.21 13 0
202 540. Single Element in a Sorted Array chromate00갤로그로 이동합니다. 23.02.21 21 0
201 35. Search Insert Position 이벤트(1.226) 23.02.21 18 0
200 35. Search Insert Position 개발뉴비갤로그로 이동합니다. 23.02.20 28 0
199 35. Search Insert Position chromate00갤로그로 이동합니다. 23.02.20 27 0
198 103. Binary Tree Zigzag Level Order Trav 개발뉴비갤로그로 이동합니다. 23.02.19 23 0
196 103. Binary Tree Zigzag Level Order Trav chromate00갤로그로 이동합니다. 23.02.19 20 0
195 103. Binary Tree Zigzag Level Order Trav 이벤트(1.226) 23.02.19 18 0
194 226. Invert Binary Tree chromate00갤로그로 이동합니다. 23.02.18 14 0
193 226. Invert Binary Tree 개발뉴비갤로그로 이동합니다. 23.02.18 23 0
191 226. Invert Binary Tree 이벤트(1.226) 23.02.18 22 0
189 783. Minimum Distance Between BST Nodes 개발뉴비갤로그로 이동합니다. 23.02.18 18 0
188 783. Minimum Distance Between BST Nodes chromate00갤로그로 이동합니다. 23.02.17 19 0
187 783. Minimum Distance Between BST Nodes 이벤트(1.226) 23.02.17 24 0
186 104. Maximum Depth of Binary Tree 개발뉴비갤로그로 이동합니다. 23.02.17 23 0
185 104. Maximum Depth of Binary Tree 이벤트(1.226) 23.02.16 16 0
183 104. Maximum Depth of Binary Tree chromate00갤로그로 이동합니다. 23.02.16 42 0
182 989. Add to Array-Form of Integer 이벤트(1.226) 23.02.15 17 0
181 989. Add to Array-Form of Integer chromate00갤로그로 이동합니다. 23.02.15 19 0
180 989. Add to Array-Form of Integer 개발뉴비갤로그로 이동합니다. 23.02.15 20 0
179 67. Add Binary 개발뉴비갤로그로 이동합니다. 23.02.14 30 0
178 67. Add Binary 이벤트(1.226) 23.02.14 21 0
177 67. Add Binary chromate00갤로그로 이동합니다. 23.02.14 31 0
176 1523. Count Odd Numbers in an Interval R chromate00갤로그로 이동합니다. 23.02.14 25 0
175 1523. Count Odd Numbers in an Interval R 이벤트(1.226) 23.02.13 17 0
174 1523. Count Odd Numbers in an Interval R 개발뉴비갤로그로 이동합니다. 23.02.13 28 0
173 2477. Minimum Fuel Cost to Report to the 개발뉴비갤로그로 이동합니다. 23.02.13 27 0
172 구현 문제 풀 때 꿀팁? 구현력 기르는 꿀팁? 있으신가요 ? [1] ㅇㅇ(180.67) 23.02.12 51 0
170 2477. Minimum Fuel Cost to Report to the 이벤트(1.226) 23.02.12 23 0
169 2477. Minimum Fuel Cost to Report to the [1] chromate00갤로그로 이동합니다. 23.02.12 29 0
168 2477. Minimum Fuel Cost to Report to the ㅇㅇ(218.234) 23.02.12 20 0
1129. Shortest Path with Alternating Col 이벤트(1.226) 23.02.12 17 0
166 1129. Shortest Path with Alternating Col [1] 개발뉴비갤로그로 이동합니다. 23.02.11 54 0
165 1129. Shortest Path with Alternating Col chromate00갤로그로 이동합니다. 23.02.11 35 0
163 오늘의 데일리 (feat. ChatGPT) 개발뉴비갤로그로 이동합니다. 23.02.11 46 2
162 1162. As Far from Land as Possible chromate00갤로그로 이동합니다. 23.02.10 25 0
161 1162. As Far from Land as Possible 이벤트(1.226) 23.02.10 22 0
160 데일리 [1] ㅇㅇ(223.62) 23.02.10 38 0
159 오늘은 포기 이벤트(39.7) 23.02.09 32 1
158 2306. Naming a Company. chromate00갤로그로 이동합니다. 23.02.09 21 0
157 2306. Naming a Company 개발뉴비갤로그로 이동합니다. 23.02.09 41 0
갤러리 내부 검색
제목+내용게시물 정렬 옵션

오른쪽 컨텐츠 영역

실시간 베스트

1/8

디시미디어

디시이슈

1/2