提出 #1821903


ソースコード 拡げる

Copy
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <sstream>
#include <functional>
#include <map>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <list>
#include <numeric>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> P;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const ll INF = 1LL<<29;
const ll mod = 1e9+7;
#define rep(i,n) for(int (i)=0;(i)<(ll)(n);++(i))
#define repd(i,n,d) for(ll (i)=0;(i)<(ll)(n);(i)+=(d))
#define all(v) (v).begin(), (v).end()
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define mset(m,v) memset((m),(v),sizeof(m))
#define chmin(X,Y) ((X)>(Y)?X=(Y),true:false)
#define chmax(X,Y) ((X)<(Y)?X=(Y),true:false)
#define fst first
#define snd second
#define UNIQUE(x) (x).erase(unique(all(x)),(x).end())
template<class T> ostream &operator<<(ostream &os, const vector<T> &v){int n=v.size();rep(i,n)os<<v[i]<<(i==n-1?"":" ");return os;}

#define M 100010
#define N 100010
ll n, m, q, a[M], b[M], x[M], y[M], res[M];

int par[N],rnk[N], cnt;
void init(int n){
	cnt = 0;
	for(int i = 0; i < n; i++){
		par[i] = i;
		rnk[i] = 0;
	}
}

int find(int x){
	if(par[x] == x){
		return x;
	}else{
		return par[x] = find(par[x]);
	}
}

void unite(int x, int y){
	x = find(x);
	y = find(y);
	if(x == y) return;

	if(rnk[x] < rnk[y]){
		par[x] = y;
	}else{
		par[y] = par[x];
		if(rnk[x] == rnk[y]) rnk[y]++;
	}
}

bool same(int x, int y){
	return find(x) == find(y);
}

void rec(vector<int> &ix, ll lb, ll ub){
	int nn = ix.size();
	if(nn==0) return;
	if(ub-lb<=1){
		rep(i, nn) res[ix[i]] = ub;
		return;
	}
	ll md = (ub+lb+1)/2;
	if(cnt>md) init();
	for(int i = cnt; i < md; i++){
		unite(a[i], b[i]);
		cnt++;
	}
	vector<int> ix1, ix2;
	rep(i, nn){
		if(same(x[ix[i]], y[ix[i]])) ix1.push_back(ix[i]);
		else ix2.push_back(ix[i]);
	}
	rec(ix1, lb, md);
	rec(ix2, md, ub);
}

int main(){
	cin>>n>>m;
	rep(i, m){
		cin>>a[i]>>b[i];
		a[i]--; b[i]--;
	}
	cin>>q;
	rep(i, q){
		cin>>x[i]>>y[i];
		x[i]--; y[i]--;
	}
	vector<int> ix(q);
	rep(i, q) ix[i] = i;
	rec(ix, 0, m+1);
	rep(i, q) if(res[i]==m+1) res[i] = -1;
	rep(i, q) cout<<res[i]<<endl;
	return 0;
}

提出情報

提出日時
問題 H - Union Sets
ユーザ Lepton
言語 C++14 (GCC 5.4.1)
得点 0
コード長 2447 Byte
結果 CE

コンパイルエラー

./Main.cpp: In function ‘void rec(std::vector<int>&, ll, ll)’:
./Main.cpp:87:18: error: too few arguments to function ‘void init(int)’
  if(cnt>md) init();
                  ^
./Main.cpp:46:6: note: declared here
 void init(int n){
      ^