Submission #1694470


Source Code Expand

#include <iostream>
#include <vector>
#include <string.h>
#include <stack>
#include <queue>
#include <algorithm>
#include <climits>
#include <cmath>
#include <map>
#include <set>
#include <assert.h>
#include <sstream>
#define REP(i,n) for(ll i=0;i<(n);i++)
#define MOD 1000000007
#define int long long
#ifdef int
const long long INF = LLONG_MAX / 10;
#else
const int INF = 1010101010;
#endif
using namespace std;
typedef long long ll;
typedef vector<vector<ll>> mat;
typedef pair<int, int> P;
//typedef pair<double, double> P;
template <class T> inline void smin(T &a, T const &b) { a = min(a, b); }
template <class T> inline void smax(T &a, T const &b) { a = max(a, b); }

#ifdef LOCAL
#define dump(...)                                         \
    do {                                                  \
        std::ostringstream os;                            \
        os << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; \
        print_to(os, ", ", "\n", __VA_ARGS__);            \
        std::cerr << os.str();                            \
    } while (0)
#define dump_(a)                                          \
    do {                                                  \
        std::ostringstream os;                            \
        os << __LINE__ << ":\t" << #a << " = [";          \
        print_to_(os, ", ", "]\n", all(a));               \
        std::cerr << os.str();                            \
    } while (0)
#else
#define dump(...)
#define dump_(...)
#endif

template <typename T>
void print_to(std::ostream &os, const char *, const char *tail, const T &fst) {
    os << fst << tail;
}
template <typename Fst, typename... Rst>
void print_to(std::ostream &os, const char *del, const char *tail, const Fst &fst, const Rst &... rst) {
    os << fst << del;
    print_to(os, del, tail, rst...);
}
template <typename Iter>
void print_to_(std::ostream &os, const char *del, const char *tail, Iter bgn, Iter end) {
    for (Iter it = bgn; it != end;) {
        os << *it;
        if (++it != end) {
            os << del;
        } else {
            os << tail;
        }
    }
}
template <typename Fst, typename... Rst>
void println(const Fst &fst, const Rst &... rst) {
    print_to(std::cout, "\n", "\n", fst, rst...);
}
template <typename Fst, typename... Rst>
void print(const Fst &fst, const Rst &... rst) {
    print_to(std::cout, " ", "\n", fst, rst...);
}
template <typename Iter>
void println_(Iter bgn, Iter end) {
    print_to_(std::cout, "\n", "\n", bgn, end);
}
template <typename Iter>
void print_(Iter bgn, Iter end) {
    print_to_(std::cout, " ", "\n", bgn, end);
}

// const int MOD = 1000000007;
namespace trush {
    int _ = (std::cout.precision(10), std::cout.setf(std::ios::fixed), std::cin.tie(0),
             std::ios::sync_with_stdio(0), 0);
}

//UnionFind!!!
class UnionFind
{
private:
    vector<int> par;
    vector<int> myRank;
    vector<int> cnt;

public:
    //n要素で初期化
    UnionFind(int n)
    {
        par.resize(n);
        myRank.resize(n);
        cnt.resize(n);
        REP(i,n) {
            par[i] = i;
            myRank[i] = 0;
            cnt[i] = 1;
        }
    }

    //木の根を求める
    int find(int x)
    {
        if (par[x] == x) return x;
        else return par[x] = find(par[x]);
    }

    //xとyの属する集合を併合
    void unite(int x, int y)
    {
        x = find(x);
        y = find(y);
        if (x == y) return;

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

    //xとyが同じ集合に属するか否か
    bool same(int x, int y)
    {
        return find(x) == find(y);
    }

    //xが含まれる集合の要素数
    int count(int x)
    {
        return cnt[find(x)];
    }
};

int N, M;
int K, L;

signed main()
{
    cin >> N >> M;
    UnionFind uf(N + M);
    REP(i,N) {
        cin >> K;
        REP(j,K) {
            cin >> L;
            L--;
            uf.unite(i, N + L);
        }
    }

    for (int i=1; i<N; i++) {
        if (!uf.same(0, i)) {
            cout << "NO" << endl;
            return 0;
        }
    }
    cout << "YES" << endl;
}

Submission Info

Submission Time
Task C - Interpretation
User hiyokko2
Language C++14 (GCC 5.4.1)
Score 400
Code Size 4453 Byte
Status AC
Exec Time 18 ms
Memory 4992 KB

Judge Result

Set Name sample dataset1 dataset2
Score / Max Score 0 / 0 200 / 200 200 / 200
Status
AC × 2
AC × 12
AC × 27
Set Name Test Cases
sample sample-01.txt, sample-02.txt
dataset1 sample-01.txt, sample-02.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt
dataset2 sample-01.txt, sample-02.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 02-01.txt, 02-02.txt, 02-03.txt, 02-04.txt, 02-05.txt, 02-06.txt, 02-07.txt, 02-08.txt, 02-09.txt, 02-10.txt, 02-11.txt, 02-12.txt, 02-13.txt, sample-01.txt, sample-02.txt
Case Name Status Exec Time Memory
01-01.txt AC 1 ms 256 KB
01-02.txt AC 1 ms 256 KB
01-03.txt AC 1 ms 256 KB
01-04.txt AC 1 ms 256 KB
01-05.txt AC 1 ms 256 KB
01-06.txt AC 1 ms 256 KB
01-07.txt AC 1 ms 256 KB
01-08.txt AC 1 ms 256 KB
01-09.txt AC 1 ms 256 KB
01-10.txt AC 1 ms 256 KB
02-01.txt AC 13 ms 2688 KB
02-02.txt AC 14 ms 2560 KB
02-03.txt AC 12 ms 1664 KB
02-04.txt AC 18 ms 3968 KB
02-05.txt AC 15 ms 2432 KB
02-06.txt AC 18 ms 3968 KB
02-07.txt AC 16 ms 2560 KB
02-08.txt AC 13 ms 2560 KB
02-09.txt AC 17 ms 4992 KB
02-10.txt AC 13 ms 2560 KB
02-11.txt AC 13 ms 2560 KB
02-12.txt AC 14 ms 2560 KB
02-13.txt AC 14 ms 2560 KB
sample-01.txt AC 1 ms 256 KB
sample-02.txt AC 1 ms 256 KB