题解:P14501 [NCPC 2025] Gotta Trade Some of 'Em
题目大意:
有
思路:
把 impossible,因为不可能凑出
定义
定义
代码:
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
const int N = 1e5 + 10;
int ans[N];
int n,m,k;
int cnt[N],a[N],fa[N];
int find(int x){
return x == fa[x]?x:fa[x] = find(fa[x]);
}
void mix(int x,int y){
int fx = find(x),fy = find(y);
fa[fx] = fy;//合并祖先
cnt[fy] += cnt[fx];//合并人数
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin >> n >> m >> k;
for(int i = 1;i <= n;i ++){
fa[i] = i;
cnt[i] = 1;
}
for(int i = 1;i <= m;i ++){
int x,y;
cin >> x >> y;
int fx = find(x),fy = find(y);
if(fx != fy){
mix(x,y);
}
}
for(int i = 1;i <= n;i ++){
int p = find(i);
if(cnt[p] < k){
puts("impossible");
return 0;
}
if(ans[p] < k){//若当前组拥有数字不足
ans[p] ++;
}
a[i] = ans[p];//当前孩子分配到的数字
}
for(int i = 1;i <= n;i ++)cout << a[i] << ' ';
return 0;
}