답안 #1010642

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1010642 2024-06-29T09:01:05 Z alexdd Love Polygon (BOI18_polygon) C++17
100 / 100
147 ms 28528 KB
#include<bits/stdc++.h>
using namespace std;
int n,rez;
pair<string,string> cit[100005];
map<string,int> nrm;
vector<int> con[100005];
int nxt[100005];
bool visited[100005],vis2[100005];
bool iscyc[100005];


bool gata[100005];
void dfs(int nod)
{
    visited[nod]=vis2[nod]=1;
    for(auto adj:con[nod])
    {
        if(iscyc[adj])
            continue;
        dfs(adj);
        if(!gata[adj] && !gata[nod])
        {
            gata[adj]=gata[nod]=1;
            rez++;
        }
    }
}
signed main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>cit[i].first>>cit[i].second;
        nrm[cit[i].first] = i;
    }
    if(n%2==1)
    {
        cout<<-1;
        return 0;
    }
    for(int i=1;i<=n;i++)
    {
        nxt[i] = nrm[cit[i].second];
        con[nrm[cit[i].second]].push_back(i);
    }
    for(int i=1;i<=n;i++)
    {
        if(visited[i])
            continue;
        int cur=i;
        while(1)
        {
            vis2[cur]=1;
            cur = nxt[cur];
            if(vis2[cur])
                break;
        }
        vector<int> cyc;
        int init = cur;
        while(1)
        {
            cyc.push_back(cur);
            iscyc[cur]=1;
            cur = nxt[cur];
            if(cur==init)
                break;
        }
        if((int)cyc.size()==2)
            gata[cyc[0]]=gata[cyc[1]]=1;
        bool bl=0;
        for(auto x:cyc)
        {
            dfs(x);
            if(gata[x]) bl=1;
        }
        if(!bl)
        {
            if((int)cyc.size()%2==0)
            {
                for(int i=0;i<cyc.size();i++)
                    gata[cyc[i]]=1;
                rez += (int)cyc.size()/2;
            }
            else
            {
                for(int i=1;i<cyc.size();i++)
                    gata[cyc[i]]=1;
                rez += (int)cyc.size()/2;
            }
        }
        else
        {
            for(int i=0;i<cyc.size();i++)
            {
                if(gata[cyc[i]])
                {
                    int poz = i-1;
                    if(i==0) poz = (int)cyc.size()-1;
                    while(1)
                    {
                        int poz1 = (poz-1 + (int)cyc.size())%(int)cyc.size();
                        if(gata[cyc[poz1]] || gata[cyc[poz]])
                            break;
                        //cout<<"scoate\n";
                        gata[cyc[poz]]=gata[cyc[poz1]]=1;
                        rez++;
                        poz = (poz1-1 + (int)cyc.size())%(int)cyc.size();
                    }
                }
            }
        }
    }
    int ramase=0;
    for(int i=1;i<=n;i++)
        if(!gata[i])
            ramase++;
    rez += ramase;
    cout<<rez;
    return 0;
}

Compilation message

polygon.cpp: In function 'int main()':
polygon.cpp:81:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   81 |                 for(int i=0;i<cyc.size();i++)
      |                             ~^~~~~~~~~~~
polygon.cpp:87:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   87 |                 for(int i=1;i<cyc.size();i++)
      |                             ~^~~~~~~~~~~
polygon.cpp:94:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   94 |             for(int i=0;i<cyc.size();i++)
      |                         ~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9052 KB Output is correct
