题意 问你从一个n 到 m 经过多少次数 *2 *3能到m
所以特判一下m能否整除n 然后除了以后根据奇偶去判断求次数
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
#include <random>
#include <ctime>
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
//inv[1]=1;
//for(int i=2; i<M; ++i) inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;
//for(int i=0; i<LIM; ++i) coef[i]=1ll*(P[i]-1)*inv[P[i]]%mod;
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
long long n,m;
scanf("%lld%lld",&n,&m);
if(m%n!=0)
{
printf("-1\n");
return 0;
}
m/=n;
int ans = 0;
while(m%2==0)
{
ans++;
m/=2;
}
while(m%3==0)
{
ans++;
m/=3;
}
if(m==1)
{
printf("%d\n",ans);
}
else printf("-1\n");
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}
给你一段 0 1 串 第一天接着第二天 问你最多 1 的次数 保证至少有个0
做法 答案为 max(中间最大连续1,后面连续1 + 前面连续1)
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
#include <random>
#include <ctime>
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
//inv[1]=1;
//for(int i=2; i<M; ++i) inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;
//for(int i=0; i<LIM; ++i) coef[i]=1ll*(P[i]-1)*inv[P[i]]%mod;
int arr[200025];
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int n;
scanf("%d",&n);
for(int i = 1;i<=n;++i) scanf("%d",&arr[i]);
int Back = 0,Front = 0,maxx = 0,res = 0;
for(int i = 1;i<=n;++i)
{
if(arr[i]==1) Front++;
else break;
}
for(int i = n;i>=1;i--)
{
if(arr[i]==1) Back++;
else break;
}
for(int i= 1;i<=n;++i)
{
if(arr[i]==1) res++;
else maxx = max(res,maxx),res=0;
}
maxx = max(res,maxx);
printf("%d\n",max(maxx,Front+Back));
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}
给你 n - 1 个差分数组 问你能否满足生成一个序列 1 - n
我们坐前缀和 就知道 第 i 个前缀和表示的是 arr[i] 与 a[1]的差 我们可以通过 1 - n的高斯公式求出 arr[1] 然后暴力判断一下即可
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
#include <random>
#include <ctime>
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
//inv[1]=1;
//for(int i=2; i<M; ++i) inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;
//for(int i=0; i<LIM; ++i) coef[i]=1ll*(P[i]-1)*inv[P[i]]%mod;
const int MAX_N = 200025;
vector<int > vt,ans;
int q[MAX_N],cnt[MAX_N*3];
long long sum[MAX_N];
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int n;
scanf("%d",&n);
bool flag = true;
long long ck = 1ll*(n+1)*n/2,tmp = 0;
for(int i = 1;i<n;++i)
{
scanf("%d",&q[i]);
sum[i] = sum[i-1] + q[i];
tmp+=sum[i];
}
long long dk = (ck - tmp)/n;
if(dk*n!=ck-tmp)
{
printf("-1\n");
return 0;
}
else
{
ans.push_back(dk);
for(int i = 1;i<n;++i)
ans.push_back(dk+sum[i]);
sort(ans.begin(),ans.end());
for(int i = 0;i<n;++i)
{
if(ans[i]!=(i+1))
{
printf("-1\n");
return 0;
}
}
printf("%lld ",dk);
for(int i = 1;i<n;++i)
printf("%lld ",dk+sum[i]);
}
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}
题意 给你两个字符串 ?是可以随意匹配的 然后同字符可以匹配 问你能匹配的最长匹配 以及下标
做法 我们先把相同的预处理出来 然后处理第一个字符串剩余的 ? 然后处理第二个字符串剩余的 ? 即可
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
#include <random>
#include <ctime>
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
//inv[1]=1;
//for(int i=2; i<M; ++i) inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;
//for(int i=0; i<LIM; ++i) coef[i]=1ll*(P[i]-1)*inv[P[i]]%mod;
map<char,int >mp;
vector<pll> ans;
queue<int > q[28],q_[28],tmp,vt,vt_,st,st_;
bool flag[150025],flag_[150025];
char str[150025],str_[150025];
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
for(int i = 0;i<26;++i)
mp['a'+i] = i;
mp['?'] = 26;
int n,ANS = 0;
scanf("%d",&n);
scanf("%s",str+1);
scanf("%s",str_+1);
for(int i = 1;i<=n;++i)
{
if(str[i]=='?') vt.push(i);
if(str_[i]=='?') vt_.push(i);
else q[mp[str_[i]]].push(i);
}
for(int i = 1;i<=n;++i)
{
if(str[i]=='?') continue;
else
{
if(!q[mp[str[i]]].empty())
{
int now = q[mp[str[i]]].front();
ans.push_back(pll(i,now));
flag[i] = true;
flag_[now] = true;
q[mp[str[i]]].pop();
}
}
}
for(int i = 1;i<=n;++i)
{
if(str[i]=='?'||flag[i]) continue;
st.push(i);
}
for(int i = 1;i<=n;++i)
{
if(str_[i]=='?'||flag_[i]) continue;
st_.push(i);
}
while(!vt.empty())
{
if(!st_.empty())
{
int now = vt.front();
int now_ = st_.front();
ans.push_back(pll(now,now_));
vt.pop();
st_.pop();
}
else if(!vt_.empty())
{
int now = vt.front();
int now_ = vt_.front();
ans.push_back(pll(now,now_));
vt.pop();
vt_.pop();
}
}
while(!vt_.empty())
{
if(!st.empty())
{
int now_ = vt_.front();
int now = st.front();
ans.push_back(pll(now,now_));
vt_.pop();
st.pop();
}
else if(!vt_.empty())
{
int now = vt.front();
int now_ = vt_.front();
ans.push_back(pll(now,now_));
vt.pop();
vt_.pop();
}
}
int sz = ans.size();
printf("%d\n",sz);
for(int i = 0;i<sz;++i)
printf("%d %d\n",ans[i].first,ans[i].second);
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}
题意 给你 n 天的每天贡献 每天要使 H 加上贡献 问你最少天数达到 H <= 0
做法 我们先暴力枚举一遍 看第一次能不能打到H<=0 能就直接输出最小天数 然后处理一下轮次 暴力跑枚举
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
#include <random>
#include <ctime>
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
//inv[1]=1;
//for(int i=2; i<M; ++i) inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;
//for(int i=0; i<LIM; ++i) coef[i]=1ll*(P[i]-1)*inv[P[i]]%mod;
const int MAX_N = 200025;
long long arr[MAX_N],sum_[MAX_N];
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
long long h,n,sum = 0,ans = 0;
scanf("%lld%lld",&h,&n);
for(int i = 1;i<=n;++i) scanf("%lld",&arr[i]),sum+=arr[i],sum_[i] = sum_[i-1] + arr[i];
long long h_ = h;
for(int i = 1;i<=n;++i)
{
h_+=arr[i];
ans++;
if(h_<=0)
{
break;
}
}
if(h_<=0)
{
printf("%lld\n",ans);
return 0;
}
else if(sum>=0)
{
printf("-1\n");
return 0;
}
ans = INF;
for(int i = 1;i<=n;++i)
{
long long cnt = i,tmp;
h_ = h + sum_[i];
if(h_%sum==0) tmp = -h_/sum;
else tmp = -h_/sum + 1;
cnt += tmp*(n);
ans = min(ans,cnt);
}
printf("%lld\n",ans);
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}
F1因为n 只有50 所以可以想到 n*n*n*n 的暴力解法即能求解
先处理出所有的前缀和差值 然后放进一个二维偏序结构体里面判断能不能塞进 然后不断地判断暴力枚举即可
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
#include <random>
#include <ctime>
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
//inv[1]=1;
//for(int i=2; i<M; ++i) inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;
//for(int i=0; i<LIM; ++i) coef[i]=1ll*(P[i]-1)*inv[P[i]]%mod;
vector<int >vt;
const int MAX_N = 55;
int arr[MAX_N];
struct node
{
int l,r;
node(){}
node(int ll ,int rr)
{
l = ll;
r = rr;
}
}tmp[MAX_N*MAX_N];
bool cmp(node a,node b)
{
if(a.r==b.r) return a.l<b.l;
return a.r<b.r;
}
vector<pll> anss,ans_;
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int n,cnt = 0,ans = 0;
scanf("%d",&n);
for(int i = 1;i<=n;++i) scanf("%d",&arr[i]);
for(int i = 1;i<=n;++i) arr[i]+=arr[i-1];
for(int i = 1;i<=n;++i)
{
for(int j = i;j<=n;++j)
{
vt.push_back(arr[j]-arr[i-1]);
}
}
int sz = vt.size();
for(int i = 0;i<sz;++i)
{
cnt = 0;
int now = vt[i];
ans_.clear();
for(int j = 1;j<=n;++j)
{
for(int k = j;k<=n;++k)
{
if(arr[k]-arr[j-1]==now)
tmp[++cnt]=node(j,k);
}
}
sort(tmp+1,tmp+1+cnt,cmp);
int pre = 0;
for(int j= 1;j<=cnt;++j)
{
if(tmp[j].l>pre)
{
ans_.push_back(pll(tmp[j].l,tmp[j].r));
pre = tmp[j].r;
}
}
if(ans<ans_.size())
{
ans = ans_.size();
anss = ans_;
}
}
printf("%d\n",ans);
for(int i = 0;i<ans;++i)
printf("%d %d\n",anss[i].first,anss[i].second);
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}
n是1500 n^3 不能做
跟第一题一样的做法 主要是你要利用第一个循环 利用 r 1 - n l 从 1 到 r 这样就能保证二维偏序 就不用sort
然后用map存出现的值 值一样的都放进一个vector
然后和第一题一样跑即可求值
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
#include <random>
#include <ctime>
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
//inv[1]=1;
//for(int i=2; i<M; ++i) inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;
//for(int i=0; i<LIM; ++i) coef[i]=1ll*(P[i]-1)*inv[P[i]]%mod;
const int MAX_N = 1525;
int arr[MAX_N];
map<int ,int > mp;
set<int > st;
vector<pll> anss,ans_,vt[MAX_N*MAX_N];
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int n,ans=0,cnt,CNT= 0;
scanf("%d",&n);
for(int i = 1;i<=n;++i) scanf("%d",&arr[i]);
for(int i = 1;i<=n;++i) arr[i] += arr[i-1];
for(int j = 1;j<=n;++j)
{
for(int i = 1;i<=j;++i)
{
int now = arr[j]-arr[i-1];
if(mp.find(now)!=mp.end()) vt[mp[now]].push_back(pll(i,j));
else mp[now] = ++CNT,vt[mp[now]].push_back(pll(i,j));
}
}
map<int,int >::iterator it;
for(it = mp.begin();it!=mp.end();it++)
{
int pre = 0,now = it->second;
ans_.clear();
cnt = 0;
int sz = vt[now].size();
for(int i = 0;i<sz;++i)
{
if(vt[now][i].first>pre)
{
cnt++;
pre = vt[now][i].second;
ans_.push_back(pll(vt[now][i].first,vt[now][i].second));
}
}
if(ans<cnt)
{
ans = cnt;
anss = ans_;
}
}
printf("%d\n",ans);
for(int i = 0;i<ans;++i)
printf("%d %d\n",anss[i].first,anss[i].second);
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}
题意 给你一颗树 定义不满足的点就是他连接的所有边里面出现了相同的颜色 问你用颜色去染色 使得不满足的点数量不超过k
做法 我们知道既然你最多只能k个不满足点 那么我们贪心按照度数从大到小排序 另颜色数等于 k+1点的度数 就满足后面所有点都合法 前面所有点都是不满足点 贪心的去染色即可解决这道题
/*
If you can't see the repay
Why not just working step by step?
to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
#include <random>
#include <ctime>
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
//inv[1]=1;
//for(int i=2; i<M; ++i) inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;
//for(int i=0; i<LIM; ++i) coef[i]=1ll*(P[i]-1)*inv[P[i]]%mod;
const int MAX_N = 200025;
int col[MAX_N],ans[MAX_N],arr[MAX_N],m;
vector<int > G[MAX_N],id[MAX_N];
struct node
{
int u,fa;
node(){}
node(int uu,int faa){
u = uu;
fa = faa;
}
};
bool cmp(int a,int b)
{
return a > b;
}
void bfs()
{
queue<node> q;
q.push(node(1,0));
while(!q.empty())
{
node now = q.front();
q.pop();
int cnt = col[now.u];
int sz = G[now.u].size();
for(int i = 0;i<sz;++i)
{
int to = G[now.u][i];
if(to==now.fa) continue;
cnt++;
if(cnt>m) cnt-=m;
col[to] = cnt;
ans[id[now.u][i]] = cnt;
q.push(node(to,now.u));
}
}
}
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int n,k,a,b;
scanf("%d%d",&n,&k);
for(int i = 1;i<n;++i)
{
scanf("%d%d",&a,&b);
G[a].push_back(b);
id[a].push_back(i);
arr[a]++;
G[b].push_back(a);
id[b].push_back(i);
arr[b]++;
}
sort(arr+1,arr+1+n,cmp);
m = arr[k+1];
bfs();
printf("%d\n",m);
for(int i = 1;i<n;++i)
i==n?printf("%d\n",ans[i]):printf("%d ",ans[i]);
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}