Submission #1230387

#TimeUsernameProblemLanguageResultExecution timeMemory
1230387Tenis0206Parking (CEOI22_parking)C++20
0 / 100
34 ms11188 KiB
#include <bits/stdc++.h>

using namespace std;

const int nmax = 2e5;

int n, m;
int b[nmax + 5], t[nmax + 5];

vector<pair<int,int>> l[nmax + 5];

int sel[nmax + 5];

int nr = 0;
int found = 0;

bool over(int a)
{
    if(l[a].front().second != 1 || l[a].back().second != 1)
    {
        return false;
    }
    return true;
}

void dfs(int nod, int poz, bool lvl)
{
    if(over(nod) && sel[nod] == 0)
    {
        ++found;
    }
    if(!sel[nod])
    {
        ++nr;
    }
    ++sel[nod];
    if(sel[nod] == 1)
    {
        for(auto it : l[nod])
        {
            if(poz == it.first && lvl == it.second)
            {
                continue;
            }
            dfs(nod, it.first, it.second);
        }
    }
    if(lvl == 0)
    {
        if(!sel[t[poz]])
        {
            dfs(t[poz], poz, 1);
        }
    }
    else
    {
        if(!sel[b[poz]])
        {
            dfs(b[poz], poz, 0);
        }
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
#ifdef home
    freopen("nr.in","r",stdin);
    freopen("nr.out","w",stdout);
#endif // home
    cin>>n>>m;
    bool done = true;
    for(int i=1; i<=m; i++)
    {
        cin>>b[i]>>t[i];
        l[b[i]].push_back({i, 0});
        l[t[i]].push_back({i, 1});
        if(t[i] != b[i])
        {
            done = false;
        }
    }
    if(n == m && !done)
    {
        cout<<-1<<'\n';
        return 0;
    }
    bool ok = false;
    int rez = 0;
    for(int i=1; i<=m; i++)
    {
        nr = 0;
        found = 0;
        if(t[i] != 0 && !sel[t[i]])
        {
            dfs(t[i], i, 1);
        }
        if(nr <= 1)
        {
            continue;
        }
        rez += (nr + (found > 1) + 1);
        if(found > 1)
        {
            ok = true;
        }
    }
    if(ok && m == n + 1)
    {
        cout<<-1<<'\n';
        return 0;
    }
    cout<<rez<<'\n';
    return 0;
}
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...