-
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.
- Loading branch information
Showing
6 changed files
with
134 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import sys | ||
sys.setrecursionlimit(10**9) | ||
def input() : return sys.stdin.readline().rstrip() | ||
|
||
n = int(input()) | ||
tree = [[] for _ in range(n+1)] | ||
visited = [-1] * (n+1) | ||
visited[1] = 0 | ||
|
||
for i in range(n-1): | ||
v, u, w = map(int, input().split()) | ||
tree[v].append((u, w)) | ||
tree[u].append((v, w)) | ||
|
||
def dfs(start, dis): | ||
for node, node_dis in tree[start]: | ||
if visited[node] == -1: | ||
visited[node] = dis + node_dis | ||
dfs(node, dis + node_dis) | ||
|
||
dfs(1,0) | ||
|
||
far_node = visited.index(max(visited)) | ||
visited = [-1] * (n+1) | ||
visited[far_node] = 0 | ||
dfs(far_node, 0) | ||
|
||
print(max(visited)) |
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
File renamed without changes.
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,53 @@ | ||
from collections import deque | ||
import sys | ||
|
||
def input(): return sys.stdin.readline().rstrip() | ||
|
||
dx = [0, 0, -1, 1] | ||
dy = [-1, 1, 0, 0] | ||
|
||
T = int(input()) | ||
|
||
for test_idx in range(T): | ||
row, col = map(int, input().split()) | ||
board = [list(map(int,input().split())) for _ in range(row)] | ||
new_board = [[0 for _ in range(col)] for _ in range(row)] | ||
|
||
for height in range(1, 1001): | ||
for start_row in range(row): | ||
for start_col in range(col): | ||
if board[start_row][start_col] >= height: continue | ||
|
||
deq = deque() | ||
deq.append((start_row, start_col)) | ||
history = {(start_row, start_col),} | ||
|
||
visited = set() | ||
visited.add((start_row, start_col)) | ||
|
||
while deq: | ||
now_row, now_col = deq.popleft() | ||
|
||
# ๊ฐ์ฅ์๋ฆฌ์ ๋ฌผ์ด ํ๋ ๋ค๋ ๊ฒ์ด๋ฏ๋ก break | ||
if now_row in (0, row-1): break | ||
if now_col in (0, col-1): break | ||
|
||
for dir in range(4): | ||
new_row = now_row + dy[dir] | ||
new_col = now_col + dx[dir] | ||
|
||
if new_row < 0 or new_row >= row: continue | ||
if new_col < 0 or new_col >= col: continue | ||
if (new_row, new_col) in visited: continue | ||
if board[new_row][new_col] >= height: continue | ||
|
||
deq.append((new_row, new_col)) | ||
history.add((new_row, new_col)) | ||
visited.add((new_row, new_col)) | ||
|
||
# ๊ฐ์ฅ์๋ฆฌ์ ๋ฌผ์ด ํ๋ฅด์ง ์์ break๋์ง ์์์ผ๋ฉด, | ||
else: | ||
for history_row, history_col in history: | ||
new_board[history_row][history_col] = height - board[history_row][history_col] | ||
|
||
print(f"Case #{test_idx+1}: {sum([sum(row) for row in new_board])}") |
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,46 @@ | ||
from collections import deque, defaultdict | ||
import sys | ||
|
||
def input(): return sys.stdin.readline().rstrip() | ||
|
||
N, M = map(int, input().split()) | ||
board = [[False for _ in range(N+1)] for _ in range(N+1)] | ||
board[1][1] = True | ||
|
||
switch = defaultdict(list) | ||
|
||
for _ in range(M): | ||
x, y, a, b = map(int, input().split()) | ||
switch[(x, y)].append((a,b)) | ||
|
||
deq = deque() | ||
deq.append((1,1)) | ||
dx = [0, 0, -1, 1] | ||
dy = [-1, 1, 0, 0] | ||
visited = {(1, 1),} | ||
dedicates = {(1, 1),} | ||
|
||
while deq: | ||
now_x, now_y = deq.popleft() | ||
|
||
for turn_on in switch[(now_x, now_y)]: | ||
if turn_on not in dedicates: # <<<------- ์ด ์ฝ๋ ํ์ค์ 3์๊ฐ ๋ ๋ฆผ | ||
dedicates.add(turn_on) | ||
|
||
if turn_on in visited: | ||
deq.append(turn_on) | ||
|
||
for dir in range(4): | ||
new_x = now_x + dx[dir] | ||
new_y = now_y + dy[dir] | ||
|
||
if new_x <= 0 or new_x >= N+1: continue | ||
if new_y <= 0 or new_y >= N+1: continue | ||
if (new_x, new_y) in visited: continue | ||
|
||
visited.add((new_x, new_y)) | ||
|
||
if (new_x, new_y) in dedicates: | ||
deq.append((new_x, new_y)) | ||
|
||
print(len(dedicates)) |