結果

問題 No.382 シャイな人たち (2)
ユーザー anta
提出日時 2016-06-23 20:21:28
言語 C++14
(gcc 8.3.0)
結果
AC  
実行時間 249 ms
コード長 6,280 Byte
コンパイル時間 1,859 ms
使用メモリ 1,604 KB
最終ジャッジ日時 2019-10-06 22:56:57

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
input-00 AC 249 ms
1,604 KB
input-01 AC 158 ms
1,600 KB
input-02 AC 145 ms
1,600 KB
input-03 AC 145 ms
1,600 KB
input-04 AC 151 ms
1,600 KB
input-05 AC 132 ms
1,604 KB
input-06 AC 172 ms
1,604 KB
input-07 AC 121 ms
1,600 KB
input-08 AC 163 ms
1,604 KB
input-09 AC 240 ms
1,604 KB
input-10 AC 128 ms
1,600 KB
input-11 AC 84 ms
1,604 KB
input-12 AC 126 ms
1,604 KB
input-13 AC 157 ms
1,604 KB
input-14 AC 149 ms
1,604 KB
input-15 AC 82 ms
1,604 KB
input-16 AC 118 ms
1,600 KB
input-17 AC 89 ms
1,600 KB
input-18 AC 96 ms
1,600 KB
input-19 AC 127 ms
1,604 KB
input-a0 AC 3 ms
1,508 KB
テストケース一括ダウンロード

ソースコード

diff #
#include "bits/stdc++.h"
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;
template<typename T, typename U> static void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> static void amax(T &x, U y) { if(x < y) x = y; }

struct BitSet {
	enum { K = 2, W = 64 };
	uint64_t data[K];

	BitSet() : data{} {}

	explicit operator bool() const {
		uint64_t r = 0;
		rep(i, K)
			r |= data[i];
		return r != 0;
	}

	bool get(unsigned pos) const {
		return data[pos / W] >> (pos % W) & 1;
	}

	void set(unsigned pos) {
		data[pos / W] |= uint64_t(1) << (pos % W);
	}

	void unset(unsigned pos) {
		data[pos / W] &= ~(uint64_t(1) << (pos % W));
	}

	int count() const {
		int res = 0;
		rep(i, K) res += popcount(data[i]);
		return res;
	}

	BitSet operator&(BitSet that) const {
		BitSet res;
		rep(i, K)
			res.data[i] = data[i] & that.data[i];
		return res;
	}

	bool isIntersectTo(const BitSet &that) const {
		return bool(*this & that);
	}

	int getSingleton() const {
		rep(i, K) if(data[i]) {
			if((data[i] & (data[i] - 1)) != 0)
				return -1;
			for(int j = i + 1; j < K; ++ j)
				if(data[j])
					return -1;
			return i * W + bsf(data[i]);
		}
		return -1;
	}

	static int bsf(uint64_t x) {
#if defined(__GNUC__)
		return __builtin_ctzll(x);
#else
		unsigned long res;
		_BitScanForward64(&res, x);
		return res;
#endif
	}

	static int popcount(uint64_t x) {
#if defined(__GNUC__)
		return __builtin_popcountll(x);
#else
		return (int)__popcnt64(x);
#endif
	}
};

class MaximumCliqueBranchAndBound {
public:
	typedef uint8_t Vertex;

	BitSet maximumClique(const vector<BitSet> &originalGraph) {
		graph = originalGraph;
		int N = (int)graph.size();
		levels.assign(N, BitSet());
		vertexLevel.assign(N, -1);
		BitSet initSet;
		vector<pair<int, int>> degreeOrder(N);
		for(int i = 0; i < N; ++ i) {
			initSet.set(i);
			degreeOrder[i] = make_pair(-(int)graph[i].count(), i);
		}
		sort(degreeOrder.begin(), degreeOrder.end());
		numRecs = 0;
		globalBuffer.resize(N * N + N + N * 2 * N);
		levelVertices.resize(N);
		rep(i, N)
			levelVertices[i].first = globalBuffer.data() + i * N;
		Vertex *initOrder = globalBuffer.data() + N * N,
			*nextBuffer = initOrder + N;
		rep(i, N)
			initOrder[i] = degreeOrder[i].second;
		rec(initSet, initOrder, N, nextBuffer);
		return maxClique;
	}

private:

