Submission #58973650


Source Code Expand

Copy
#include <bits/stdc++.h>
using namespace std;
#define PI 3.141592653589793238462643383279502884L
#define LL long long
#define DD double
#define ULL unsigned long long
#define STR string
template <class T>
using V = vector<T>;
template <class T>
using VV = V<V<T>>;
#define MLL map<LL, LL>
#define MSL map<STR, LL>
#define MLB map<LL, bool>
template <class T>
using PQA = priority_queue<T, V<T>, greater<T>>;
template <class T>
using PQD = priority_queue<T, V<T>, less<T>>;
#define IT iterator
#define F first
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#include <bits/stdc++.h>

using namespace std;
#define PI 3.141592653589793238462643383279502884L
#define LL long long
#define DD double
#define ULL unsigned long long
#define STR string
template <class T>
using V = vector<T>;
template <class T>
using VV = V<V<T>>;
#define MLL map<LL, LL>
#define MSL map<STR, LL>
#define MLB map<LL, bool>
template <class T>
using PQA = priority_queue<T, V<T>, greater<T>>;
template <class T>
using PQD = priority_queue<T, V<T>, less<T>>;
#define IT iterator
#define F first
#define S second
#define PB push_back
#define PLL pair<LL, LL>
#define MP make_pair
#define rep(i, e) for (LL i = 0; i < e; i++)
#define repa(i, s, e) for (LL i = s; i < e; i++)
#define repd(i, s, e) for (LL i = s; i >= e; i--)
#define repauto(x, s) for (auto x : s)
#define rd(...) \
  __VA_ARGS__;  \
  read(__VA_ARGS__)
#define rdv(value, ...) \
  value(__VA_ARGS__);   \
  cin >> value
template <class T>
auto &operator>>(istream &is, vector<T> &xs)
{
  for (auto &x : xs)
    is >> x;
  return is;
}
template <class T>
auto &operator<<(ostream &os, vector<T> &xs)
{
  int sz = xs.size();
  rep(i, sz) os << xs[i] << " \n"[i + 1 == sz];
  return os;
}
template <class T, class Y>
auto &operator<<(ostream &os, pair<T, Y> &xs)
{
  os << "{" << xs.first << ", " << xs.second << "}";
  return os;
}
template <class T, class Y>
auto &operator>>(istream &is, vector<pair<T, Y>> &xs)
{
  for (auto &[x1, x2] : xs)
    is >> x1 >> x2;
  return is;
}
template <class... Args>
auto &read(Args &...args) { return (cin >> ... >> args); }
#define vsorta(v) sort(v.begin(), v.end())
#define vsortd(v) sort(v.begin(), v.end(), greater<>())
#define sort_arr(v, n) sort(v, v + n)
#define rev(v) reverse(v.begin(), v.end())
#define pr(x) cout << x
#define prs(x) cout << x << ' '
#define prn(x) cout << x << '\n'
#define prd() cout << fixed << setprecision(50);
#define Yes cout << "Yes\n"
#define YES cout << "YES\n"
#define No cout << "No\n"
#define NO cout << "NO\n"
#define PN cout << '\n'
#define PS cout << ' '
#define INF (1LL << 60)
#define MOD1 1000000007
#define MOD2 998244353
#define MAX_N 100100

using namespace std;
struct Graph
{
  int V;
  vector<vector<int>> E;
  bool is_directed;

  Graph(int n, bool dir = false)
  {
    V = n;
    E.resize(n);
    is_directed = dir;
  }

  void add_edge(int u, int v)
  {
    E[u].push_back(v);
    if (!is_directed)
      E[v].push_back(u);
  }
  int BFS(int root)
  {
    vector<bool> visited(V, false);
    queue<int> next;
    int ans = 1e8;

    visited[root] = true;
    next.push(root);
    vector<int> dist(V, 1e8);

    dist[root] = 0;

    while (!next.empty())
    {
      int current_node = next.front();
      next.pop();
      for (auto &x : E[current_node])
      {
        if (!visited[x])
        {
          dist[x] = dist[current_node] + 1;
          visited[x] = true;
          next.push(x);
        }
        else if (x == root)
        {
          ans = min(ans, dist[current_node] + 1);
        }
      }
    }
    return ans == 1e8 ? -1 : ans;
  }
};

void solve()
{
  int rd(n, m);
  Graph G(n, true);
  while (m--)
  {
    int rd(u, v);
    G.add_edge(u - 1, v - 1);
  }

  prn(G.BFS(0));
}

int main()
{
  LL T = 1;
  // LL rd(T);
  while (T--)
  {
    solve();
  }
  return 0;
}

Submission Info

Submission Time
Task D - Cycle
User togi11
Language C++ 20 (gcc 12.2)
Score 400
Code Size 3421 Byte
Status AC
Exec Time 113 ms
Memory 15180 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 3
AC × 39
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, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 02_cycle_00.txt, 02_cycle_01.txt, 03_path_00.txt, 03_path_01.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 3532 KB
00_sample_01.txt AC 1 ms 3528 KB
00_sample_02.txt AC 1 ms 3484 KB
01_random_00.txt AC 94 ms 12624 KB
01_random_01.txt AC 55 ms 4848 KB
01_random_02.txt AC 60 ms 11592 KB
01_random_03.txt AC 40 ms 4588 KB
01_random_04.txt AC 105 ms 12852 KB
01_random_05.txt AC 69 ms 5252 KB
01_random_06.txt AC 68 ms 11888 KB
01_random_07.txt AC 51 ms 7588 KB
01_random_08.txt AC 98 ms 12652 KB
01_random_09.txt AC 60 ms 4932 KB
01_random_10.txt AC 72 ms 12128 KB
01_random_11.txt AC 51 ms 4828 KB
01_random_12.txt AC 96 ms 12672 KB
01_random_13.txt AC 51 ms 4780 KB
01_random_14.txt AC 64 ms 11944 KB
01_random_15.txt AC 58 ms 5044 KB
01_random_16.txt AC 99 ms 12644 KB
01_random_17.txt AC 42 ms 4764 KB
01_random_18.txt AC 90 ms 12448 KB
01_random_19.txt AC 44 ms 4592 KB
01_random_20.txt AC 109 ms 12972 KB
01_random_21.txt AC 90 ms 8644 KB
01_random_22.txt AC 72 ms 12120 KB
01_random_23.txt AC 34 ms 4628 KB
01_random_24.txt AC 99 ms 12588 KB
01_random_25.txt AC 79 ms 7056 KB
01_random_26.txt AC 87 ms 12356 KB
01_random_27.txt AC 65 ms 5100 KB
01_random_28.txt AC 98 ms 12684 KB
01_random_29.txt AC 73 ms 5524 KB
01_random_30.txt AC 61 ms 11660 KB
01_random_31.txt AC 44 ms 4548 KB
02_cycle_00.txt AC 113 ms 15180 KB
02_cycle_01.txt AC 111 ms 15016 KB
03_path_00.txt AC 76 ms 15084 KB
03_path_01.txt AC 98 ms 14696 KB


2025-03-05 (Wed)
20:46:25 +00:00