-
Notifications
You must be signed in to change notification settings - Fork 6
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
1 parent
83dff68
commit 7247d82
Showing
9 changed files
with
587 additions
and
276 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,59 @@ | ||
import sys; input = sys.stdin.readline; from array import * | ||
|
||
class Node: | ||
z = 0 | ||
def __init__(self, c, l): self.id = self.z; Node.z += 1; X.append(self); self.ch = {}; self.c = c; self.l = l | ||
|
||
# Construct trie | ||
V, N = input().split(); V = ord(V)-96; N = int(N); W = [[ord(x)-97 for x in input().strip()] for _ in range(N)]; G = [set() for _ in range(V)]; indeg, top = [0]*V, []; X = []; T = Node(-1, -1) | ||
for n in range(N): | ||
w = W[n]; t = T | ||
for k, i in enumerate(w): | ||
if i not in t.ch: t.ch[i] = Node(i, k) | ||
t = t.ch[i] | ||
vg = Node.z; g = [set() for _ in range(vg)]; p = []; q = {} | ||
for n in range(N): | ||
w = W[n]; t = T | ||
for i in w: g[t.id].add(t.ch[i].id); g[t.ch[i].id].add(t.id); t = t.ch[i] | ||
p.append(t.id); q[t.id] = n | ||
for i in range(vg): g[i] = [*g[i]] | ||
|
||
# Tarjan's OLCA | ||
Q = [[] for _ in range(vg)]; R = []; d = array('i', [0]*vg); par = array('i', range(vg)); d[0] = 1; s = [(0, 0)]; Z = 0 | ||
for i in range(N): | ||
for j in range(i): R.append([p[i], p[j], None]), Q[p[i]].append(Z), Q[p[j]].append(Z); Z += 1 | ||
def find(i): | ||
if par[i] == i: return i | ||
par[i] = find(par[i]); return par[i] | ||
while s: | ||
ub, p = s.pop(); u = ub//2 | ||
if ub%2: | ||
for x in Q[u]: | ||
if R[x][1] == u: R[x][0], R[x][1] = R[x][1], R[x][0] | ||
R[x][2] = find(R[x][1]) | ||
par[u] = p | ||
else: | ||
s.append((ub+1, p)) | ||
for t in g[u]: | ||
if t != p: d[t] = d[u]+1; s.append((2*t, u)) | ||
for a, b, lca in R: | ||
lcp = X[lca].l+1 if lca != vg-1 else 0 | ||
if lcp < len(W[q[a]]) and lcp < len(W[q[b]]): G[W[min(q[a], q[b])][lcp]].add(W[max(q[a], q[b])][lcp]) | ||
elif lcp == len(W[max(q[a], q[b])]): print('IMPOSSIBLE'), exit(0) | ||
|
||
# Topological sort | ||
for v in range(V): | ||
for w in G[v]: indeg[w] += 1 | ||
q = [i for i in range(V) if indeg[i] == 0]; r = [set() for _ in range(2*V)]; z = 0 | ||
for i in range(V): r[indeg[i]].add(i) | ||
for u in q: | ||
if len(r[0]) > 1: z = 1 | ||
top.append(u); r[0].discard(u) | ||
for v in G[u]: | ||
r[indeg[v]].discard(v) | ||
indeg[v] -= 1 | ||
r[indeg[v]].add(v) | ||
if indeg[v] == 0: q.append(v) | ||
if len(top) != V: print('IMPOSSIBLE') | ||
elif z: print('AMBIGUOUS') | ||
else: print(''.join(chr(i+97) for i in top)) |
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,22 @@ | ||
import sys; input = sys.stdin.readline | ||
R = {}; G = []; X = []; V = ('hillbilly', 'paradox', 'paradox') | ||
while True: | ||
s = input().strip() | ||
if not s: break | ||
if s != 'done': | ||
a, _, b = s.split() | ||
if a not in R: R[a] = len(R); G.append([]); X.append(a) | ||
if b not in R: R[b] = len(R); G.append([]); X.append(b) | ||
G[R[b]].append(R[a]) | ||
continue | ||
N = len(R); Z = [0]*N | ||
for i in range(N): | ||
# DFS from i as source, if got multiple visits, gets a report | ||
s = [i]; v = [0]*N | ||
while s: | ||
u = s.pop() | ||
if v[u]: Z[i] |= 1<<(u==i); continue | ||
v[u] = 1; s.extend(G[u]) | ||
for i in sorted(R): | ||
if Z[R[i]]: print(i, V[Z[R[i]]-1]) | ||
R = {}; G = []; print() |
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,11 @@ | ||
import sys; input = sys.stdin.readline; from array import * | ||
V, E = map(int, input().split()); M = [0]*V; R = {1<<i: i for i in range(V)}; D = array('i', [-1]*(1<<V)); B = array('i', [0]*(1<<V)) | ||
for _ in range(E): a, b, w = map(int, input().split()); M[a] -= w; M[b] += w | ||
for i in range(1, 1<<V): B[i] = B[i-(i&-i)]+M[R[i&-i]] | ||
def dp(bm): | ||
if bm == 0: return 0 | ||
if D[bm] != -1: return D[bm] | ||
bm2 = bm; ans = 0 | ||
while bm2: nxt = bm2&-bm2; bm2 ^= nxt; ans = max(ans, dp(bm^nxt)) | ||
D[bm] = ans+(B[bm]==0); return D[bm] | ||
print(V-dp(2**V-1)) |
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,17 @@ | ||
import sys; input = sys.stdin.readline; from heapq import *; from array import * | ||
xmin, xmax = map(int, input().split()); C = array('i'); L = []; H = []; D = array('b', [0]*(N:=10**6+1)); M = 10**9+7 | ||
for _ in range(int(input())): | ||
t, *r = input().split() | ||
if t < '-': | ||
s, lo, hi = map(int, r) | ||
if lo <= xmin and xmax <= hi: C.append(s) | ||
elif lo <= xmin: heappush(L, (M-hi)*N+s) | ||
elif xmax <= hi: heappush(H, lo*N+s) | ||
else: | ||
D[int(r[0])] = 1 # lazy deletion | ||
while C and D[C[-1]]: C.pop() | ||
while L and D[L[0]%N]: heappop(L) | ||
while H and D[H[0]%N]: heappop(H) | ||
if C: print(1) | ||
elif L and H and L[0]//N+H[0]//N <= M: print(2) | ||
else: print(-1) |
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; input = sys.stdin.readline; TC = 0 | ||
|
||
def d2(a, b): | ||
return (a[0]-b[0])**2+(a[1]-b[1])**2 | ||
|
||
def intersect_check(s1, s2): | ||
(p1, p2), (p3, p4) = s1, s2; (x1, y1), (x2, y2), (x3, y3), (x4, y4) = p1, p2, p3, p4 | ||
c1 = (x2-x1)*(y3-y1)-(y2-y1)*(x3-x1); c2 = (x2-x1)*(y4-y1)-(y2-y1)*(x4-x1) | ||
if (c1 < 0 and c2 < 0) or (c1 > 0 and c2 > 0): return 0 | ||
c1 = (x4-x3)*(y1-y3)-(y4-y3)*(x1-x3); c2 = (x4-x3)*(y2-y3)-(y4-y3)*(x2-x3) | ||
if (c1 < 0 and c2 < 0) or (c1 > 0 and c2 > 0): return 0 | ||
return 1 | ||
|
||
def pip(p): | ||
ray = (p, (p[0]+10**5, p[1]+1)); return sum(intersect_check(ray, (P[i], P[i+1])) for i in range(n))%2 | ||
|
||
while (n:=int(input())): | ||
TC += 1; P = [[*map(int, input().split())] for _ in range(n)]; P.append(P[0]); Q = int(input()); print('Case', TC) | ||
for _ in range(Q): | ||
x, y = map(int, input().split()); hit = pip(p:=(x, y)); best = 1e18 | ||
for i in range(n): | ||
x1, y1 = P[i]; x2, y2 = P[i+1]; A = y2-y1; B = x1-x2; r = (y-y1)*A-(x-x1)*B; d = A*A+B*B | ||
if r < 0: best = min(best, d2(p, P[i])) | ||
elif r > d: best = min(best, d2(p, P[i+1])) | ||
else: best = min(best, (A*(x-x1)+B*(y-y1))**2/d) | ||
if best == 0: print('Winged!') | ||
elif hit: print('Hit!', best**.5) | ||
else: print('Miss!', best**.5) |
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,19 @@ | ||
import sys; input = sys.stdin.readline; from array import * | ||
|
||
def dp(i, p, s): | ||
if s > 100: return 0 | ||
if i == N: return s == 100 | ||
if D[(x:=10201*i+101*p+s)] != -1: return D[x] | ||
D[x] = 0; lo, hi = divmod(S[i], 101) | ||
for v in range(lo, min(hi, p)+1): | ||
if dp(i+1, v, s+v): D[x] = 1; M[i] = 101*min(M[i]//101, v)+max(M[i]%101, v) | ||
return D[x] | ||
|
||
S = []; M = []; E = []; N = int(input()); D = array('b', [-1]*N*101*101) | ||
for i in range(N): | ||
s, p = input().strip().split(); E.append(s) | ||
if p != '?': S.append(102*int(p)) | ||
else: S.append(100) | ||
M.append(10100) | ||
dp(0, 100, 0) | ||
for i in range(N): print(E[i], *divmod(M[i], 101)) |
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,22 @@ | ||
N = int(input()); W = [input().strip() for _ in range(N)]; K = len(W[0]); R = {W[i]:i for i in range(N)}; G = [[] for _ in range(N)] | ||
for i in range(N): | ||
for j in range(K): | ||
for k in range(65, 91): | ||
if chr(k) != W[i][j]: | ||
s = W[i][:j]+chr(k)+W[i][j+1:] | ||
if s not in R: R[s] = len(R); W.append(s); G.append([]) | ||
G[R[W[i]]].append(R[s]); G[R[s]].append(R[W[i]]) | ||
D = [-1]*len(G); E = [-1]*len(G); Q = [0]; D[0] = 0; R = [1]; E[1] = 0 | ||
for u in Q: | ||
for v in G[u]: | ||
if D[v] == -1: D[v] = D[u]+1; Q.append(v) if v < N else 0 | ||
for u in R: | ||
for v in G[u]: | ||
if E[v] == -1: E[v] = E[u]+1; R.append(v) if v < N else 0 | ||
ans = None; dist = D[1] if D[1] > -1 else 2000 | ||
for i in range(N, len(G)): | ||
if D[i] > -1 < E[i]: | ||
if D[i]+E[i] < dist: ans = W[i]; dist = D[i]+E[i] | ||
elif D[i]+E[i] == dist and ans != None: ans = min(W[i], ans) | ||
if ans == None: print(0, dist if dist < 2000 else -1) | ||
else: print(ans, dist) |