結果

問題 No.317 辺の追加
ユーザー antaanta
提出日時 2015-12-10 03:11:07
言語 C++11
(gcc 11.4.0)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 7,120 bytes
コンパイル時間 320 ms
コンパイル使用メモリ 36,224 KB
最終ジャッジ日時 2024-11-14 19:30:21
合計ジャッジ時間 2,599 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
main.cpp:42:26: error: ‘string’ has not been declared
   42 |         void read_string(string *str) {
      |                          ^~~~~~
main.cpp:48:24: error: ‘string’ has not been declared
   48 |         void read_line(string *str) {
      |                        ^~~~~~
main.cpp:75:31: error: ‘string’ has not been declared
   75 |         FastInput &operator()(string &res) { read_string(&res); return *this; }
      |                               ^~~~~~
main.cpp:75:20: error: ‘FastInput& FastInput::operator()(int&)’ cannot be overloaded with ‘FastInput& FastInput::operator()(int&)’
   75 |         FastInput &operator()(string &res) { read_string(&res); return *this; }
      |                    ^~~~~~~~
main.cpp:70:20: note: previous declaration ‘FastInput& FastInput::operator()(int&)’
   70 |         FastInput &operator()(int &res) { read_signed(&res); return *this; }
      |                    ^~~~~~~~
main.cpp: In member function ‘void FastInput::read_string(int*)’:
main.cpp:43:22: error: request for member ‘clear’ in ‘* str’, which is of non-class type ‘int’
   43 |                 str->clear();
      |                      ^~~~~
main.cpp: In member function ‘void FastInput::read_line(int*)’:
main.cpp:49:22: error: request for member ‘clear’ in ‘* str’, which is of non-class type ‘int’
   49 |                 str->clear();
      |                      ^~~~~
main.cpp:52:26: error: request for member ‘empty’ in ‘* str’, which is of non-class type ‘int’
   52 |                 if(!str->empty() && (*str)[str->size() - 1] == '\r') str->resize(str->size() - 1);
      |                          ^~~~~
main.cpp:52:49: error: request for member ‘size’ in ‘* str’, which is of non-class type ‘int’
   52 |                 if(!str->empty() && (*str)[str->size() - 1] == '\r') str->resize(str->size() - 1);
      |                                                 ^~~~
main.cpp:52:75: error: r

ソースコード

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;

class FastInput {
public:
	operator void*() { return feof(stdin) ? 0 : (void*)this; }

	template<typename T>
	void read_unsigned(T *res) {
		T x = 0;
		for(char c = skip(); '0' <= c && c <= '9'; c = gc())
			x = x * 10 + (c - '0');
		*res = x;
	}

	template<typename T>
	void read_signed(T *res) {
		char c = skip();
		bool sign = false;
		if(c == '-') sign = true, c = gc();
		T x = 0;
		for(; '0' <= c && c <= '9'; c = gc())
			x = x * 10 + (c - '0');
		*res = !sign ? x : -x;
	}

	void read_c_string(char *str, int *len) {
		int n = 0;
		for(char c = skip(); !is_delim(c); c = gc())
			str[n ++] = c;
		str[n] = 0;
		*len = n;
	}

	void read_string(string *str) {
		str->clear();
		for(char c = skip(); !is_delim(c); c = gc())
			*str += c;
	}

	void read_line(string *str) {
		str->clear();
		for(char c = gc(); c != '\n'; c = gc())
			*str += c;
		if(!str->empty() && (*str)[str->size() - 1] == '\r') str->resize(str->size() - 1);
	}

	void read_double(double *res) {
		string buf;
		read_string(&buf);
		sscanf(buf.c_str(), "%lf", res);
	}

	void read_char(char *res) {
		*res = skip();
	}

	void read_string_buf(char *res, int n) {
		*res = skip();
	}

	FastInput &operator()(char &res) { read_char(&res); return *this; }
	FastInput &operator()(int &res) { read_signed(&res); return *this; }
	FastInput &operator()(unsigned &res) { read_unsigned(&res); return *this; }
	FastInput &operator()(long long &res) { read_signed(&res); return *this; }
	FastInput &operator()(unsigned long long &res) { read_unsigned(&res); return *this; }
	FastInput &operator()(char *res) { int len; read_c_string(res, &len); return *this; }
	FastInput &operator()(string &res) { read_string(&res); return *this; }
	FastInput &operator()(double &res) { read_double(&res); return *this; }

	template<typename T1, typename T2>
	FastInput &operator()(T1 &res1, T2 &res2) {
		return operator()(res1)(res2);
	}
	template<typename T1, typename T2, typename T3>
	FastInput &operator()(T1 &res1, T2 &res2, T3 &res3) {
		return operator()(res1)(res2)(res3);
	}

private:
	static char gc() {
#ifdef __GNUC__
		return (char)getchar_unlocked();
#elif defined(_MSC_VER)
		return (char)_getchar_nolock();
#else
		return (char)getchar();
#endif
	}

	static bool is_delim(char c) {
		return c == ' ' || c == '\t' || c == '\n' || c == '\r';
	}

	char skip() {
		char c;
		for(c = gc(); is_delim(c); c = gc()) ;
		return c;
	}
} in;

class FastOutput {
public:

	template<typename T>
	void print_unsigned(T x) {
		char buf[24]; int n = 0;
		do buf[n ++] = x % 10, x /= 10; while(x != 0);
		while(n > 0) pc('0' + buf[-- n]);
	}

	template<typename T>
	void print_signed(T x) {
		char buf[24]; int n = 0; bool sign = false;
		if(x < 0) sign = true;
		do buf[n ++] = x % 10, x /= 10; while(x != 0);
		if(!sign) {
			while(n > 0) pc('0' + buf[-- n]);
		} else {
			pc('-');
			while(n > 0) pc('0' - buf[-- n]);
		}
	}

	void print_c_string(const char *str) {
		for(const char *p = str; *p; ++ p)
			pc(*p);
	}

	void print_string(const string &str) {
		print_c_string(str.c_str());
	}

	void print_double(double x, int digits) {
		//|res| < 2^1024 ~ 10^308
		char buf[512];
#ifdef _MSC_VER
		sprintf_s(buf, "%.*f", digits, x);
#else
		sprintf(buf, "%.*f", digits, x);
#endif
		print_c_string(buf);
	}

	void print_char(char x) {
		pc(x);
	}

	FastOutput &operator()(char x) { print_char(x); return *this; }
	FastOutput &operator()(int x) { print_signed(x); return *this; }
	FastOutput &operator()(unsigned x) { print_unsigned(x); return *this; }
	FastOutput &operator()(long long x) { print_signed(x); return *this; }
	FastOutput &operator()(unsigned long long x) { print_unsigned(x); return *this; }
	FastOutput &operator()(const char *str) { print_c_string(str); return *this; }
	FastOutput &operator()(const string &str) { print_string(str); return *this; }
	FastOutput &operator()(double x) { print_double(x, 10); return *this; }

	template<typename T1, typename T2>
	FastOutput &operator()(T1 x1, T2 x2) { return operator()(x1)(x2); }
	template<typename T1, typename T2, typename T3>
	FastOutput &operator()(T1 x1, T2 x2, T3 x3) { return operator()(x1)(x2)(x3); }
	template<typename T1, typename T2, typename T3, typename T4>
	FastOutput &operator()(T1 x1, T2 x2, T3 x3, T4 x4) { return operator()(x1)(x2)(x3)(x4); }

private:
	static void pc(char c) {
#ifdef __GNUC__
		putchar_unlocked(c);
#elif defined(_MSC_VER)
		_putchar_nolock(c);
#else
		putchar(c);
#endif
	}
} out;

class FastOutputN {
public:
	template<typename T1>
	FastOutputN &operator()(T1 x1) { out(x1)('\n'); return *this; }
	template<typename T1, typename T2>
	FastOutputN &operator()(T1 x1, T2 x2) { out(x1, x2)('\n'); return *this; }
	template<typename T1, typename T2, typename T3>
	FastOutputN &operator()(T1 x1, T2 x2, T3 x3) { out(x1, x2, x3)('\n'); return *this; }
	template<typename T1, typename T2, typename T3, typename T4>
	FastOutputN &operator()(T1 x1, T2 x2, T3 x3, T4 x4) { out(x1, x2, x3, x4)('\n'); return *this; }
} outn;

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 - 3; 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(in(N, M)) {
		UnionFind uf; uf.init(N);
		rep(i, M) {
			int u; int v;
			in(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)
			outn(dp[i] == INF ? -1 : dp[i] - 1);
	}
	return 0;
}
0