提出 #58984892


ソースコード 拡げる

#include <bits/stdc++.h>
// #include "prettyprint.hpp"
#define fastio                 \
  ios::sync_with_stdio(false); \
  cin.tie(0)
#define ll long long
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x.size())

#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)

using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set tree<array<ll, 2>, null_type, less<array<ll, 2>>, rb_tree_tag, tree_order_statistics_node_update>

template <int D, typename T>
struct Vec : public vector<Vec<D - 1, T>>
{
  static_assert(D >= 1, "Vector dimension must be greater than zero!");
  template <typename... Args>
  Vec(int n = 0, Args... args) : vector<Vec<D - 1, T>>(n, Vec<D - 1, T>(args...))
  {
  }
};
template <typename T>
struct Vec<1, T> : public vector<T>
{
  Vec(int n = 0, const T &val = T()) : vector<T>(n, val)
  {
  }
};

// Code begins here

const ll M = 1000000007LL;
const ll INF = 1e18 + 10;

template <typename T>
void maxa(T &v, const T &x)
{
  v = max(v, x);
}

template <typename T>
void mina(T &v, const T &x)
{
  v = min(v, x);
}

template <typename T>
void madd(T &v, const T &x)
{
  v = (v + x);
  v %= M;
}

const int mxn = 1e6 + 10;

ll pw(ll a, ll b)
{
  ll res = 1;
  while (b)
  {
    if (b & 1)
      res = (res * a) % M;
    a = (a * a) % M;
    b >>= 1;
  }
  return res;
}

void solve()
{
  int n,m;
  cin >> n >> m;
  Vec<2,int> adj(n);
  Vec<2,int> adj_rev(n);
  for(int i=0;i<m;i++){
    int u,v;
    cin >> u >> v;
    u--,v--;
    adj[u].push_back(v);
    adj_rev[v].push_back(u);
  }
  int re = M;

  auto Bfs = [&](int s,Vec<2,int> &adj){
    vector<int> dist(n,M);
    queue<int> q;
    q.push(s);
    dist[s]=0;
    while(!q.empty()){
      int u=q.front();
      q.pop();
      for(auto v:adj[u]){
        if(dist[v]==M){
          dist[v]=dist[u]+1;
          q.push(v);
        }
      }
    }
    return dist;
  };

  auto dis1 = Bfs(0,adj);
  auto dis2 = Bfs(0,adj_rev);
  for(int i=1;i<n;i++){
    re=min(re,dis1[i]+dis2[i]);
  }
  
  re=(re==M)?-1:re;
  cout << re << endl;
}

int main()
{
  fastio;
  // freopen("in.txt","r",stdin);
  // int t;
  // cin >> t;
  // while (t--)
  solve();
}

提出情報

提出日時
問題 D - Cycle
ユーザ decsp
言語 C++ 23 (gcc 12.2)
得点 400
コード長 2363 Byte
結果 AC
実行時間 75 ms
メモリ 26640 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 400 / 400
結果
AC × 3
AC × 39
セット名 テストケース
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
ケース名 結果 実行時間 メモリ
00_sample_00.txt AC 1 ms 3460 KiB
00_sample_01.txt AC 1 ms 3484 KiB
00_sample_02.txt AC 1 ms 3388 KiB
01_random_00.txt AC 51 ms 21920 KiB
01_random_01.txt AC 20 ms 6080 KiB
01_random_02.txt AC 36 ms 20204 KiB
01_random_03.txt AC 18 ms 5624 KiB
01_random_04.txt AC 56 ms 22560 KiB
01_random_05.txt AC 35 ms 7052 KiB
01_random_06.txt AC 37 ms 20340 KiB
01_random_07.txt AC 26 ms 11744 KiB
01_random_08.txt AC 52 ms 22036 KiB
01_random_09.txt AC 28 ms 6276 KiB
01_random_10.txt AC 40 ms 20768 KiB
01_random_11.txt AC 25 ms 6176 KiB
01_random_12.txt AC 49 ms 22008 KiB
01_random_13.txt AC 17 ms 6036 KiB
01_random_14.txt AC 37 ms 20196 KiB
01_random_15.txt AC 23 ms 6612 KiB
01_random_16.txt AC 52 ms 22000 KiB
01_random_17.txt AC 14 ms 5976 KiB
01_random_18.txt AC 44 ms 21476 KiB
01_random_19.txt AC 16 ms 5520 KiB
01_random_20.txt AC 54 ms 22308 KiB
01_random_21.txt AC 44 ms 14164 KiB
01_random_22.txt AC 39 ms 20764 KiB
01_random_23.txt AC 12 ms 5504 KiB
01_random_24.txt AC 49 ms 22028 KiB
01_random_25.txt AC 41 ms 10968 KiB
01_random_26.txt AC 46 ms 21468 KiB
01_random_27.txt AC 32 ms 6908 KiB
01_random_28.txt AC 52 ms 22036 KiB
01_random_29.txt AC 37 ms 7800 KiB
01_random_30.txt AC 33 ms 19896 KiB
01_random_31.txt AC 19 ms 5716 KiB
02_cycle_00.txt AC 65 ms 26640 KiB
02_cycle_01.txt AC 75 ms 26572 KiB
03_path_00.txt AC 34 ms 26552 KiB
03_path_01.txt AC 52 ms 26516 KiB