結果
| 問題 | No.317 辺の追加 |
| コンテスト | |
| ユーザー |
しらっ亭
|
| 提出日時 | 2015-12-25 06:34:21 |
| 言語 | C++11(廃止可能性あり) (gcc 15.2.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 135 ms / 2,000 ms |
| コード長 | 2,489 bytes |
| 記録 | |
| コンパイル時間 | 1,710 ms |
| コンパイル使用メモリ | 172,796 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-09-18 23:43:31 |
| 合計ジャッジ時間 | 8,361 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 38 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define RFOR(i,a,b) for (int i = (b)-1; i >= (a); i--)
#define REP(i,n) for (int i = 0; i < (n); i++)
#define RREP(i,n) for (int i = (n)-1; i >= 0; i--)
#define ALL(x) (x).begin(), (x).end()
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define RITR(x,c) for(__typeof(c.rbegin()) x=c.rbegin();x!=c.rend();x++)
#define MP(x,y) make_pair((x), (y))
#define MIN(a,b) (a < b ? a : b)
#define MAX(a,b) (a > b ? a : b)
#define SZ(x) ((int)(x).size())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define F3(a) memset(a,0x3f,sizeof(a))
#define bitcount(b) __builtin_popcount(b)
#define GCD(a,b) (__gcd(a,b))
#define GCD3(a,b,c) (GCD(GCD(a,b), c))
#define LCM(a,b) (a/GCD(a,b)*b)
#define LCM3(a,b,c) LCM(LCM(a,b),c)
#define MOD 1000000007
template<class T> inline bool amax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool amin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
typedef long long ll;
// -------------------------------------
template<int um> class UF {
public:
vector<int> par,rank,cnt;
UF() {par=rank=vector<int>(um,0); cnt=vector<int>(um,1); for(int i=0;i<um;i++) par[i]=i;}
void reinit() {REP(i,um) rank[i]=0,cnt[i]=1,par[i]=i;}
int operator[](int x) {return (par[x]==x)?(x):(par[x] = operator[](par[x]));}
int count(int x) { return cnt[operator[](x)];}
int operator()(int x,int y) {
if((x=operator[](x))==(y=operator[](y))) return x;
cnt[y]=cnt[x]=cnt[x]+cnt[y];
if(rank[x]>rank[y]) return par[x]=y;
rank[x]+=rank[x]==rank[y]; return par[y]=x;
}
};
int N, M;
int co[100001];
int dp[100001];
UF<100001> uf;
int main() {
cin >> N >> M;
REP(i, M) {
int u, v;
cin >> u >> v;
uf(u-1, v-1);
}
map<int, int> gs;
REP(i, N) co[uf[i]]++;
REP(i, N) if (co[i] != 0) gs[co[i]]++;
F3(dp);
dp[0] = 0;
deque<int> deq;
for(auto& g : gs) {
auto v = g.first;
auto n = g.second;
int num = n;
for (int k = 1; num > 0; k <<= 1) {
int mul = min(k, num);
// v * mul で weight が mul が1個ある
RREP(i, N+1) {
if (i - v * mul >= 0) amin(dp[i], dp[i - v * mul] + mul);
}
num -= mul;
}
}
FOR(i, 1, N + 1) {
cout << (dp[i] <= N ? dp[i] - 1 : -1) << '\n';
}
return 0;
}
しらっ亭