	void rec(BitSet remSet, const Vertex *originalOrder, int originalOrderSize, Vertex *buffer) {
		++ numRecs;

		int numLevels = 0, numVertices = 0;
		for(int ix = 0; ix < originalOrderSize; ++ ix) {
			int p = originalOrder[ix];
			if(!remSet.get(p)) continue;
			++ numVertices;
			int k = 0;
			while(k < numLevels && levels[k].isIntersectTo(graph[p]))
				++ k;
			if(k == numLevels) {
				levels[k] = BitSet();
				++ numLevels;
			}
			levels[k].set(p);
			vertexLevel[p] = k;

			int threshold = maxSize - curSize;
			if(k > threshold && k == numLevels - 1) {
				reNumber(p, k, threshold, numLevels);
				if(!levels[k])
					-- numLevels;
			}
		}

		Vertex *order = buffer, *levelOffsets = buffer + numVertices,
			*nextBuffer = levelOffsets + numLevels;
		for(int k = 0; k < numLevels; ++ k)
			levelVertices[k].second = 0;
		for(int ix = 0; ix < originalOrderSize; ++ ix) {
			int v = originalOrder[ix];
			if(remSet.get(v)) {
				auto &p = levelVertices[vertexLevel[v]];
				p.first[p.second ++] = v;
			}
		}
		numVertices = 0;
		for(int k = 0; k < numLevels; ++ k) {
			levelOffsets[k] = numVertices;
			auto p = levelVertices[k];
			for(int i = 0; i < p.second; ++ i)
				order[numVertices ++] = p.first[i];
		}

		while(1) {
			int threshold = max(maxSize - curSize, 0);
			if(threshold >= numLevels || numVertices <= levelOffsets[threshold])
				break;

			int u = order[-- numVertices];

			curClique.set(u), ++ curSize;

			BitSet newSet = remSet & graph[u];
			if(newSet) {
				rec(newSet, order, numVertices, nextBuffer);
			} else if(curSize > maxSize) {
				maxClique = curClique, maxSize = curSize;
			}
			curClique.unset(u), -- curSize;
		}
	}

	void reNumber(int p, int k, int threshold, int numLevels) {
		for(int i = 0; i < threshold; ++ i) {
			BitSet S = graph[p] & levels[i];
			int q = S.getSingleton();
			if(q == -1) continue;
			for(int j = i + 1; j <= threshold; ++ j) {
				if(graph[q] & levels[j]) continue;
				levels[k].unset(p);
				levels[i].unset(q);
				levels[i].set(p);
				levels[j].set(q);
				vertexLevel[p] = i;
				vertexLevel[q] = j;
				return;
			}
		}
	}

	vector<BitSet> graph;
	BitSet curClique, maxClique;
	int curSize, maxSize;

	vector<BitSet> levels;
	vector<int> vertexLevel;
	vector<pair<Vertex*, int>> levelVertices;
	vector<Vertex> globalBuffer;
	long long numRecs;
};

//#include "C:\Dropbox\backup\implements\Util\CPUTime.hpp"

int main() {
	int initS;
	while(~scanf("%d", &initS)) {
	vector<pair<double, int>> list;
	//int initS; for(initS = 1; initS < 1000003; ++initS) {
	//if(initS % 100 == 0)cerr << initS << "..." << endl;
	//for(int initS : vector<int>{ 115398, 267904, 52859, 518145, 66814, 519427, 882073, 670079, 758187, 65005, 31884, 519427 , 552242 , 394304 , 371116 , 626395, 60145, 649 }) CPUTIMEIT("test") {
		int N, P;
		vector<BitSet> g;
		{
			int S = initS;
			S = ((ll)S * 12345) % 1000003;
			N = (S % 120) + 2;
			g.assign(N, BitSet());
			S = ((ll)S * 12345) % 1000003;
			P = S;
			rep(i, N) reu(j, i + 1, N) {
				S = ((ll)S * 12345) % 1000003;
				if(S < P) {
					g[i].set(j);
					g[j].set(i);
				}
			}
		}
		//cerr << initS << " (" << N << ", " << P << ")" << ": ";
		MaximumCliqueBranchAndBound mc;
		//double start = getCPUTime();
		BitSet ans = mc.maximumClique(g);
		//list.emplace_back(getCPUTime() - start, initS);
		
		if(ans.count() == N) {
			puts("-1");
		} else {
			printf("%d\n", (int)ans.count() + 1);
			vector<int> v;
			rep(i, N) if(ans.get(i))
				v.push_back(i);
			for(int i = 0; i < (int)v.size(); ++ i) {
				if(i != 0) putchar(' ');
				printf("%d", v[i] + 1);
			}
			puts("");
		}
	}
	return 0;
}
0