Submission #19812154


Source Code Expand

Copy
#!/usr/bin/env python3
# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools (tips: You use the default template now. You can remove this line by using your custom template)
def main():
N,M = map(int, input().split())
edges = [[] for _ in range(N)]
for _ in range(M):
a,b = map(int,input().split())
a-=1
b-=1
edges[a].append(b)
edges[b].append(a)
K = int(input())
cs = [int(s)-1 for s in input().split()]
dist = []
for i in range(K):
diff = [-1 for _ in range(N)]
diff[cs[i]] = 0
queue = [cs[i]]
ind = 0
 
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#!/usr/bin/env python3


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    N,M = map(int, input().split())
    edges = [[] for _ in range(N)]
    for _ in range(M):
        a,b = map(int,input().split())
        a-=1
        b-=1
        edges[a].append(b)
        edges[b].append(a)
    K = int(input())
    cs = [int(s)-1 for s in input().split()]
    dist = []
    for i in range(K):
        diff = [-1 for _ in range(N)]
        diff[cs[i]] = 0
        queue = [cs[i]]
        ind = 0
        while  ind<len(queue):
            v = queue[ind]
            ind += 1
            for v2 in edges[v]:
                #print(f"v={v}, v2={v2}")
                if diff[v2]==-1:
                    diff[v2] = diff[v]+1
                    queue.append(v2)
        # print(cs[i], diff)
        dist.append( [diff[cs[j]] for j in range(K)])

    # print(dist)
    for s in dist:
        if -1 in s:
            print(-1)
            return
    inf = 10**20
    dp = [ [inf for bin in range(2**K)] for t in range(K)]
    for t in range(K):
        dp[t][1<<t] = 1
    for bin in range(2**K):
        for t1 in range(K):
            for t2 in range(K):
                dp[t2][(1<<t2) | bin]  = min( dp[t2][(1<<t2) | bin], dp[t1][bin]+dist[t1][t2] )
    ret = min(dp[t][2**K-1] for t in range(K))
    print(ret)
    # Failed to predict input format
    pass

if __name__ == '__main__':
    main()

Submission Info

Submission Time
Task E - Magical Ornament
User hirosegolf
Language Python (3.8.2)
Score 0
Code Size 1523 Byte
Status TLE
Exec Time 2207 ms
Memory 51268 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 500
Status
AC × 3
AC × 23
TLE × 17
Set Name Test Cases
Sample 01_sample.txt, 02_sample.txt, 03_sample.txt
All 01_sample.txt, 02_sample.txt, 03_sample.txt, 04_hand.txt, 05_hand.txt, 06_small.txt, 07_small.txt, 08_small.txt, 09_small.txt, 10_small.txt, 11_small.txt, 12_small.txt, 13_small.txt, 14_small.txt, 15_small.txt, 16_small.txt, 17_small.txt, 18_small.txt, 19_small.txt, 20_small.txt, 21_large.txt, 22_large.txt, 23_large.txt, 24_large.txt, 25_large.txt, 26_max.txt, 27_max.txt, 28_max.txt, 29_max.txt, 30_max.txt, 31_max.txt, 32_tree.txt, 33_tree.txt, 34_tree.txt, 35_path.txt, 36_path.txt, 37_path.txt, 38_star.txt, 39_star.txt, 40_star.txt
Case Name Status Exec Time Memory
01_sample.txt AC 19 ms 9200 KB
02_sample.txt AC 20 ms 9036 KB
03_sample.txt AC 26 ms 9024 KB
04_hand.txt AC 22 ms 9196 KB
05_hand.txt AC 42 ms 16844 KB
06_small.txt AC 18 ms 9032 KB
07_small.txt AC 23 ms 9036 KB
08_small.txt AC 23 ms 9264 KB
09_small.txt AC 18 ms 9192 KB
10_small.txt AC 30 ms 9036 KB
11_small.txt AC 21 ms 9320 KB
12_small.txt AC 19 ms 9256 KB
13_small.txt AC 23 ms 9320 KB
14_small.txt AC 22 ms 9036 KB
15_small.txt AC 21 ms 9264 KB
16_small.txt AC 20 ms 9188 KB
17_small.txt AC 24 ms 9324 KB
18_small.txt AC 21 ms 9032 KB
19_small.txt AC 20 ms 9028 KB
20_small.txt AC 19 ms 9136 KB
21_large.txt AC 57 ms 11736 KB
22_large.txt TLE 2206 ms 30204 KB
23_large.txt TLE 2206 ms 13512 KB
24_large.txt AC 1679 ms 17580 KB
25_large.txt AC 1675 ms 17320 KB
26_max.txt TLE 2207 ms 49348 KB
27_max.txt TLE 2207 ms 47812 KB
28_max.txt TLE 2207 ms 47096 KB
29_max.txt TLE 2207 ms 44488 KB
30_max.txt TLE 2207 ms 39824 KB
31_max.txt TLE 2207 ms 36196 KB
32_tree.txt TLE 2207 ms 50428 KB
33_tree.txt TLE 2207 ms 50300 KB
34_tree.txt TLE 2207 ms 49744 KB
35_path.txt TLE 2207 ms 50728 KB
36_path.txt TLE 2207 ms 51188 KB
37_path.txt TLE 2207 ms 51268 KB
38_star.txt TLE 2207 ms 46288 KB
39_star.txt TLE 2207 ms 46784 KB
40_star.txt TLE 2207 ms 47048 KB


2025-04-08 (Tue)
09:18:06 +00:00