Submission #1179997


Source Code Expand

#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
#include <utility>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <random>
#include <deque>

#define INF_LL 1e18
#define INF 1e9

#define REP(i, n) for(int i = 0;i < (n);i++)
#define FOR(i, a, b) for(int i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()

using namespace std;

using ll = long long;
using PII = pair<int, int>;
using PLL = pair<ll, ll>;

const ll MOD = 1e9+7;

int dx[9] = {-1, 1, 0, 0, -1, -1, 1, 1, 0};
int dy[9] = {0, 0, -1, 1, -1, 1, -1, 1, 0};

template<typename T>
void chmax(T &a, T &b){
	a = max(a, b);
}

template<typename T>
void chmin(T &a, T &b){
	a = min(a, b);
}

class Union_find{
private:
	vector<int> par;
	vector<int> rank;
	int n;

public:
	Union_find(int a){
		n = a;
		for(int i = 0;i < n;i++){
			par.push_back(i);
			rank.push_back(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(rank[x] < rank[y]){
			par[x] = y;
		}else{
			par[y] = x;
			if(rank[x] == rank[y]) rank[x]++;
		}
	}

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

class RMQ{
	int n;
	vector<int> dat;
public:
	RMQ(int n_){
		n = 1;
		while(n < n_) n *= 2;
		REP(i, n*2-1){
			dat.push_back(INF);
		}
	}

	void update(int x, int i){
		i += n-1;
		dat[i] = x;
		while(i > 0){
			i = (i-1)/2;
			dat[i] = min(dat[i*2+1], dat[i*2+2]);
		}
	}

	int query(int a, int b, int l, int r, int x){
		if(a <= l && r <= b) return dat[x];
		if(r <= a || b <= l) return INF;

		int res = INF;
		res = min(query(a, b, l, (l+r)/2, x*2+1), query(a, b, (l+r)/2, r, x*2+2));
		return res;
	}
	int query(int a, int b){
		return query(a, b, 0, n, 0);
	}
};

const ll mod = 1e9+7;

vector<int> G[114514];

int main(void){
	int N, M;
	cin >> N >> M;
	Union_find uf(N);
	REP(i, M){
		int a, b;
		cin >> a >> b; a--; b--;
		G[a].push_back(b);
		G[b].push_back(a);
		uf.unite(a, b);
	}
	ll res = 0;
	REP(i, N){
		if(uf.find(i) == i)
			res++;
	}
	cout << res-1 << endl;
}

Submission Info

Submission Time
Task B - 道路工事
User maze1230
Language C++14 (GCC 5.4.1)
Score 100
Code Size 2354 Byte
Status AC
Exec Time 80 ms
Memory 6516 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 2
AC × 22
Set Name Test Cases
Sample sample1.txt, sample2.txt
All 0.txt, 1.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 2.txt, 3.txt, 4.txt, 5.txt, 6.txt, 7.txt, 8.txt, 9.txt, sample1.txt, sample2.txt
Case Name Status Exec Time Memory
0.txt AC 2 ms 2944 KB
1.txt AC 2 ms 2944 KB
10.txt AC 2 ms 2944 KB
11.txt AC 2 ms 2944 KB
12.txt AC 2 ms 2944 KB
13.txt AC 2 ms 2944 KB
14.txt AC 2 ms 2944 KB
15.txt AC 46 ms 4096 KB
16.txt AC 3 ms 3964 KB
17.txt AC 3 ms 3964 KB
18.txt AC 3 ms 3964 KB
19.txt AC 80 ms 6516 KB
2.txt AC 2 ms 2944 KB
3.txt AC 2 ms 2944 KB
4.txt AC 2 ms 2944 KB
5.txt AC 2 ms 2944 KB
6.txt AC 2 ms 2944 KB
7.txt AC 2 ms 2944 KB
8.txt AC 2 ms 2944 KB
9.txt AC 2 ms 2944 KB
sample1.txt AC 2 ms 2944 KB
sample2.txt AC 2 ms 2944 KB