Submission #37805586


Source Code Expand

#include <iostream>
#include <string>
#include <numeric>
#include <vector>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <set>
#include <iomanip>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <cctype>
#include <bitset>
#include <cassert>
#include <sstream>
#include <chrono>
using namespace std;
using namespace chrono;
typedef long long ll;
#define int ll
#define pb push_back
#define eb emplace_back
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define repon(i,x) for(int i=0;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define REV(x) reverse(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
#define all(x) x.begin(),x.end()
#define si(x) int(x.size())
#define popcnt(x) __builtin_popcountll(x)
template<class T> using vc = vector<T>;
template<class T> using vvc = vector<vector<T>>;
template<class T> using vvvc = vector<vector<vector<T>>>;
template<class T> using vvvvc = vector<vector<vector<vector<T>>>>;

const ll INF = 1LL << 60;
const ll mod = 1000000007;
//const ll mod = 998244353;

template<class T> void pval(T s, int pre=0, int zp=0){
    if (zp == 0) cout << fixed << setprecision(pre);
    else cout << setfill('0') << setw(zp);
    cout << s << endl;
}

template<class T> void parrV(vc<T> a){
    rep(i, (T)a.size()) pval(a[i]);
}

template<class T> void parrH(vc<T> a){
    rep(i, (T)a.size()){
        if(i) cout << " ";
        cout << a[i];
    } 
    cout << endl;
}

template<class T> void parrV(set<T> a){
    rep(i, (T)a.size()) pval(*next(a.begin(), i));
}

template<class T> void parrV(set<pair<T,T>> a){
    rep(i, (T)a.size()) cout << next(a.begin(), i)->fi << " " << next(a.begin(), i)->sc << endl;
}

template<class T> void parrH(set<T> a){
    rep(i, (T)a.size()){
        if(i) cout << " ";
        cout << *next(a.begin(), i);
    } 
    cout << endl;
}

template<class T> void parrV(vc<pair<T,T>> a){
    rep(i, (T)a.size()){
        cout << next(a.begin(), i)->fi << " " << next(a.begin(), i)->sc << endl;
    } 
}

template<class T> void parrV(vc<pair<char,T>> a){
    rep(i, (T)a.size()){
        cout << next(a.begin(), i)->fi << " " << next(a.begin(), i)->sc << endl;
    } 
}

template<class T> void parrV(vc<pair<string,T>> a){
    rep(i, (T)a.size()){
        cout << next(a.begin(), i)->fi << " " << next(a.begin(), i)->sc << endl;
    } 
}

template<class T> void parrV(vc<map<T,T>> a){
    for(auto x: a){
        cout << x.fi << " " << x.sc << endl;
    }
}

template<class T> void parrV(vc<map<char,T>> a){
    for(auto x: a){
        cout << x.fi << " " << x.sc << endl;
    }
}

template<class T> void parrV(vc<map<string,T>> a){
    for(auto x: a){
        cout << x.fi << " " << x.sc << endl;
    }
}

template<class T> void parr2s(vvc<T> a, bool setmaxlen=false){
    int max_len = 0;
    if(setmaxlen){
        int max_val = -INF;
        rep(i, (T)a.size()){
            rep(j, (T)a.size()){
                max_val = max(a[i][j], max_val);
            }
        }
        while(max_val!=0){
            max_val /= 10;
            max_len++;
        }
    }

    rep(i, (T)a.size()){
        rep(j, (T)a[0].size()){
            if(j) cout << " ";
            if(setmaxlen) cout << setw(max_len) << a[i][j];
            else cout << a[i][j];
        } 
        cout << endl;
    } 
    cout << endl;
}

template<class T> void parr2(vvc<T> a){
    rep(i, (T)a.size()){
        rep(j, (T)a[0].size()){
            cout << a[i][j];
        } 
        cout << endl;
    } 
    cout << endl;
}

int modpow(int a, int b, int m) {
    int p = 1, q = a;
    rep(i,63){
        if ((b & (1LL << i)) != 0) {
            p *= q;
            p %= m;
        }
        q *= q;
        q %= m;
    }
    return p;
}

struct mint {
    ll x; //typedef long long ll;
    mint(ll x=0):x((x%mod+mod)%mod){}
    mint& operator+=(const mint a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += mod-a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const mint a) {
        (x *= a.x) %= mod;
        return *this;
    }
    mint operator+(const mint a) const {
        mint res(*this);
        return res+=a;
    }
    mint operator-(const mint a) const {
        mint res(*this);
        return res-=a;
    }
    mint operator*(const mint a) const {
        mint res(*this);
        return res*=a;
    }
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }
 
  // for prime mod
    mint inv() const {
        return pow(mod-2);
    }
    mint& operator/=(const mint a) {
        return (*this) *= a.inv();
    }
    mint operator/(const mint a) const {
        mint res(*this);
        return res/=a;
    }
};

struct combination {
    vector<mint> fact, ifact;
    combination(int n):fact(n+1),ifact(n+1) {
        assert(n < mod);
        fact[0] = 1;
        for (int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;
        ifact[n] = fact[n].inv();
        for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;
    }
    mint operator()(int n, int k) {
        if (k < 0 || k > n) return 0;
        return fact[n]*ifact[k]*ifact[n-k];
    }
};

template<class T> bool chmin(T& a, T b){
    if(a > b){
        a = b;
        return true;
    }else return false;
}

template<class T> bool chmax(T& a, T b){
    if(a < b){
        a = b;
        return true;
    }else return false;
}

struct Edge{
	int to;
	long long w;
	Edge(int to, long long w): to(to), w(w){}
};
using Graph = vvc<int>;
//using Graph = vvc<Edge>;

/*
    int n, m; cin >> n >> m;
    Graph G(n);
    rep(i,m){
        int a, b; cin >> a >> b; a--; b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }

    int n, m; cin >> n >> m;
    Graph G(n);
    rep(i,m){
        int a, b; int w; cin >> a >> b >> w; a--; b--;
        G[a].push_back(Edge(b, w));
    }

    rep(bit, (1<<n)){
        if(popcnt(bit) >= 5) continue;
        rep(i,n){
            if(bit & (1<<i)){
            }
        }
    }

*/
struct UnionFind {
	vector<int> par, sz;
	UnionFind(int n) : par(n, -1), sz(n, 1){}
	
	int root(int pos){
		if (par[pos] == -1) return pos;
		else return par[pos] = root(par[pos]);
	}

	bool issame(int u, int v){
		return root(u) == root(v);
	}
	
	bool unite(int u, int v){
		u = root(u); v = root(v);
		if(u == v) return false;
		if(sz[u] < sz[v]) swap(u, v);
		par[v] = u;
		sz[u] += sz[v];
		return true;
	}
	
	int size(int pos){
		return sz[root(pos)];
	}
};


UnionFind uf(7);

signed main(){
    int n, m; cin >> n >> m;
    Graph G(n);
    UnionFind 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);
    }

    set<int> ans;
    rep(i,n){
       ans.insert(uf.root(i));
    }
    pval(si(ans));
    return 0;
}

