-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimunitate.cpp
188 lines (131 loc) · 2.8 KB
/
imunitate.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include<cstdio>
#include<iostream>
#include<string>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<sstream>
#include<limits.h>
#include<list>
#include<queue>
#include<bitset>
#include<map>
//#include<unordered_map>
using namespace std;
typedef pair<int, int> ii;
typedef long long int64;
#define Max 3100
#define INF 2000000000
#define TRvi(c, it) \
for(vector<int>::iterator it = c.begin(); it != c.end(); it++)
int V, E;
int dp[1 << 18];
vector<vector<int> > AdjList;
int elim[1 << 18][18];
//vector<int> curentDegree;
int getBit(int n, int position)
{
return (n >> position) & 1;
}
void optimize()
{
for(int i = 0; i < V; i++)
elim[0][i] = 0;
for(int i = 1; i < (1 << V); i++)
{
int politics(int bitmask) /* complexity is ~ O(N * 2^N) */
{
//for(int
if(dp[bitmask] != -1)
return dp[bitmask];
if(bitmask == 0)
return dp[bitmask] = 0;
else if((bitmask & (bitmask-1)) == 0) /* bitmask is power of 2 */
return dp[bitmask] = 1;
vector<int> curentDegree(V);
for(int i = 0; i < V; i++)
{
curentDegree[i] = AdjList[i].size();
}
int setNumber = 0;
int influence;
for(int position = 0; position < V; position++)
{
if(getBit(bitmask, position) == 1)
{
setNumber++;
}
else /* politician was removed */
{
TRvi(AdjList[position], v)
{
influence = *v;
curentDegree[influence]--;
}
}
}
//cout << bitmask << endl;
//cout << setNumber << endl;
//for(int i = 0; i < V; i++)
//cout << curentDegree[i] << " " << getBit(bitmask, i) << endl;
//cout << endl;
int res = 0;
bool found = false;
for(int position = 0; position < V; position++)
{
if(getBit(bitmask, position) == 1)
{
if(curentDegree[position] >= (setNumber+1)/2)
{
//if(bitmask == 15)
//cout << position << endl;
int aux = ~(1 << position);
int aux1 = bitmask;
aux1 &= aux;
/*if(bitmask == 15)
{
cout << position << endl;
aux1 = bitmask;
aux1 &= aux;
cout << aux1 << endl;
}*/
//int aux1 = bitmask;
//if(dp[aux1 & aux] == -1)
if(dp[aux1] == -1)
res += politics(bitmask & aux);
found = true;
}
}
}
if(!found) /* set is solution */
return dp[bitmask] = 1;
return dp[bitmask] = res;
}
int main()
{
FILE* fin = freopen("imunitate.in", "r", stdin);
FILE* fout = freopen("imunitate.out", "w", stdout);
int start, end;
//curentDegree.assign(V, -1);
int T; cin >> T;
while(T--) {
cin >> V >> E;
AdjList.clear();
AdjList.resize(V);
for(int i = 0; i < E; i++)
{
cin >> start >> end;
start--; end--;
AdjList[start].push_back(end);
AdjList[end].push_back(start);
}
memset(dp, -1, sizeof(dp));
cout << politics((1 << V) - 1) << endl;
}
//for(int i = 0; i < (1 << V); i++)
//cout << dp[i] << " ";
//cout << endl;
fclose(fin);
fclose(fout);
return 0;
}