Submission #58967749


Source Code Expand

Copy
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define v64 vector<int>
#define vp64 vector<pair<int, int>>
#define forn(i, a, b) for (int i = a; i < b; i++)
#define pqmin priority_queue<int, vector<int>, greater<int>>
#define pqmax priority_queue<int>
#define ln "\n"
#define yy cout << "Yes" << ln
#define nn cout << "No" << ln
#define pi 3.14159265358979323846
const int mod = 1e9 + 7;
#define dbg cout << "debug" << ln;
tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> T;
// Ordered set functions user less_equal for multiset
// it=s.find_by_order(x) (for index)
// s.order_of_key(x)(no of elements smaller than x)
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define v64 vector<int>
#define vp64 vector<pair<int, int>>
#define forn(i, a, b) for (int i = a; i < b; i++)
#define pqmin priority_queue<int, vector<int>, greater<int>>
#define pqmax priority_queue<int>
#define ln "\n"
#define yy cout << "Yes" << ln
#define nn cout << "No" << ln
#define pi 3.14159265358979323846
const int mod = 1e9 + 7;
#define dbg cout << "debug" << ln;
tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> T;
// Ordered set functions user less_equal for multiset
// it=s.find_by_order(x) (for index)
// s.order_of_key(x)(no of elements smaller than x)
int fac[1000001];
void factorial()
{
    fac[0] = fac[1] = 1;
    forn(i, 2, 1000001)
    {
        fac[i] = (fac[i - 1] * i) % mod;
    }
}
int pows[1000001];
bool done = 0;
int power10(int n)
{
    if (!done)
    {
        pows[0] = 1;
        for (int i = 1; i <= 1000000; i++)
            pows[i] = (pows[i - 1] * 10LL) % mod;
        done = 1;
    }
    return pows[n];
}
vector<int> prime;
void sieve()
{
    prime.resize(1e6 + 1);
    for (int i = 0; i < prime.size(); i++)
    {
        prime[i] = i;
    }
    for (int i = 2; i <= 1e6; i++)
    {
        if (prime[i] == i)
        {
            for (int j = 2 * i; j <= 1e6; j += i)
            {
                prime[j] = i;
            }
        }
    }
}
v64 primefac(int n)
{
    v64 res;
    while (n != prime[n])
    {
        res.push_back(prime[n]);
        n /= prime[n];
    }
    if (n != 1)
        res.push_back(n);
    return res;
}
int fastexpo(int a, int b, int m)
{
    a %= m;
    int res = 1;
    while (b > 0)
    {
        if (b & 1)
            res = res * a % m;
        a = a * a % m;
        b >>= 1;
    }
    return res;
}
int fastpow(int a, int b)
{
    if (b == 0)
        return 1;
    int t = fastpow(a, b / 2);
    if (b % 2 == 0)
        return t * t;
    else
        return a * (t * t);
}
int inv(int n)
{
    return fastexpo(n, mod - 2, mod);
}
int ncr(int n, int r)
{
    if (r > n)
        return 0;
    // on factorial fun from main
    int a = fac[n];
    int b = (fac[r] * fac[n - r]) % mod;
    int b1 = inv(b);
    return (a * b1) % mod;
}

// for inverse modulo (k^mod-2)%mod
// by inforkc => don't use hashing instead use set and map
int ans = 1e9;
void dfs(int s, vector<int> adj[], unordered_set<int> vis, int count = 1)
{
    vis.insert(s);
    for (auto child : adj[s])
    {
        if (child == 0)
        {
            ans = min(ans, count);
            return;
        }
        else if (!vis.count(child))
        {
            // cout << child << endl;
            dfs(child, adj, vis, count + 1);
        }
    }
}
void inforkc()
{
    int n, m;
    cin >> n >> m;
    vector<int> adj[n];
    vector<int> deg(n);
    forn(i, 0, m)
    {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        deg[b]++;
        adj[a].push_back(b);
    }
    queue<int> q;
    for (int i = 0; i < n; i++)
    {
        if (deg[i] == 0)
        {
            q.push(i);
        }
    }
    unordered_set<int> st;
    while (!q.empty())
    {
        int t = q.front();
        q.pop();
        st.insert(t);
        for (auto child : adj[t])
        {
            deg[child]--;
            if (deg[child] == 0)
            {
                st.insert(child);
            }
        }
    }
    if (st.size() == n || st.count(0))
    {
        cout << -1 << ln;
        return;
    }
    unordered_set<int> vis;
    dfs(0, adj, vis);
    cout << ans;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    //  freopen("filename.in", "r", stdin);
    // freopen("filename.out", "w", stdout);
    // sieve();
    // factorial();
    int t_e_s_t = 1;
    while (t_e_s_t--)
    {
        inforkc();
    }
    return 0;
}

