Submission #1348891

#TimeUsernameProblemLanguageResultExecution timeMemory
1348891adscodingJakarta Skyscrapers (APIO15_skyscraper)C++20
100 / 100
171 ms112400 KiB
#include <bits/stdc++.h>
#define SZ(x) ((int)x.size())
#define fi first
#define se second
#define pb push_back
#define FOR(i, a, b) for (int i = a, _b = b; i <= _b; ++i)
#define FORD(i, a, b) for (int i = a, _b = b; i >= _b; --i)
#define FORLL(i, a, b) for (ll i = a, _b = b; i <= _b; ++i)
#define FORDLL(i, a, b) for (ll i = a, _b = b; i >= _b; --i)
#define all(x) x.begin(), x.end()
#define uni(x) sort(all(x)), x.erase(unique(all(x)), x.end())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define dbg(...) debug(#__VA_ARGS__, __VA_ARGS__)

template<typename T>
void __print_one(const char *&s, const T &x)
{
    while (*s == ' ') ++s;
    const char *p = s;
    int bal = 0;
    while (*s)
    {
        if (*s == '(') ++bal;
        else if (*s == ')') --bal;
        else if (*s == ',' && bal == 0) break;
        ++s;
    }
    cerr.write(p, s - p) << " = " << x;
    if (*s == ',')
    {
        ++s;
        cerr << "  ,  ";
    }
}

template<typename... Args>
void debug(const char *s, Args... args)
{
    cerr << "[  ";
    int dummy[] = { 0 , ( __print_one(s, args) , 0 )... };
    (void)dummy;
    cerr << "  ]\n\n";
}

template<class X>
bool maximize(X &a, X b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}

template<class X>
bool minimize(X &a, X b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}

// --------------------------------------------------------------------------------------------

constexpr int maxn = 3e4 + 3;
int n, m, P[maxn], B[maxn], res = 1e9;
vector<int> Dogs[maxn];
bitset<maxn> vis;
bitset<maxn> vis_dist[maxn];

// --------------------------------------------------------------------------------------------

struct Obj
{
    int u, d, w;
};
queue<Obj> Q;

bool valid(int x)
{
    return x >= 0 && x <= n - 1;
}

bool connect(int u, int d, int w, int v)
{
    if (!valid(v)) return false;
    if (vis_dist[v][d]) return false;
//    dbg(u, d);

    Q.push({v, d, w + 1});
    vis_dist[v][d] = true;

    if (vis[v] == false)
    {
        for (int x : Dogs[v])
        {
            if (vis_dist[v][P[x]] == false)
            {
                Q.push({v, P[x], w + 1});
                vis_dist[v][P[x]] = true;
            }
        }

        vis[v] = true;
    }

    if (v == B[1])
    {
        res = w + 1;
        return true;
    }

    return false;
}

void solve()
{
    cin >> n >> m;
    FOR(i, 0, m - 1)
    {
        cin >> B[i] >> P[i];
        Dogs[B[i]].push_back(i);
    }

    if (B[0] == B[1])
    {
        res = 0;
        goto LAST;
    }

    vis[B[0]] = true;
    for (int x : Dogs[B[0]])
    {
        if (vis_dist[B[0]][P[x]] == false)
        {
            Q.push({B[0], P[x], 0});
            vis_dist[B[0]][P[x]] = true;
        }
    }

    while (Q.size())
    {
        int u = Q.front().u, d = Q.front().d, w = Q.front().w; Q.pop();
        if (connect(u, d, w, u + d))
            break;
        if (connect(u, d, w, u - d))
            break;
    }

    LAST:
        ;
    res = (res == int(1e9) ? -1 : res);
    cout << res;
    dbg(res);
}

signed main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    #define TASK "TEST"
    if (fopen(TASK".INP", "r"))
    {
        freopen(TASK".INP", "r", stdin);
        freopen(TASK".OUT", "w", stdout);
    }
    solve();
    return 0;
}

Compilation message (stderr)

skyscraper.cpp: In function 'int main()':
skyscraper.cpp:172:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  172 |         freopen(TASK".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:173:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  173 |         freopen(TASK".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...