Submission #124784

# Submission time Handle Problem Language Result Execution time Memory
124784 2019-07-04T01:59:12 Z model_code Political Development (BOI17_politicaldevelopment) Python 3
39 / 100
3000 ms 32956 KB
#!/usr/bin/env python3
import sys
from collections import deque
from itertools import chain, combinations

def getdegeneracyordering(graph, n, k):
    # O(kn)
    active = [True] * n
    nsize = [len(graph[i]) for i in range(n)]
    
    tinydegs = [[] for i in range(k)]
    tinydegscount = 0
    for i in range(n):
        if nsize[i] < k:
            tinydegs[nsize[i]].append(i)
            tinydegscount += 1

    degorder = [-1] * n
    degorder_i = []
    mindeg = min(nsize)
    while tinydegscount > 0:
        # print(tinydegscount, mindeg, tinydegs)
        while len(tinydegs[mindeg]) == 0:
            mindeg += 1
        node = tinydegs[mindeg].pop()
        if not active[node]: continue
        
        ## This is node to put in ordering!
        degorder[node] = len(degorder_i)
        degorder_i.append(node)
        active[node] = False
        tinydegscount -= 1
        
        # Update state for neighbours
        for nb in graph[node]:
            if not active[nb]: continue
            nsize[nb] -= 1
            if nsize[nb] < k:
                tinydegs[nsize[nb]].append(nb)
                mindeg = min(mindeg, nsize[nb])
                if nsize[nb] == k-1:
                    tinydegscount += 1
                    
    return degorder, degorder_i
    
def powerset(s):
    return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))    

def getmaxclique(node, graph):
    # returns largest clique of graph (active part) which contains node.
    # O(2^k * k^2 + k^2) (assuming adjecency lists < k)
    nbhood = []
    rev = {}
    for i, u in enumerate(graph[node]):
        rev[u] = i
        nbhood.append(u)
            
    # Make adjacency matrix
    adjmatrix = [[0] * len(nbhood) for i in range(len(nbhood))]
    for u in rev:
        for v in graph[u]:
            if v in rev:
                adjmatrix[rev[u]][rev[v]] = adjmatrix[rev[v]][rev[u]] = 1
    
    # Try all subsets, check if it is a clique.
    def isclique(myset):
        for i, u in enumerate(myset):
            for j in range(i+1, len(myset)):
                if adjmatrix[u][myset[j]] == 0:
                    return False
        return True
        
    maxclique = 0
    for myset in powerset(list(range(len(nbhood)))):
        if isclique(myset):
            maxclique = max(maxclique, len(myset))
            
    return maxclique + 1
    

def poldev():
    # Step 0: Read input
    n, k = map(int, sys.stdin.readline().split())
    graph = []
    for i in range(n):
        graph.append(list(map(int, sys.stdin.readline().split()[1:])))
    
    # Step 1: Find degeneracy order O(kn)
    degorder, degorder_i = getdegeneracyordering(graph, n, k)
    
    # Step 2: Trim graph, such that edges only point forwards in degen. order
    # Adjecency list of each node is now < k
    for node in range(n):
        futureneighs = []
        for nb in graph[node]:
            if degorder[nb] > degorder[node]:
                futureneighs.append(nb)
        assert(len(futureneighs) < k)
        graph[node] = futureneighs
    
    # Step 3: Swipe through in degeneracy ordering: What is largest clique
    # of vertex i, using only future vertices? O(n * 2^k * k^2)
    maxclique = 1
    for node in degorder_i:
        maxclique = max(maxclique, getmaxclique(node, graph))
    
    return maxclique
    
    
