結果

問題 No.317 辺の追加
ユーザー FF256grhyFF256grhy
提出日時 2020-04-12 03:28:29
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,065 ms / 2,000 ms
コード長 2,921 bytes
コンパイル時間 2,236 ms
コンパイル使用メモリ 181,620 KB
実行使用メモリ 11,208 KB
最終ジャッジ日時 2023-10-21 14:41:35
合計ジャッジ時間 12,726 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 153 ms
9,944 KB
testcase_03 AC 146 ms
8,164 KB
testcase_04 AC 296 ms
10,152 KB
testcase_05 AC 130 ms
6,580 KB
testcase_06 AC 171 ms
9,944 KB
testcase_07 AC 54 ms
5,028 KB
testcase_08 AC 620 ms
10,416 KB
testcase_09 AC 200 ms
10,944 KB
testcase_10 AC 223 ms
11,208 KB
testcase_11 AC 774 ms
9,944 KB
testcase_12 AC 218 ms
10,944 KB
testcase_13 AC 1,065 ms
9,944 KB
testcase_14 AC 196 ms
10,944 KB
testcase_15 AC 211 ms
10,944 KB
testcase_16 AC 305 ms
10,680 KB
testcase_17 AC 205 ms
10,944 KB
testcase_18 AC 231 ms
10,944 KB
testcase_19 AC 225 ms
10,944 KB
testcase_20 AC 398 ms
10,680 KB
testcase_21 AC 83 ms
6,844 KB
testcase_22 AC 45 ms
5,292 KB
testcase_23 AC 46 ms
5,292 KB
testcase_24 AC 146 ms
9,944 KB
testcase_25 AC 104 ms
7,900 KB
testcase_26 AC 110 ms
9,944 KB
testcase_27 AC 87 ms
7,108 KB
testcase_28 AC 46 ms
5,292 KB
testcase_29 AC 12 ms
4,348 KB
testcase_30 AC 191 ms
10,416 KB
testcase_31 AC 194 ms
10,416 KB
testcase_32 AC 193 ms
10,416 KB
testcase_33 AC 197 ms
10,416 KB
testcase_34 AC 193 ms
10,416 KB
testcase_35 AC 196 ms
10,416 KB
testcase_36 AC 207 ms
10,416 KB
testcase_37 AC 201 ms
10,416 KB
testcase_38 AC 199 ms
10,416 KB
testcase_39 AC 195 ms
10,416 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using LL = long long int;
#define incID(i, l, r) for(int i = (l)    ; i <  (r); ++i)
#define decID(i, l, r) for(int i = (r) - 1; i >= (l); --i)
#define incII(i, l, r) for(int i = (l)    ; i <= (r); ++i)
#define decII(i, l, r) for(int i = (r)    ; i >= (l); --i)
#define inc(i, n)  incID(i, 0, n)
#define dec(i, n)  decID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define dec1(i, n) decII(i, 1, n)
#define inID(v, l, r) ((l) <= (v) && (v) <  (r))
#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define FR front()
#define BA back()
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
auto setmin   = [](auto & a, auto b) { return (b <  a ? a = b, true : false); };
auto setmax   = [](auto & a, auto b) { return (b >  a ? a = b, true : false); };
auto setmineq = [](auto & a, auto b) { return (b <= a ? a = b, true : false); };
auto setmaxeq = [](auto & a, auto b) { return (b >= a ? a = b, true : false); };
#define SI(v) static_cast<int>(v.size())
#define RF(e, v) for(auto & e: v)
#define until(e) while(! (e))
#define if_not(e) if(! (e))
#define ef else if
#define UR assert(false)
#define IN(T, ...) T __VA_ARGS__; IN_(__VA_ARGS__);
void IN_() { };
template<typename T, typename ... U> void IN_(T & a, U & ... b) { cin >> a; IN_(b ...); };
template<typename T> void OUT(T && a) { cout << a << endl; }
template<typename T, typename ... U> void OUT(T && a, U && ... b) { cout << a << " "; OUT(b ...); }

// ---- ----

class UnionFind {
private:
	int n, s;
	vector<int> t;
	vector<vector<int>> v;
public:
	UnionFind(int arg_n = 0) { init(arg_n); }
	void init(int arg_n) {
		n = s = arg_n;
		t.clear();
		v.clear();
		inc(i, n) { t.PB(i); v.EB(1, i); }
	}
	int get_n() { return n; }
	int size() { return s; }
	int id(int x) { return t.at(x); }
	const vector<vector<int>> & get_v() { return v; }
	bool unite(int x, int y) {
		x = id(x);
		y = id(y);
		if(x == y) { return false; }
		if(v[x].size() < v[y].size()) { swap(x, y); }
		for(auto & e: v[y]) { v[x].PB(e); t[e] = x; }
		v[y].clear();
		s--;
		return true;
	}
	bool same(int x, int y) { return (id(x) == id(y)); }
	const vector<int> & operator[](int x) { return v[id(x)]; }
	friend ostream & operator<<(ostream & os, const UnionFind & uf) {
		inc(i, uf.n) { os << i << ": "; for(auto & e: uf.v[i]) { os << e << " "; } os << "\n"; }
		return os;
	}
};

// ---- ----

int main() {
	IN(int, n, m);
	UnionFind uf(n);
	inc(i, m) {
		IN(int, a, b);
		a--; b--;
		uf.unite(a, b);
	}
	map<int, int> mp;
	RF(e, uf.get_v()) { mp[SI(e)]++; }
	
	int INF = n + 1;
	vector<int> dp(n + 1, INF);
	dp[0] = 0;
	RF(e, mp) {
		dec(i, n) {
			if(dp[i] == INF) { continue; }
			inc1(j, e.SE) {
				if_not(setmin(dp[i + e.FI * j], dp[i] + j)) { break; }
			}
		}
	}
	inc1(i, n) { OUT(dp[i] == INF ? -1 : dp[i] - 1); }
}
0