Submission #904837

# Submission time Handle Problem Language Result Execution time Memory
904837 2024-01-12T10:14:55 Z simona1230 Love Polygon (BOI18_polygon) C++17
0 / 100
2000 ms 11064 KB
#include <bits/stdc++.h>

using namespace std;
int n,names=1;
map<string,int> mp;
int nxt[100001],in[100001];
void read()
{
    cin>>n;
    for(int i=1; i<=n; i++)
    {
        string a,b;
        cin>>a>>b;
        if(!mp[a])mp[a]=names++;
        if(!mp[b])mp[b]=names++;

        //cout<<mp[a]<<" "<<mp[b]<<'\n';

        nxt[mp[a]]=mp[b];
        in[mp[b]]++;
        if(a==b)nxt[mp[a]]=0;
    }
    if(n%2==1)
    {
        cout<<"-1"<<endl;
        exit(0);
    }
}

int used[100001];
queue<int> q;

void solve()
{
    for(int i=1;i<=n;i++)
    {
        if(in[i]==0)
            q.push(i);
    }

    int cnt=0,ans=0;
    while(q.size())
    {
        int ver=q.front();
        q.pop();
        int a=nxt[ver];
        int b=nxt[a];
        if(nxt[b]==a)continue;
        in[b]--;
        if(in[b]==0)q.push(b);
        nxt[a]=ver;
        in[ver]++;
        ver=b;
        ans++;
    }

    for(int i=1; i<=n; i++)
    {
        if(in[i]==0)
        {
            ans++;
            continue;
        }
        if(used[i]||nxt[nxt[i]]==i)continue;
        int len=1,ver=i;
        while(nxt[ver]!=i)
        {
            len++;
            ver=nxt[ver];
            used[ver]=1;
        }
        ans+=len/2+len%2;
    }

    cout<<ans<<'\n';
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    read();
    solve();
    return 0;
}

Compilation message

polygon.cpp: In function 'void solve()':
polygon.cpp:41:9: warning: unused variable 'cnt' [-Wunused-variable]
   41 |     int cnt=0,ans=0;
      |         ^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Execution timed out 2082 ms 468 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2013 ms 348 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2017 ms 11064 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Execution timed out 2082 ms 468 KB Time limit exceeded
4 Halted 0 ms 0 KB -