結果

問題 No.317 辺の追加
ユーザー antaanta
提出日時 2015-12-10 01:21:37
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,180 bytes
コンパイル時間 366 ms
コンパイル使用メモリ 48,236 KB
実行使用メモリ 4,544 KB
最終ジャッジ日時 2023-10-13 10:09:51
合計ジャッジ時間 4,749 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <vector>
#include <algorithm>
#include <cassert>
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#ifndef __GNUC__
#include <intrin.h>
#endif
using namespace std;

struct UnionFind {
	vector<int> data;
	void init(int n) { data.assign(n, -1); }
	bool unionSet(int x, int y) {
		x = root(x); y = root(y);
		if(x != y) {
			if(data[y] < data[x]) swap(x, y);
			data[x] += data[y]; data[y] = x;
		}
		return x != y;
	}
	bool findSet(int x, int y) { return root(x) == root(y); }
	int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
	int size(int x) { return -data[root(x)]; }
};

void update(int *dp, int n, int s, int t) {
	assert(s > 1);
	for(; n >= 0 && n % 4 != 3; -- n) {
		int &x = dp[n + s], y = dp[n] + t;
		if(x > y) x = y;
	}
#ifndef __GNUC__
	const __m128i tttt = _mm_set1_epi32(t);
	for(int i = n - 3; i >= 0; i -= 4) {
		const __m128i x = _mm_loadu_si128(reinterpret_cast<const __m128i*>(dp + (i + s)));
		const __m128i y = _mm_add_epi32(_mm_loadu_si128(reinterpret_cast<const __m128i*>(dp + i)), tttt);
		const __m128i z = _mm_min_epi32(x, y);
		_mm_storeu_si128(reinterpret_cast<__m128i*>(dp + (i + s)), z);
	}
#else
	__asm("vmovd %0, %%xmm2\n\tvpshufd $0, %%xmm2, %%xmm1":: "r"(t) : "xmm1", "xmm2");
	for(int i = n - 1; i >= 0; i -= 4) {
		const int * const p = dp + i;
		int * const q = dp + (i + s);
		__asm("vpaddd %1, %%xmm1, %%xmm0\n\tvpminsd %0, %%xmm0, %%xmm0\n\tvmovups %%xmm0, %0"
			: "=m"(*q) : "m"(*p) : "xmm0", "xmm1");
	}
#endif
}

int main() {
	int N; int M;
	while(~scanf("%d%d", &N, &M)) {
		UnionFind uf; uf.init(N);
		rep(i, M) {
			int u; int v;
			scanf("%d%d", &u, &v), -- u, -- v;
			uf.unionSet(u, v);
		}
		vector<int> counts(N + 1);
		rep(i, N) if(uf.root(i) == i)
			++ counts[uf.size(i)];
		const int INF = 0x3f3f3f3f;
		vector<int> dp(N + 1, INF);
		for(int n = counts[1]; n >= 0; -- n)
			dp[n] = n;
		int sum = counts[1];
		for(int i = 2; i <= N; ++ i) {
			int n = counts[i];
			while(n > 0) {
				int m = (n + 1) / 2;
				update(&dp[0], sum, i * m, m);
				sum += i * m;
				n -= m;
			}
		}
		for(int i = 1; i <= N; ++ i)
			printf("%d\n", dp[i] == INF ? -1 : dp[i] - 1);
	}
	return 0;
}
0