#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define X first #define Y second #define pb push_back #define rep(X,Y) for (int (X) = 0;(X) < (Y);++(X)) #define rrep(X,Y) for (int (X) = (Y)-1;(X) >=0;--(X)) #define repe(X,Y) for ((X) = 0;(X) < (Y);++(X)) #define peat(X,Y) for (;(X) < (Y);++(X)) #define all(X) (X).begin(),(X).end() #define rall(X) (X).rbegin(),(X).rend() #define eb emplace_back #define UNIQUE(X) (X).erase(unique(all(X)),(X).end()) using namespace std; typedef long long ll; typedef pair pii; typedef pair pll; template using vv=vector>; template ostream& operator<<(ostream &os, const vector &t) { os<<"{"; rep(i,t.size()) {os< ostream& operator<<(ostream &os, const pair &t) { return os<<"("< data; UF(int size):data(size,-1){} bool unite(int x,int y){ x=root(x); y=root(y); if(x!=y){ if(data[y]>n>>m; int x,y; UF uf(n); rep(i,m){ cin>>x>>y; --x; --y; uf.unite(x,y); } vector dp(312345,INF),cnt(n+1),pre; rep(i,n) if(uf.data[i]<0) ++cnt[-uf.data[i]]; //cout< que; rep(j,n/i+1){ while(que.size() && j-que.front().Y>cnt[i]) que.pop_front(); while(que.size() && que.back().X+j-que.back().Y>=dp[j*i+d]) que.pop_back(); que.eb(pre[j*i+d],j); if(que.size()) dp[j*i+d]=que.front().X+j-que.front().Y; //rep(k,que.size())cout<=INF?-1:dp[i+1]-1)<