Submission Info

Submission Time
Task C - Count Connected Components
User neustein
Language C++ (GCC 9.2.1)
Score 300
Code Size 7422 Byte
Status AC
Exec Time 9 ms
Memory 3636 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 300 / 300
Status
AC × 3
AC × 20
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 02_corner_00.txt, 02_corner_01.txt, 02_corner_02.txt, 03_path_00.txt, 04_perfect_00.txt, 05_m_eq_0_00.txt, 06_n_eq_1_00.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 5 ms 3540 KiB
00_sample_01.txt AC 2 ms 3516 KiB
00_sample_02.txt AC 3 ms 3592 KiB
01_random_00.txt AC 2 ms 3456 KiB
01_random_01.txt AC 3 ms 3604 KiB
01_random_02.txt AC 2 ms 3520 KiB
01_random_03.txt AC 7 ms 3484 KiB
01_random_04.txt AC 2 ms 3548 KiB
01_random_05.txt AC 2 ms 3484 KiB
01_random_06.txt AC 4 ms 3572 KiB
01_random_07.txt AC 2 ms 3556 KiB
01_random_08.txt AC 3 ms 3604 KiB
01_random_09.txt AC 3 ms 3524 KiB
02_corner_00.txt AC 3 ms 3596 KiB
02_corner_01.txt AC 3 ms 3604 KiB
02_corner_02.txt AC 4 ms 3608 KiB
03_path_00.txt AC 2 ms 3480 KiB
04_perfect_00.txt AC 9 ms 3636 KiB
05_m_eq_0_00.txt AC 2 ms 3488 KiB
06_n_eq_1_00.txt AC 2 ms 3588 KiB