Skip to content

Instantly share code, notes, and snippets.

@macrat
Created June 3, 2016 18:07
Show Gist options
  • Save macrat/0cbbe5652f47c758b1088f8c4050917b to your computer and use it in GitHub Desktop.
Save macrat/0cbbe5652f47c758b1088f8c4050917b to your computer and use it in GitHub Desktop.
#include <algorithm>
#include <iostream>
#include <iterator>
#include <unordered_set>
#include <vector>
int read_i() {
int t;
std::cin >> t;
return t;
}
class People {
private:
std::vector< std::unordered_set<int> > people;
int alive;
public:
People(const int n) {
alive = n;
for(int i=0; i<n; i++){
people.push_back(std::unordered_set<int>());
for(int j=read_i(); j>0; j--){
people[i].insert(read_i());
}
}
}
People(const People& x) {
std::copy(x.people.begin(), x.people.end(), std::back_inserter(people));
alive = x.alive;
}
void kill(int i, const std::unordered_set<int>& js) {
for(auto j: js){
if(i == j){
continue;
}
people[j].clear();
alive--;
}
}
int run(const int day=1) const {
if(alive == 1){
return day - 1;
}
for(int d=day; d<30; d++){
for(int i=0; i<people.size(); i++){
if(people[i].find(day) == people[i].end()){
continue;
}
std::unordered_set<int> cands;
for(int j=i+1; j<people.size(); j++){
if(people[j].find(day) != people[j].end()){
cands.insert(j);
}
}
if(cands.size() == 0){
continue;
}
cands.insert(i);
for(const auto c: cands){
People child(*this);
child.kill(c, cands);
const int r = child.run(day+1);
if(r > 0){
return r;
}
}
}
}
return -1;
}
};
int main() {
while(true){
const int n = read_i();
if(n == 0){
break;
}
People people(n);
std::cout << people.run() << std::endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment