Submission #398209

# Submission time Handle Problem Language Result Execution time Memory
398209 2021-05-03T23:58:10 Z mars Political Development (BOI17_politicaldevelopment) C++14
77 / 100
3000 ms 7716 KB
#include<iostream>
#include<vector>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<bitset>

using namespace std;

int n,k;
vector<int> degree;
vector<vector<int> > G;

vector<bool> done;
vector<int> current;

vector<int> degOrdering;
vector<int> degOrderingPos;


//for debugging;
vector<int> maxCliqueFound;


void readInput() {
	scanf("%d%d", &n, &k);
	degree = vector<int> (n,0);
	for(int i = 0; i < n; ++i) {
		scanf("%d", &(degree[i]));
		G.push_back(vector<int> (degree[i], 0));
		for(int j = 0; j < degree[i]; ++j) {scanf("%d", &(G[i][j]));}
	}
	done = vector<bool> (n, false);
	current = vector<int> (n, -1);
}

void makeDegeneracyOrdering() {
		queue<int> Q;
		vector<int> reducedDegree(degree);
		
		for(int i = 0; i < n; ++i) {if(reducedDegree[i] <= k) Q.push(i);}
		
		while(!Q.empty()) {
			int v = Q.front(); Q.pop();
			degOrdering.push_back(v);
			for(int i = 0; i < G[v].size(); ++i) {
				int u = G[v][i];
				reducedDegree[u]--;
				if(reducedDegree[u] == k) Q.push(u);
		}}
		
		degOrderingPos = vector<int> (n,0);
		for(int i = 0; i < n; ++i) {degOrderingPos[degOrdering[i]] = i;}
}
		
int maxCliqueSize(int pos) {
	// filter out active vertices
	vector<int> zactive(G[pos]);
	zactive.push_back(pos);
		
	vector<int> active;
	for(int i = 0; i < zactive.size(); ++i) {if(!done[zactive[i]]) active.push_back(zactive[i]);}
	int t = active.size();
	
	// set current
	for(int i = 0; i < t; ++i) current[active[i]] = i;
		
	//cerr << "num active vertices " << t << endl;
	
	// make adjacency matrix as bitmask
	vector<int> adj(t, 0);
	for(int i = 0; i < t; ++i) {
		adj[i] |= 1 << i;
		
		int u = active[i];
		for(int j = 0; j < G[u].size(); ++j) {
				int v = G[u][j];
				adj[i] |= current[v] != -1 ? 1 << current[v] : 0;
		}
	}	
	
	int ans = 0;
	int N = 1 << t;
	for(int s = 0; s < N; ++s) {
		// check if s is a clique:
		int isSet = s;
		for(int p = 0; p < t; ++p) {if(s & (1 << p)) isSet &= adj[p];}
		if(isSet != s) continue;
		
		int card = bitset<12>(s).count();
		ans = max(ans, card);
		
		// keep largest clique for debugginf
		if(card > maxCliqueFound.size()) {
			maxCliqueFound.clear();
			for(int i = 0; i < t; ++i) {if(s & (1 << i)) maxCliqueFound.push_back(active[i]);}
		}
	}
	
	// clear current
	for(int i = 0; i < t; ++i) current[active[i]] = -1;
	
	return ans;
}

// ensure that the max clique found is actually a clique.
void assertClique() {
	for(int i = 0; i < maxCliqueFound.size(); ++i) {
		for(int j = 0; j < maxCliqueFound.size(); ++j) {
			if(i==j) continue;
			bool found = false;
			for(int p = 0; p < G[maxCliqueFound[j]].size(); ++p) {
				found |= G[maxCliqueFound[j]][p] == maxCliqueFound[i];
			}
			if(!found) {
				cerr << "WTF" << endl;
}}}}


int main() {
	readInput();
	makeDegeneracyOrdering();
	
	int best = 0;
	for(int i = 0; i < n; ++i) {
		int v = degOrdering[i];
		int c = maxCliqueSize(v); 
		best = max(best, c);
		done[v] = true;		
	}
	
	assertClique();
	
	printf("%d\n", best);
	return 0;
}

Compilation message

