Artifact Content
Not logged in

Artifact 7827709f822d3c66beb5c1f97e2894b2439e9dd4


// TODO : optimize to O(n log n)
//  current version looks to be running in O(n^2) ... orz
//
// TODO : clean up as a library
//
// Copy&Paste from SRM562 Div1 Hard

	// Input is given as:
	//    v----c---->G[v][c]
	// Must be a complete automaton.
	// !!! The following code is assuming |c|==4 !!!
	//
	// !!! The following code is assuming the final state is {0}.
	//
	mint solve(vector< vector<int> >& G)
	{
		vector< vector<int> > set_buffer;
		set<int> active_sets;
		set<int> active_splitters;

		{
			set_buffer.push_back( vector<int>(1,0) );
			vector<int> all;
			for(int v=1; v<G.size(); ++v)
				all.push_back(v);
			set_buffer.push_back(all);

			active_sets.insert(0);
			active_sets.insert(1);
			active_splitters.insert(0);
		}

		while(!active_splitters.empty())
		{
			const vector<int>& splv = set_buffer[*active_splitters.begin()];
			set<int> spl(splv.begin(), splv.end());
			active_splitters.erase(active_splitters.begin());

			for(int d=0; d<4; ++d)
			for(set<int>::iterator it=active_sets.begin(); it!=active_sets.end(); )
			{
				int ii = *it++;
				const vector<int>& cur = set_buffer[ii];
				vector<int> t, f;
				for(int k=0; k<cur.size(); ++k)
					(spl.count(G[cur[k]][d]) ? t : f).push_back(cur[k]);
				if(!t.empty() && !f.empty())
				{
					set_buffer.push_back(t);
					set_buffer.push_back(f);
					active_sets.insert(set_buffer.size()-2);
					active_sets.insert(set_buffer.size()-1);
					if(active_splitters.count(ii)) {
						active_splitters.insert(set_buffer.size()-2);
						active_splitters.insert(set_buffer.size()-1);
					} else {
						active_splitters.insert(t.size()<f.size() ? set_buffer.size()-2 : set_buffer.size()-1);
					}
					active_splitters.erase(ii);
					active_sets.erase(ii);
				}
			}
		}

		vector<int> es;
		for(set<int>::iterator it=active_sets.begin(); it!=active_sets.end(); ++it)
			if(*it)
				es.push_back(set_buffer[*it].size());
/*
		for(bool changed=true; changed;) 
		{
			changed=false;
			for(int i=0; i<eqs.size(); ++i) if(!eqs[i].second && !dead[i]){
				set<int> spl(eqs[i].first.begin(), eqs[i].first.end());
				eqs[i].second = true;
				for(int k=0; k<eqs.size(); ++k)
				for(int d=0; d<4; ++d) if(!dead[k]) {
					vector<int> t, f;
					for(vector<int>::iterator it=eqs[k].first.begin(); it!=eqs[k].first.end(); ++it)
						(spl.count(G[*it][d]) ? t : f).push_back(*it);
					if(!t.empty() && !f.empty()) {
						if(eqs[k].second) {
							eqs.push_back(make_pair(t,t.size()>f.size()));
							eqs.push_back(make_pair(f,t.size()<=f.size()));
							dead.push_back(false);
							dead.push_back(false);
							changed = true;
						} else {
							eqs.push_back(make_pair(t,false));
							eqs.push_back(make_pair(f,false));
							dead.push_back(false);
							dead.push_back(false);
							changed = true;
						}
						dead[k] = true;
					}
				}
			}
		}

		vector<int> es;
		for(int i=1; i<eqs.size(); ++i) if(!dead[i]) 
			es.push_back(eqs[i].first.size());
*/
		int sum = accumulate(es.begin(), es.end(), 0);
		mint x = POW(2, sum);
		for(int i=0; i<es.size(); ++i)
			x -= POW(2, es[i])-1;
		return x-1;
	}