print(poldev())
# Verdict Execution time Memory Grader output
1 Correct 30 ms 3684 KB Output is correct
2 Correct 30 ms 3812 KB Output is correct
3 Correct 100 ms 4844 KB Output is correct
4 Correct 94 ms 4964 KB Output is correct
5 Correct 95 ms 4968 KB Output is correct
6 Correct 97 ms 5016 KB Output is correct
7 Correct 97 ms 5172 KB Output is correct
8 Correct 73 ms 4736 KB Output is correct
9 Correct 30 ms 3680 KB Output is correct
10 Correct 73 ms 4724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 3684 KB Output is correct
2 Correct 30 ms 3812 KB Output is correct
3 Correct 100 ms 4844 KB Output is correct
4 Correct 94 ms 4964 KB Output is correct
5 Correct 95 ms 4968 KB Output is correct
6 Correct 97 ms 5016 KB Output is correct
7 Correct 97 ms 5172 KB Output is correct
8 Correct 73 ms 4736 KB Output is correct
9 Correct 30 ms 3680 KB Output is correct
10 Correct 73 ms 4724 KB Output is correct
11 Correct 96 ms 5096 KB Output is correct
12 Correct 99 ms 5104 KB Output is correct
13 Correct 30 ms 3656 KB Output is correct
14 Correct 100 ms 5108 KB Output is correct
15 Correct 30 ms 3620 KB Output is correct
16 Correct 97 ms 4972 KB Output is correct
17 Correct 30 ms 3640 KB Output is correct
18 Correct 97 ms 5052 KB Output is correct
19 Correct 74 ms 4720 KB Output is correct
20 Correct 90 ms 4832 KB Output is correct
21 Correct 90 ms 4836 KB Output is correct
22 Correct 73 ms 4704 KB Output is correct
23 Correct 109 ms 5004 KB Output is correct
24 Correct 73 ms 4696 KB Output is correct
25 Correct 108 ms 5112 KB Output is correct
26 Correct 105 ms 4960 KB Output is correct
27 Correct 100 ms 5008 KB Output is correct
28 Correct 107 ms 5080 KB Output is correct
29 Correct 103 ms 5076 KB Output is correct
30 Correct 110 ms 5112 KB Output is correct
31 Correct 111 ms 5092 KB Output is correct
32 Correct 108 ms 5060 KB Output is correct
33 Correct 111 ms 5160 KB Output is correct
34 Correct 111 ms 5232 KB Output is correct
35 Correct 70 ms 4356 KB Output is correct
36 Correct 72 ms 4336 KB Output is correct
37 Correct 71 ms 4368 KB Output is correct
38 Correct 53 ms 4068 KB Output is correct
39 Correct 52 ms 3992 KB Output is correct
40 Correct 129 ms 5332 KB Output is correct
41 Correct 52 ms 4012 KB Output is correct
42 Correct 129 ms 5356 KB Output is correct
43 Correct 127 ms 5308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 74 ms 4684 KB Output is correct
2 Correct 30 ms 3760 KB Output is correct
3 Correct 31 ms 3684 KB Output is correct
4 Correct 30 ms 3676 KB Output is correct
5 Correct 32 ms 3704 KB Output is correct
6 Correct 30 ms 3684 KB Output is correct
7 Correct 30 ms 3604 KB Output is correct
8 Correct 30 ms 3668 KB Output is correct
9 Correct 30 ms 3684 KB Output is correct
10 Correct 30 ms 3656 KB Output is correct
11 Execution timed out 3039 ms 32956 KB Time limit exceeded
12 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 30 ms 3684 KB Output is correct
2 Correct 30 ms 3812 KB Output is correct
3 Correct 100 ms 4844 KB Output is correct
4 Correct 94 ms 4964 KB Output is correct
5 Correct 95 ms 4968 KB Output is correct
6 Correct 97 ms 5016 KB Output is correct
7 Correct 97 ms 5172 KB Output is correct
8 Correct 73 ms 4736 KB Output is correct
9 Correct 30 ms 3680 KB Output is correct
10 Correct 73 ms 4724 KB Output is correct
11 Correct 96 ms 5096 KB Output is correct
12 Correct 99 ms 5104 KB Output is correct
13 Correct 30 ms 3656 KB Output is correct
14 Correct 100 ms 5108 KB Output is correct
15 Correct 30 ms 3620 KB Output is correct
16 Correct 97 ms 4972 KB Output is correct
17 Correct 30 ms 3640 KB Output is correct
18 Correct 97 ms 5052 KB Output is correct
19 Correct 74 ms 4720 KB Output is correct
20 Correct 90 ms 4832 KB Output is correct
21 Correct 90 ms 4836 KB Output is correct
22 Correct 73 ms 4704 KB Output is correct
23 Correct 109 ms 5004 KB Output is correct
24 Correct 73 ms 4696 KB Output is correct
25 Correct 108 ms 5112 KB Output is correct
26 Correct 105 ms 4960 KB Output is correct
27 Correct 100 ms 5008 KB Output is correct
28 Correct 107 ms 5080 KB Output is correct
29 Correct 103 ms 5076 KB Output is correct
30 Correct 110 ms 5112 KB Output is correct
31 Correct 111 ms 5092 KB Output is correct
32 Correct 108 ms 5060 KB Output is correct
33 Correct 111 ms 5160 KB Output is correct
34 Correct 111 ms 5232 KB Output is correct
35 Correct 70 ms 4356 KB Output is correct
36 Correct 72 ms 4336 KB Output is correct
37 Correct 71 ms 4368 KB Output is correct
38 Correct 53 ms 4068 KB Output is correct
39 Correct 52 ms 3992 KB Output is correct
40 Correct 129 ms 5332 KB Output is correct
41 Correct 52 ms 4012 KB Output is correct
42 Correct 129 ms 5356 KB Output is correct
43 Correct 127 ms 5308 KB Output is correct
44 Correct 1129 ms 6356 KB Output is correct
45 Correct 30 ms 3688 KB Output is correct
46 Correct 131 ms 5328 KB Output is correct
47 Correct 244 ms 6212 KB Output is correct
48 Correct 134 ms 5344 KB Output is correct
49 Correct 244 ms 6196 KB Output is correct
50 Correct 255 ms 6352 KB Output is correct
51 Correct 1787 ms 7640 KB Output is correct
52 Correct 99 ms 5220 KB Output is correct
53 Execution timed out 3037 ms 8152 KB Time limit exceeded
54 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 30 ms 3684 KB Output is correct
2 Correct 30 ms 3812 KB Output is correct
3 Correct 100 ms 4844 KB Output is correct
4 Correct 94 ms 4964 KB Output is correct
5 Correct 95 ms 4968 KB Output is correct
6 Correct 97 ms 5016 KB Output is correct
7 Correct 97 ms 5172 KB Output is correct
8 Correct 73 ms 4736 KB Output is correct
9 Correct 30 ms 3680 KB Output is correct
10 Correct 73 ms 4724 KB Output is correct
11 Correct 96 ms 5096 KB Output is correct
12 Correct 99 ms 5104 KB Output is correct
13 Correct 30 ms 3656 KB Output is correct
14 Correct 100 ms 5108 KB Output is correct
15 Correct 30 ms 3620 KB Output is correct
16 Correct 97 ms 4972 KB Output is correct
17 Correct 30 ms 3640 KB Output is correct
18 Correct 97 ms 5052 KB Output is correct
19 Correct 74 ms 4720 KB Output is correct
20 Correct 90 ms 4832 KB Output is correct
21 Correct 90 ms 4836 KB Output is correct
22 Correct 73 ms 4704 KB Output is correct
23 Correct 109 ms 5004 KB Output is correct
24 Correct 73 ms 4696 KB Output is correct
25 Correct 108 ms 5112 KB Output is correct
26 Correct 105 ms 4960 KB Output is correct
27 Correct 100 ms 5008 KB Output is correct
28 Correct 107 ms 5080 KB Output is correct
29 Correct 103 ms 5076 KB Output is correct
30 Correct 110 ms 5112 KB Output is correct
31 Correct 111 ms 5092 KB Output is correct
32 Correct 108 ms 5060 KB Output is correct
33 Correct 111 ms 5160 KB Output is correct
34 Correct 111 ms 5232 KB Output is correct
35 Correct 70 ms 4356 KB Output is correct
36 Correct 72 ms 4336 KB Output is correct
37 Correct 71 ms 4368 KB Output is correct
38 Correct 53 ms 4068 KB Output is correct
39 Correct 52 ms 3992 KB Output is correct
40 Correct 129 ms 5332 KB Output is correct
41 Correct 52 ms 4012 KB Output is correct
42 Correct 129 ms 5356 KB Output is correct
43 Correct 127 ms 5308 KB Output is correct
44 Correct 29 ms 3644 KB Output is correct
45 Correct 1511 ms 24180 KB Output is correct
46 Correct 973 ms 19704 KB Output is correct
47 Correct 1602 ms 25056 KB Output is correct
48 Correct 1544 ms 24188 KB Output is correct
49 Correct 701 ms 17760 KB Output is correct
50 Correct 1988 ms 29900 KB Output is correct
51 Correct 1623 ms 24704 KB Output is correct
52 Correct 719 ms 17324 KB Output is correct
53 Correct 708 ms 17904 KB Output is correct
54 Correct 483 ms 13592 KB Output is correct
55 Correct 1984 ms 30120 KB Output is correct
56 Correct 651 ms 15060 KB Output is correct
57 Correct 711 ms 17336 KB Output is correct
58 Correct 1110 ms 19236 KB Output is correct
59 Correct 657 ms 14932 KB Output is correct
60 Correct 649 ms 14968 KB Output is correct
61 Correct 1102 ms 19020 KB Output is correct
62 Correct 842 ms 17116 KB Output is correct
63 Correct 1041 ms 19948 KB Output is correct
64 Correct 652 ms 15020 KB Output is correct
65 Correct 1327 ms 22320 KB Output is correct
66 Correct 845 ms 17148 KB Output is correct
67 Correct 1058 ms 19940 KB Output is correct
68 Correct 1226 ms 22072 KB Output is correct
69 Correct 1291 ms 22156 KB Output is correct
70 Correct 794 ms 17800 KB Output is correct
71 Correct 1214 ms 21944 KB Output is correct
72 Correct 992 ms 20236 KB Output is correct
73 Correct 1403 ms 23408 KB Output is correct
74 Correct 783 ms 17672 KB Output is correct
75 Correct 552 ms 12392 KB Output is correct
76 Correct 1002 ms 20268 KB Output is correct
77 Correct 1399 ms 23516 KB Output is correct
78 Correct 686 ms 13664 KB Output is correct
79 Correct 563 ms 12304 KB Output is correct
80 Correct 263 ms 8116 KB Output is correct
81 Correct 696 ms 13568 KB Output is correct
82 Correct 1153 ms 21976 KB Output is correct
83 Correct 272 ms 8148 KB Output is correct
84 Correct 1143 ms 21972 KB Output is correct