-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into 26-alstjr7437
- Loading branch information
Showing
5 changed files
with
151 additions
and
6 deletions.
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
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,41 @@ | ||
from collections import deque | ||
import sys | ||
input = sys.stdin.readline | ||
|
||
t = int(input()) | ||
|
||
for _ in range(t): | ||
temp_reverse = False | ||
error = 0 | ||
|
||
p = input() | ||
n = int(input()) | ||
x = deque(input().strip()[1:-1].split(',')) | ||
|
||
if n == 0 : | ||
x = deque() | ||
|
||
for i in p: | ||
if i == 'R': | ||
if temp_reverse : | ||
temp_reverse = False | ||
else : | ||
temp_reverse = True | ||
|
||
if i == "D": | ||
if len(x) == 0: | ||
error = 1 | ||
break | ||
else : | ||
if temp_reverse : | ||
x.pop() | ||
else : | ||
x.popleft() | ||
|
||
if temp_reverse : | ||
x.reverse() | ||
|
||
if error == 0 : | ||
print(f"[{','.join(str(i) for i in x)}]") | ||
else : | ||
print("error") |
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
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,58 @@ | ||
from collections import defaultdict | ||
from heapq import * | ||
import sys | ||
|
||
INF = 50_000_001 | ||
|
||
def input(): return sys.stdin.readline().rstrip() | ||
|
||
T = int(input()) | ||
|
||
for _ in range(T): | ||
cnt_node, cnt_edge, cnt_dedication = map(int, input().split()) | ||
start_node, g, h = map(int, input().split()) | ||
|
||
graph = defaultdict(lambda : defaultdict(int)) | ||
for _ in range(cnt_edge): | ||
start, destination, cost = map(int,input().split()) | ||
graph[start][destination] = cost | ||
graph[destination][start] = cost | ||
|
||
node_dedications = set() | ||
for _ in range(cnt_dedication): | ||
node_dedications.add(int(input())) | ||
|
||
table = [INF for _ in range(cnt_node+1)] | ||
table[start_node] = 0 | ||
|
||
visited = set() | ||
heap = [] | ||
heappush(heap, [0, start_node, 1]) | ||
flag = [1 for _ in range(cnt_node+1)] | ||
|
||
while heap: | ||
now_cost, now_node, now_flag = heappop(heap) | ||
|
||
if now_node in visited: continue | ||
visited.add(now_node) | ||
|
||
if flag[now_node] == 1 and now_flag == 0: flag[now_node] = 0 | ||
|
||
for next_node in graph[now_node]: | ||
cost = graph[now_node][next_node] | ||
|
||
if (now_cost + cost) > table[next_node]: continue | ||
|
||
table[next_node] = now_cost + cost | ||
|
||
new_flag = now_flag | ||
# ๋ง์ฝ, ์ง๊ธ ์์ง์ด๋ ค๋ ๋๋ก๊ฐ ๋์๊ฐ ๋๋ ๋๋ก์์ ๊ฒฝ์ฐ flag๋ฅผ 0๋ก ๋ฐ๊ฟ | ||
if (now_node, next_node) in {(g, h), (h, g)}: new_flag = 0 | ||
|
||
heappush(heap, [now_cost + cost, next_node, new_flag]) | ||
|
||
answer = [] | ||
for dedication in node_dedications: | ||
if flag[dedication] == 0: answer.append(dedication) | ||
|
||
print(*sorted(answer)) |
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,40 @@ | ||
import sys | ||
from collections import defaultdict | ||
|
||
def input(): return sys.stdin.readline().rstrip() | ||
|
||
def find_parent(element, graph): | ||
if graph[element] == element: return element | ||
|
||
parent = graph[element] | ||
graph[element] = find_parent(parent, graph) | ||
return graph[element] | ||
|
||
def union(first, second, graph, count): | ||
x = find_parent(first, graph) | ||
y = find_parent(second, graph) | ||
|
||
if x == y: return | ||
|
||
x, y = min(x, y), max(x, y) | ||
graph[y] = x | ||
count[x] += count[y] | ||
count[y] = 0 | ||
return | ||
|
||
T = int(input()) | ||
|
||
for _ in range(T): | ||
F = int(input()) | ||
index = 1 | ||
parent = defaultdict(str) | ||
count = defaultdict(lambda : 1) | ||
|
||
for _ in range(F): | ||
first, second = input().split() | ||
if parent[first] == "": parent[first] = first | ||
if parent[second] == "": parent[second] = second | ||
if parent[first] != parent[second]: union(first, second, parent, count) | ||
|
||
print(count[find_parent(first, parent)]) | ||
|