답안 #904839

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
904839 2024-01-12T10:20:59 Z simona1230 Love Polygon (BOI18_polygon) C++17
100 / 100
210 ms 11576 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++;

        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||nxt[i]==0)
        {
            ans++;
            continue;
        }
        if(used[i]||nxt[nxt[i]]==i)continue;
        int len=1,ver=i;
        //cout<<i<<endl;
        while(nxt[ver]!=i)
        {
            //cout<<ver<<endl;
            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:39:9: warning: unused variable 'cnt' [-Wunused-variable]
   39 |     int cnt=0,ans=0;
      |         ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 210 ms 11432 KB Output is correct
5 Correct 171 ms 11372 KB Output is correct
6 Correct 179 ms 11356 KB Output is correct
7 Correct 205 ms 10964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 153 ms 9068 KB Output is correct
2 Correct 146 ms 11020 KB Output is correct
3 Correct 173 ms 11200 KB Output is correct
4 Correct 156 ms 11092 KB Output is correct
5 Correct 182 ms 11116 KB Output is correct
6 Correct 153 ms 11244 KB Output is correct
7 Correct 160 ms 11316 KB Output is correct
8 Correct 152 ms 11176 KB Output is correct
9 Correct 171 ms 11348 KB Output is correct
10 Correct 158 ms 11404 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 504 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 210 ms 11432 KB Output is correct
20 Correct 171 ms 11372 KB Output is correct
21 Correct 179 ms 11356 KB Output is correct
22 Correct 205 ms 10964 KB Output is correct
23 Correct 153 ms 9068 KB Output is correct
24 Correct 146 ms 11020 KB Output is correct
25 Correct 173 ms 11200 KB Output is correct
26 Correct 156 ms 11092 KB Output is correct
27 Correct 182 ms 11116 KB Output is correct
28 Correct 153 ms 11244 KB Output is correct
29 Correct 160 ms 11316 KB Output is correct
30 Correct 152 ms 11176 KB Output is correct
31 Correct 171 ms 11348 KB Output is correct
32 Correct 158 ms 11404 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 160 ms 11092 KB Output is correct
38 Correct 177 ms 11272 KB Output is correct
39 Correct 146 ms 11572 KB Output is correct
40 Correct 162 ms 11384 KB Output is correct
41 Correct 152 ms 11292 KB Output is correct
42 Correct 147 ms 11116 KB Output is correct
43 Correct 144 ms 11348 KB Output is correct
44 Correct 165 ms 11276 KB Output is correct
45 Correct 167 ms 11260 KB Output is correct
46 Correct 155 ms 11308 KB Output is correct
47 Correct 157 ms 11392 KB Output is correct
48 Correct 191 ms 11236 KB Output is correct
49 Correct 163 ms 11156 KB Output is correct
50 Correct 157 ms 11092 KB Output is correct
51 Correct 148 ms 11036 KB Output is correct
52 Correct 155 ms 11020 KB Output is correct
53 Correct 180 ms 11320 KB Output is correct
54 Correct 159 ms 11412 KB Output is correct
55 Correct 155 ms 11256 KB Output is correct
56 Correct 163 ms 11348 KB Output is correct
57 Correct 131 ms 11176 KB Output is correct
58 Correct 1 ms 348 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 1 ms 348 KB Output is correct
61 Correct 1 ms 348 KB Output is correct
62 Correct 1 ms 348 KB Output is correct
63 Correct 1 ms 348 KB Output is correct
64 Correct 1 ms 348 KB Output is correct
65 Correct 168 ms 11400 KB Output is correct
66 Correct 159 ms 11576 KB Output is correct
67 Correct 149 ms 11464 KB Output is correct
68 Correct 158 ms 11076 KB Output is correct
69 Correct 1 ms 348 KB Output is correct
70 Correct 1 ms 348 KB Output is correct
71 Correct 1 ms 348 KB Output is correct
72 Correct 1 ms 348 KB Output is correct
73 Correct 1 ms 348 KB Output is correct
74 Correct 1 ms 348 KB Output is correct
75 Correct 1 ms 348 KB Output is correct
76 Correct 1 ms 464 KB Output is correct
77 Correct 0 ms 348 KB Output is correct
78 Correct 0 ms 348 KB Output is correct
79 Correct 1 ms 348 KB Output is correct
80 Correct 1 ms 500 KB Output is correct
81 Correct 1 ms 348 KB Output is correct
82 Correct 1 ms 600 KB Output is correct
83 Correct 1 ms 348 KB Output is correct