Submission #27860233


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); }

using Graph = vector<vector<int>>;

typedef struct Node {
    int now, prev;
};

bool hasCycle(Graph &graph) {
    int n = graph.size();
    vector<int> used(n,0);
    rep(i,n) {
        if (used[i]) continue;
        queue<Node> que;
        que.push({i,-1});
        while(!que.empty()) {
            Node node = que.front();
            que.pop();
            if (used[node.now]) {
                return true;
            }
            used[node.now] = true;
            for (int x : graph[node.now]) {
                if (x == node.prev) continue;
                que.push({x,node.now});
            }
        }
    }
    return false;
}

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

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

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

    if (hasCycle(graph)) {
        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 1686 Byte
Status AC
Exec Time 74 ms
Memory 8992 KiB

Compile Error

./Main.cpp:20:1: warning: ‘typedef’ was ignored in this declaration
   20 | typedef struct Node {
      | ^~~~~~~

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 10 ms 5560 KiB
hand_02.txt AC 7 ms 5908 KiB
hand_03.txt AC 2 ms 3556 KiB
hand_04.txt AC 16 ms 5664 KiB
random_01.txt AC 27 ms 6604 KiB
random_02.txt AC 42 ms 7776 KiB
random_03.txt AC 59 ms 8652 KiB
random_04.txt AC 70 ms 8832 KiB
random_05.txt AC 12 ms 4844 KiB
random_06.txt AC 35 ms 6512 KiB
random_07.txt AC 43 ms 6800 KiB
random_08.txt AC 31 ms 5128 KiB
random_09.txt AC 14 ms 4956 KiB
random_10.txt AC 32 ms 6276 KiB
random_11.txt AC 49 ms 8124 KiB
random_12.txt AC 33 ms 5460 KiB
random_13.txt AC 35 ms 7252 KiB
random_14.txt AC 53 ms 8720 KiB
random_15.txt AC 68 ms 8980 KiB
random_16.txt AC 72 ms 8980 KiB
random_17.txt AC 3 ms 3656 KiB
random_18.txt AC 53 ms 7856 KiB
random_19.txt AC 67 ms 8440 KiB
random_20.txt AC 22 ms 4344 KiB
random_21.txt AC 58 ms 8728 KiB
random_22.txt AC 63 ms 8832 KiB
random_23.txt AC 63 ms 8916 KiB
random_24.txt AC 63 ms 8984 KiB
random_25.txt AC 65 ms 8540 KiB
random_26.txt AC 69 ms 8452 KiB
random_27.txt AC 66 ms 8380 KiB
random_28.txt AC 66 ms 8452 KiB
random_29.txt AC 74 ms 8188 KiB
random_30.txt AC 68 ms 8060 KiB
random_31.txt AC 62 ms 8976 KiB
random_32.txt AC 61 ms 8992 KiB
sample_01.txt AC 2 ms 3440 KiB
sample_02.txt AC 2 ms 3632 KiB