politicaldevelopment.cpp: In function 'void makeDegeneracyOrdering()':
politicaldevelopment.cpp:46:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |    for(int i = 0; i < G[v].size(); ++i) {
      |                   ~~^~~~~~~~~~~~~
politicaldevelopment.cpp: In function 'int maxCliqueSize(int)':
politicaldevelopment.cpp:62:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   62 |  for(int i = 0; i < zactive.size(); ++i) {if(!done[zactive[i]]) active.push_back(zactive[i]);}
      |                 ~~^~~~~~~~~~~~~~~~
politicaldevelopment.cpp:76:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   76 |   for(int j = 0; j < G[u].size(); ++j) {
      |                  ~~^~~~~~~~~~~~~
politicaldevelopment.cpp:94:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   94 |   if(card > maxCliqueFound.size()) {
      |      ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
politicaldevelopment.cpp: In function 'void assertClique()':
politicaldevelopment.cpp:108:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  108 |  for(int i = 0; i < maxCliqueFound.size(); ++i) {
      |                 ~~^~~~~~~~~~~~~~~~~~~~~~~
politicaldevelopment.cpp:109:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  109 |   for(int j = 0; j < maxCliqueFound.size(); ++j) {
      |                  ~~^~~~~~~~~~~~~~~~~~~~~~~
politicaldevelopment.cpp:112:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  112 |    for(int p = 0; p < G[maxCliqueFound[j]].size(); ++p) {
      |                   ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
politicaldevelopment.cpp: In function 'void readInput()':
politicaldevelopment.cpp:26:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   26 |  scanf("%d%d", &n, &k);
      |  ~~~~~^~~~~~~~~~~~~~~~
politicaldevelopment.cpp:29:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   29 |   scanf("%d", &(degree[i]));
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~
politicaldevelopment.cpp:31:44: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   31 |   for(int j = 0; j < degree[i]; ++j) {scanf("%d", &(G[i][j]));}
      |                                       ~~~~~^~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 4 ms 716 KB Output is correct
4 Correct 37 ms 716 KB Output is correct
5 Correct 38 ms 792 KB Output is correct
6 Correct 5 ms 716 KB Output is correct
7 Correct 5 ms 696 KB Output is correct
8 Correct 2 ms 588 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 2 ms 588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 4 ms 716 KB Output is correct
4 Correct 37 ms 716 KB Output is correct
5 Correct 38 ms 792 KB Output is correct
6 Correct 5 ms 716 KB Output is correct
7 Correct 5 ms 696 KB Output is correct
8 Correct 2 ms 588 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 2 ms 588 KB Output is correct
11 Correct 38 ms 792 KB Output is correct
12 Correct 38 ms 776 KB Output is correct
13 Correct 1 ms 208 KB Output is correct
14 Correct 37 ms 796 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 5 ms 716 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 5 ms 728 KB Output is correct
19 Correct 2 ms 588 KB Output is correct
20 Correct 3 ms 648 KB Output is correct
21 Correct 4 ms 716 KB Output is correct
22 Correct 2 ms 588 KB Output is correct
23 Correct 5 ms 696 KB Output is correct
24 Correct 2 ms 588 KB Output is correct
25 Correct 5 ms 720 KB Output is correct
26 Correct 5 ms 684 KB Output is correct
27 Correct 4 ms 716 KB Output is correct
28 Correct 5 ms 720 KB Output is correct
29 Correct 4 ms 716 KB Output is correct
30 Correct 13 ms 720 KB Output is correct
31 Correct 6 ms 716 KB Output is correct
32 Correct 15 ms 696 KB Output is correct
33 Correct 5 ms 696 KB Output is correct
34 Correct 5 ms 716 KB Output is correct
35 Correct 3 ms 428 KB Output is correct
36 Correct 3 ms 460 KB Output is correct
37 Correct 3 ms 460 KB Output is correct
38 Correct 2 ms 332 KB Output is correct
39 Correct 2 ms 332 KB Output is correct
40 Correct 6 ms 716 KB Output is correct
41 Correct 2 ms 332 KB Output is correct
42 Correct 6 ms 716 KB Output is correct
43 Correct 6 ms 716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 588 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 444 ms 7672 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 436 ms 7616 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 442 ms 7716 KB Output is correct
16 Correct 444 ms 7672 KB Output is correct
17 Correct 442 ms 7672 KB Output is correct
18 Correct 447 ms 7672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 4 ms 716 KB Output is correct
4 Correct 37 ms 716 KB Output is correct
5 Correct 38 ms 792 KB Output is correct
6 Correct 5 ms 716 KB Output is correct
7 Correct 5 ms 696 KB Output is correct
8 Correct 2 ms 588 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 2 ms 588 KB Output is correct
11 Correct 38 ms 792 KB Output is correct
12 Correct 38 ms 776 KB Output is correct
13 Correct 1 ms 208 KB Output is correct
14 Correct 37 ms 796 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 5 ms 716 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 5 ms 728 KB Output is correct
19 Correct 2 ms 588 KB Output is correct
20 Correct 3 ms 648 KB Output is correct
21 Correct 4 ms 716 KB Output is correct
22 Correct 2 ms 588 KB Output is correct
23 Correct 5 ms 696 KB Output is correct
24 Correct 2 ms 588 KB Output is correct
25 Correct 5 ms 720 KB Output is correct
26 Correct 5 ms 684 KB Output is correct
27 Correct 4 ms 716 KB Output is correct
28 Correct 5 ms 720 KB Output is correct
29 Correct 4 ms 716 KB Output is correct
30 Correct 13 ms 720 KB Output is correct
31 Correct 6 ms 716 KB Output is correct
32 Correct 15 ms 696 KB Output is correct
33 Correct 5 ms 696 KB Output is correct
34 Correct 5 ms 716 KB Output is correct
35 Correct 3 ms 428 KB Output is correct
36 Correct 3 ms 460 KB Output is correct
37 Correct 3 ms 460 KB Output is correct
38 Correct 2 ms 332 KB Output is correct
39 Correct 2 ms 332 KB Output is correct
40 Correct 6 ms 716 KB Output is correct
41 Correct 2 ms 332 KB Output is correct
42 Correct 6 ms 716 KB Output is correct
43 Correct 6 ms 716 KB Output is correct
44 Correct 33 ms 972 KB Output is correct
45 Correct 1 ms 204 KB Output is correct
46 Correct 8 ms 716 KB Output is correct
47 Correct 12 ms 972 KB Output is correct
48 Correct 6 ms 716 KB Output is correct
49 Correct 12 ms 972 KB Output is correct
50 Correct 17 ms 1104 KB Output is correct
51 Correct 116 ms 1228 KB Output is correct
52 Correct 38 ms 792 KB Output is correct
53 Correct 497 ms 1384 KB Output is correct
54 Correct 275 ms 1356 KB Output is correct
55 Correct 5 ms 716 KB Output is correct
56 Correct 39 ms 788 KB Output is correct
57 Correct 2 ms 564 KB Output is correct
58 Correct 528 ms 1396 KB Output is correct
59 Correct 6 ms 716 KB Output is correct
60 Correct 5 ms 716 KB Output is correct
61 Correct 6 ms 716 KB Output is correct
62 Correct 6 ms 824 KB Output is correct
63 Correct 26 ms 972 KB Output is correct
64 Correct 17 ms 928 KB Output is correct
65 Correct 6 ms 720 KB Output is correct
66 Correct 6 ms 716 KB Output is correct
67 Correct 50 ms 972 KB Output is correct
68 Correct 16 ms 972 KB Output is correct
69 Correct 6 ms 720 KB Output is correct
70 Correct 16 ms 848 KB Output is correct
71 Correct 48 ms 980 KB Output is correct
72 Correct 45 ms 976 KB Output is correct
73 Correct 4 ms 716 KB Output is correct
74 Correct 17 ms 848 KB Output is correct
75 Correct 48 ms 976 KB Output is correct
76 Correct 16 ms 844 KB Output is correct
77 Correct 88 ms 1100 KB Output is correct
78 Correct 4 ms 716 KB Output is correct
79 Correct 44 ms 588 KB Output is correct
80 Correct 20 ms 844 KB Output is correct
81 Correct 88 ms 1104 KB Output is correct
82 Correct 2 ms 332 KB Output is correct
83 Correct 46 ms 688 KB Output is correct
84 Correct 43 ms 972 KB Output is correct
85 Correct 2 ms 332 KB Output is correct
86 Correct 42 ms 892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 4 ms 716 KB Output is correct
4 Correct 37 ms 716 KB Output is correct
5 Correct 38 ms 792 KB Output is correct
6 Correct 5 ms 716 KB Output is correct
7 Correct 5 ms 696 KB Output is correct
8 Correct 2 ms 588 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 2 ms 588 KB Output is correct
11 Correct 38 ms 792 KB Output is correct
12 Correct 38 ms 776 KB Output is correct
13 Correct 1 ms 208 KB Output is correct
14 Correct 37 ms 796 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 5 ms 716 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 5 ms 728 KB Output is correct
19 Correct 2 ms 588 KB Output is correct
20 Correct 3 ms 648 KB Output is correct
21 Correct 4 ms 716 KB Output is correct
22 Correct 2 ms 588 KB Output is correct
23 Correct 5 ms 696 KB Output is correct
24 Correct 2 ms 588 KB Output is correct
25 Correct 5 ms 720 KB Output is correct
26 Correct 5 ms 684 KB Output is correct
27 Correct 4 ms 716 KB Output is correct
28 Correct 5 ms 720 KB Output is correct
29 Correct 4 ms 716 KB Output is correct
30 Correct 13 ms 720 KB Output is correct
31 Correct 6 ms 716 KB Output is correct
32 Correct 15 ms 696 KB Output is correct
33 Correct 5 ms 696 KB Output is correct
34 Correct 5 ms 716 KB Output is correct
35 Correct 3 ms 428 KB Output is correct
36 Correct 3 ms 460 KB Output is correct
37 Correct 3 ms 460 KB Output is correct
38 Correct 2 ms 332 KB Output is correct
39 Correct 2 ms 332 KB Output is correct
40 Correct 6 ms 716 KB Output is correct
41 Correct 2 ms 332 KB Output is correct
42 Correct 6 ms 716 KB Output is correct
43 Correct 6 ms 716 KB Output is correct
44 Correct 1 ms 296 KB Output is correct
45 Correct 87 ms 6316 KB Output is correct
46 Correct 1070 ms 5228 KB Output is correct
47 Correct 91 ms 6392 KB Output is correct
48 Correct 90 ms 6228 KB Output is correct
49 Execution timed out 3074 ms 5072 KB Time limit exceeded
50 Halted 0 ms 0 KB -