Submission #27860144


Source Code Expand

#include <bits/stdc++.h>
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define rep2(i,k,n) for (int i = (k); i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int,int>;
// using P = pair<ll,ll>;

const ll INF = (ll)1e18;
// const int INF = (int)1e9+7;
template<typename T>
void chmin(T &a, T b) { a = min(a, b); }
template<typename T>
void chmax(T &a, T b) { a = max(a, b); }

struct UnionFind {
  vector<int> d;
  UnionFind(int n=0): d(n,-1) {}
  int find(int x) {
    if (d[x] < 0) return x;
    return d[x] = find(d[x]);
  }
  bool unite(int x, int y) {
    x = find(x); y = find(y);
    if (x == y) return false;
    if (d[x] > d[y]) swap(x,y);
    d[x] += d[y];
    d[y] = x;
    return true;
  }
  bool same(int x, int y) { return find(x) == find(y);}
  int size(int x) { return -d[find(x)];}
};

using Graph = vector<vector<int>>;

void solve() {
    int n, m;
    cin >> n >> m;

    UnionFind uf(n);
    Graph graph(n);
    rep(i,m) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        if (uf.same(a,b)) {
            cout << "No" << endl;
            return;
        }
        uf.unite(a,b);
        graph[a].push_back(b);
        graph[b].push_back(a);
    }

    rep(i,n) {
        if (graph[i].size() > 2) {
            cout << "No" << endl;
            return;
        }
    }

    cout << "Yes" << endl;
}

int main() {
    solve();
    return 0;
}

Submission Info

Submission Time
Task D - Neighbors
User goropikari
Language C++ (GCC 9.2.1)
Score 400
Code Size 1553 Byte
Status AC
Exec Time 77 ms
Memory 9052 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 2
AC × 38
Set Name Test Cases
Sample sample_01.txt, sample_02.txt
All hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt, random_30.txt, random_31.txt, random_32.txt, sample_01.txt, sample_02.txt
Case Name Status Exec Time Memory
hand_01.txt AC 8 ms 5640 KiB
hand_02.txt AC 4 ms 5688 KiB
hand_03.txt AC 2 ms 3556 KiB
hand_04.txt AC 4 ms 5880 KiB
random_01.txt AC 18 ms 6484 KiB
random_02.txt AC 36 ms 7908 KiB
random_03.txt AC 50 ms 8784 KiB
random_04.txt AC 66 ms 9008 KiB
random_05.txt AC 11 ms 4728 KiB
random_06.txt AC 27 ms 6444 KiB
random_07.txt AC 41 ms 6764 KiB
random_08.txt AC 30 ms 4836 KiB
random_09.txt AC 12 ms 4840 KiB
random_10.txt AC 31 ms 6176 KiB
random_11.txt AC 49 ms 8160 KiB
random_12.txt AC 33 ms 5184 KiB
random_13.txt AC 36 ms 7396 KiB
random_14.txt AC 56 ms 8688 KiB
random_15.txt AC 74 ms 8860 KiB
random_16.txt AC 77 ms 8996 KiB
random_17.txt AC 4 ms 3476 KiB
random_18.txt AC 60 ms 7860 KiB
random_19.txt AC 69 ms 8388 KiB
random_20.txt AC 19 ms 4476 KiB
random_21.txt AC 54 ms 8604 KiB
random_22.txt AC 65 ms 8960 KiB
random_23.txt AC 68 ms 8816 KiB
random_24.txt AC 63 ms 8816 KiB
random_25.txt AC 54 ms 8596 KiB
random_26.txt AC 60 ms 8816 KiB
random_27.txt AC 65 ms 9052 KiB
random_28.txt AC 65 ms 9048 KiB
random_29.txt AC 40 ms 7908 KiB
random_30.txt AC 41 ms 7992 KiB
random_31.txt AC 61 ms 8804 KiB
random_32.txt AC 57 ms 9044 KiB
sample_01.txt AC 3 ms 3556 KiB
sample_02.txt AC 2 ms 3452 KiB