Submission #8886329


Source Code Expand

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; i++)
#define reps(i,n) for(int i = 1; i <= n; i++)
#define rrep(i,n) for(int i = n-1; i >= 0; i--)
#define rreps(i,n) for(int i = n; i >= 1; i--)
#define co(x) cout << x <<endl
#define cs(x) cout << x <<" "
#define cer(x) cerr << x <<endl
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define ERASE(x,val) x.erase(remove(ALL(x), val), x.end())
#define pb push_back

typedef long long ll;
typedef unsigned long long ull;
using namespace std;
ll mod = 1e9 + 7;
template<class T>inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; }return false; }
template<class T>inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; }return false; }

struct UnionFind
{
	vector<int> par;
	vector<int> rank;

	UnionFind(int n) : par(n), rank(n)
	{
		for (int i = 0; i < n; i++)
		{
			par[i] = i;
			rank[i] = 0;
		}
	}
	int root(int x)
	{
		if (par[x] == x)
		{
			return x;
		}
		else
		{
			return par[x] = root(par[x]);
		}
	}
	void unite(int x, int y)
	{
		int rx = root(x);
		int ry = root(y);
		if (rx == ry)return;
		if (rank[rx] < rank[ry])
		{
			par[rx] = ry;
		}
		else
		{
			par[ry] = rx;
			if (rank[rx] == rank[ry])rank[rx]++;
		}
	}
	bool same(int x, int y)
	{
		return root(x) == root(y);
	}
};


int main()
{
	int n, m; cin >> n >> m;
	UnionFind uf(n);
	rep(i, m)
	{
		int a, b; cin >> a >> b;
		a--; b--;
		uf.unite(a, b);
	}
	set<int>s;
	rep(i, n)
	{
		s.insert(uf.root(i));
	}
	int ans = SZ(s) - 1;
	co(ans);
	return 0;
}

Submission Info

Submission Time
Task B - 道路工事
User seryu
Language C++14 (GCC 5.4.1)
Score 100
Code Size 1620 Byte
Status AC
Exec Time 65 ms
Memory 5760 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 1 ms 256 KB
1.txt AC 1 ms 256 KB
10.txt AC 1 ms 256 KB
11.txt AC 1 ms 256 KB
12.txt AC 1 ms 256 KB
13.txt AC 1 ms 256 KB
14.txt AC 1 ms 256 KB
15.txt AC 43 ms 256 KB
16.txt AC 26 ms 5760 KB
17.txt AC 25 ms 5760 KB
18.txt AC 26 ms 5760 KB
19.txt AC 65 ms 1792 KB
2.txt AC 1 ms 256 KB
3.txt AC 1 ms 256 KB
4.txt AC 1 ms 256 KB
5.txt AC 1 ms 256 KB
6.txt AC 1 ms 256 KB
7.txt AC 1 ms 256 KB
8.txt AC 1 ms 256 KB
9.txt AC 1 ms 256 KB
sample1.txt AC 1 ms 256 KB
sample2.txt AC 1 ms 256 KB