Submission Info

Submission Time
Task D - Cycle
User invictus109
Language C++ 20 (gcc 12.2)
Score 0
Code Size 4133 Byte
Status WA
Exec Time 2404 ms
Memory 3609244 KB

Compile Error

Main.cpp: In function ‘void sieve()’:
Main.cpp:48:23: warning: comparison of integer expressions of different signedness: ‘long long int’ and ‘std::vector<long long int>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
   48 |     for (int i = 0; i < prime.size(); i++)
      |                     ~~^~~~~~~~~~~~~~
Main.cpp: In function ‘void inforkc()’:
Main.cpp:171:19: warning: comparison of integer expressions of different signedness: ‘std::unordered_set<long long int>::size_type’ {aka ‘long unsigned int’} and ‘long long int’ [-Wsign-compare]
  171 |     if (st.size() == n || st.count(0))
      |         ~~~~~~~~~~^~~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 400
Status
AC × 3
AC × 9
WA × 1
TLE × 29
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 3440 KB
00_sample_01.txt AC 1 ms 3488 KB
00_sample_02.txt AC 1 ms 3536 KB
01_random_00.txt WA 47 ms 18392 KB
01_random_01.txt TLE 2210 ms 63288 KB
01_random_02.txt TLE 2263 ms 1064536 KB
01_random_03.txt TLE 2263 ms 1064020 KB
01_random_04.txt TLE 2346 ms 2442840 KB
01_random_05.txt TLE 2382 ms 3171220 KB
01_random_06.txt AC 50 ms 18248 KB
01_random_07.txt TLE 2321 ms 2014908 KB
01_random_08.txt TLE 2260 ms 869204 KB
01_random_09.txt TLE 2286 ms 1356628 KB
01_random_10.txt TLE 2334 ms 2264772 KB
01_random_11.txt TLE 2372 ms 2997848 KB
01_random_12.txt AC 49 ms 18496 KB
01_random_13.txt TLE 2209 ms 29712 KB
01_random_14.txt TLE 2219 ms 192380 KB
01_random_15.txt TLE 2234 ms 482244 KB
01_random_16.txt TLE 2284 ms 1310752 KB
01_random_17.txt TLE 2207 ms 12820 KB
01_random_18.txt AC 46 ms 18468 KB
01_random_19.txt TLE 2213 ms 43468 KB
01_random_20.txt TLE 2332 ms 2250128 KB
01_random_21.txt TLE 2325 ms 2099392 KB
01_random_22.txt TLE 2343 ms 2436052 KB
01_random_23.txt TLE 2211 ms 24976 KB
01_random_24.txt AC 48 ms 18352 KB
01_random_25.txt TLE 2367 ms 2838168 KB
01_random_26.txt TLE 2326 ms 2120112 KB
01_random_27.txt TLE 2357 ms 2714288 KB
01_random_28.txt TLE 2310 ms 1819660 KB
01_random_29.txt TLE 2381 ms 3178664 KB
01_random_30.txt AC 42 ms 18400 KB
01_random_31.txt TLE 2255 ms 762352 KB
02_cycle_00.txt TLE 2404 ms 3609244 KB
02_cycle_01.txt TLE 2367 ms 2969252 KB
03_path_00.txt AC 28 ms 15364 KB
03_path_01.txt TLE 2366 ms 2971740 KB


2025-03-16 (Sun)
20:18:58 +00:00