2 Correct 5 ms 8840 KB Output is correct
3 Correct 4 ms 9052 KB Output is correct
4 Correct 4 ms 9052 KB Output is correct
5 Correct 4 ms 9052 KB Output is correct
6 Correct 4 ms 9048 KB Output is correct
7 Correct 4 ms 9052 KB Output is correct
8 Correct 3 ms 9052 KB Output is correct
9 Correct 4 ms 9048 KB Output is correct
10 Correct 4 ms 9052 KB Output is correct
11 Correct 4 ms 9052 KB Output is correct
12 Correct 3 ms 9052 KB Output is correct
13 Correct 4 ms 9052 KB Output is correct
14 Correct 3 ms 9052 KB Output is correct
15 Correct 3 ms 9052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 9052 KB Output is correct
2 Correct 4 ms 9052 KB Output is correct
3 Correct 4 ms 9052 KB Output is correct
4 Correct 116 ms 23260 KB Output is correct
5 Correct 119 ms 22864 KB Output is correct
6 Correct 119 ms 23252 KB Output is correct
7 Correct 62 ms 19028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 116 ms 22096 KB Output is correct
2 Correct 115 ms 23736 KB Output is correct
3 Correct 111 ms 22792 KB Output is correct
4 Correct 51 ms 18952 KB Output is correct
5 Correct 126 ms 28528 KB Output is correct
6 Correct 107 ms 21208 KB Output is correct
7 Correct 113 ms 21328 KB Output is correct
8 Correct 127 ms 21588 KB Output is correct
9 Correct 126 ms 20820 KB Output is correct
10 Correct 79 ms 19924 KB Output is correct
11 Correct 4 ms 9052 KB Output is correct
12 Correct 6 ms 9052 KB Output is correct
13 Correct 4 ms 9052 KB Output is correct
14 Correct 3 ms 9052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9052 KB Output is correct
2 Correct 5 ms 8840 KB Output is correct
3 Correct 4 ms 9052 KB Output is correct
4 Correct 4 ms 9052 KB Output is correct
5 Correct 4 ms 9052 KB Output is correct
6 Correct 4 ms 9048 KB Output is correct
7 Correct 4 ms 9052 KB Output is correct
8 Correct 3 ms 9052 KB Output is correct
9 Correct 4 ms 9048 KB Output is correct
10 Correct 4 ms 9052 KB Output is correct
11 Correct 4 ms 9052 KB Output is correct
12 Correct 3 ms 9052 KB Output is correct
13 Correct 4 ms 9052 KB Output is correct
14 Correct 3 ms 9052 KB Output is correct
15 Correct 3 ms 9052 KB Output is correct
16 Correct 3 ms 9052 KB Output is correct
17 Correct 4 ms 9052 KB Output is correct
18 Correct 4 ms 9052 KB Output is correct
19 Correct 116 ms 23260 KB Output is correct
20 Correct 119 ms 22864 KB Output is correct
21 Correct 119 ms 23252 KB Output is correct
22 Correct 62 ms 19028 KB Output is correct
23 Correct 116 ms 22096 KB Output is correct
24 Correct 115 ms 23736 KB Output is correct
25 Correct 111 ms 22792 KB Output is correct
26 Correct 51 ms 18952 KB Output is correct
27 Correct 126 ms 28528 KB Output is correct
28 Correct 107 ms 21208 KB Output is correct
29 Correct 113 ms 21328 KB Output is correct
30 Correct 127 ms 21588 KB Output is correct
31 Correct 126 ms 20820 KB Output is correct
32 Correct 79 ms 19924 KB Output is correct
33 Correct 4 ms 9052 KB Output is correct
34 Correct 6 ms 9052 KB Output is correct
35 Correct 4 ms 9052 KB Output is correct
36 Correct 3 ms 9052 KB Output is correct
37 Correct 124 ms 22612 KB Output is correct
38 Correct 117 ms 21584 KB Output is correct
39 Correct 117 ms 21332 KB Output is correct
40 Correct 123 ms 21344 KB Output is correct
41 Correct 111 ms 21324 KB Output is correct
42 Correct 133 ms 21668 KB Output is correct
43 Correct 132 ms 21844 KB Output is correct
44 Correct 124 ms 21848 KB Output is correct
45 Correct 129 ms 21780 KB Output is correct
46 Correct 128 ms 21832 KB Output is correct
47 Correct 147 ms 20964 KB Output is correct
48 Correct 120 ms 22052 KB Output is correct
49 Correct 118 ms 23508 KB Output is correct
50 Correct 123 ms 22728 KB Output is correct
51 Correct 54 ms 19024 KB Output is correct
52 Correct 134 ms 28496 KB Output is correct
53 Correct 108 ms 21332 KB Output is correct
54 Correct 112 ms 21328 KB Output is correct
55 Correct 123 ms 21620 KB Output is correct
56 Correct 108 ms 20820 KB Output is correct
57 Correct 71 ms 20140 KB Output is correct
58 Correct 3 ms 9052 KB Output is correct
59 Correct 3 ms 9052 KB Output is correct
60 Correct 3 ms 9052 KB Output is correct
61 Correct 4 ms 9052 KB Output is correct
62 Correct 3 ms 9052 KB Output is correct
63 Correct 3 ms 9052 KB Output is correct
64 Correct 3 ms 9052 KB Output is correct
65 Correct 118 ms 23368 KB Output is correct
66 Correct 134 ms 22928 KB Output is correct
67 Correct 116 ms 23420 KB Output is correct
68 Correct 51 ms 19028 KB Output is correct
69 Correct 4 ms 9052 KB Output is correct
70 Correct 6 ms 9052 KB Output is correct
71 Correct 4 ms 8928 KB Output is correct
72 Correct 3 ms 9048 KB Output is correct
73 Correct 4 ms 8956 KB Output is correct
74 Correct 5 ms 9052 KB Output is correct
75 Correct 3 ms 9080 KB Output is correct
76 Correct 4 ms 9052 KB Output is correct
77 Correct 3 ms 9052 KB Output is correct
78 Correct 4 ms 9048 KB Output is correct
79 Correct 4 ms 9052 KB Output is correct
80 Correct 3 ms 9304 KB Output is correct
81 Correct 3 ms 9052 KB Output is correct
82 Correct 4 ms 9052 KB Output is correct
83 Correct 4 ms 9052 KB Output is correct