답안 #862330

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
862330 2023-10-18T04:41:44 Z guagua0407 Love Polygon (BOI18_polygon) C++17
100 / 100
233 ms 22776 KB
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

void setIO(string s) {
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
}

const int mxn=1e5+5;
map<string,int> mp;
vector<int> adj[mxn];
bool visited[mxn];
bool visited2[mxn];
int nxt[mxn];
bool is_cycle[mxn];
bool used[mxn];
int prv[mxn];
vector<int> tmp;
int dif;

void dfs(int v){
    visited[v]=true;
    for(auto u:adj[v]){
        if(is_cycle[u]) continue;
        dfs(u);
        if(!used[u] and !used[v]){
            used[u]=true;
            used[v]=true;
        }
    }
}

int main() {_
    int n;
    cin>>n;
    if(n&1){
        cout<<-1<<'\n';
        return 0;
    }
    int cnt=0;
    for(int i=0;i<n;i++){
        string a,b;
        cin>>a>>b;
        if(!mp.count(a)) mp[a]=cnt++;
        if(!mp.count(b)) mp[b]=cnt++;
        nxt[mp[a]]=mp[b];
        adj[mp[b]].push_back(mp[a]);
    }
    for(int i=0;i<n;i++){
        if(visited[i]) continue;
        vector<int> vec;
        int cur=i;
        while(!visited[cur]){
            vec.push_back(cur);
            visited[cur]=true;
            cur=nxt[cur];
        }
        vector<int> cycle;
        reverse(all(vec));
        for(auto v:vec){
            is_cycle[v]=true;
            cycle.push_back(v);
            if(v==cur){
                break;
            }
        }
        for(auto v:cycle){
            dfs(v);
            if((int)cycle.size()==2 and used[v]){
                for(auto u:adj[v]){
                    if(used[u]){
                        used[u]=used[v]=false;
                        break;
                    }
                }
            }
        }
        int cur2=cur;
        do{
            if(used[cur2]) break;
            cur2=nxt[cur2];
        }while(cur2!=cur);
        int cur3=cur2;
        vector<int> tmp2;
        do{
            if(used[cur3]){
                for(auto v:tmp2){
                    used[v]=true;
                }
                if((int)tmp2.size()%2==1){
                    used[tmp2.back()]=false;
                }
                tmp2.clear();
            }
            else{
                tmp2.push_back(cur3);
            }
            cur3=nxt[cur3];
        }while(cur3!=cur2);
        if((int)cycle.size()==2 and (int)tmp2.size()==2){
            dif++;
        }
        for(auto v:tmp2){
            used[v]=true;
        }
        if((int)tmp2.size()%2==1){
            used[tmp2.back()]=false;
        }
        tmp2.clear();
    }
    int cnt2=0;
    for(int i=0;i<n;i++){
        //cout<<used[i]<<' ';
        if(used[i]) cnt2++;
    }
    //cout<<'\n';
    cout<<(cnt2/2+n-cnt2-dif)<<'\n';
    return 0;
}
//maybe its multiset not set

Compilation message

polygon.cpp: In function 'void setIO(std::string)':
polygon.cpp:12:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   12 |     freopen((s + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
polygon.cpp:13:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 |     freopen((s + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3676 KB Output is correct
2 Correct 1 ms 3816 KB Output is correct
3 Correct 1 ms 3676 KB Output is correct
4 Correct 1 ms 3676 KB Output is correct
5 Correct 1 ms 3676 KB Output is correct
6 Correct 1 ms 3672 KB Output is correct
7 Correct 1 ms 3676 KB Output is correct
8 Correct 1 ms 3676 KB Output is correct
9 Correct 1 ms 3676 KB Output is correct
10 Correct 1 ms 3676 KB Output is correct
11 Correct 1 ms 3676 KB Output is correct
12 Correct 2 ms 3676 KB Output is correct
13 Correct 1 ms 3676 KB Output is correct
14 Correct 1 ms 3676 KB Output is correct
15 Correct 1 ms 3676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3676 KB Output is correct
2 Correct 1 ms 3676 KB Output is correct
3 Correct 1 ms 3812 KB Output is correct
4 Correct 199 ms 18288 KB Output is correct
5 Correct 208 ms 16904 KB Output is correct
6 Correct 207 ms 18428 KB Output is correct
7 Correct 1 ms 3676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 184 ms 15952 KB Output is correct
2 Correct 196 ms 18000 KB Output is correct
3 Correct 169 ms 16724 KB Output is correct
4 Correct 1 ms 3676 KB Output is correct
5 Correct 209 ms 22744 KB Output is correct
6 Correct 176 ms 15184 KB Output is correct
7 Correct 189 ms 15352 KB Output is correct
8 Correct 203 ms 15696 KB Output is correct
9 Correct 162 ms 14672 KB Output is correct
10 Correct 137 ms 14004 KB Output is correct
11 Correct 1 ms 3676 KB Output is correct
12 Correct 1 ms 3676 KB Output is correct
13 Correct 1 ms 3676 KB Output is correct
14 Correct 1 ms 3676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3676 KB Output is correct
2 Correct 1 ms 3816 KB Output is correct
3 Correct 1 ms 3676 KB Output is correct
4 Correct 1 ms 3676 KB Output is correct
5 Correct 1 ms 3676 KB Output is correct
6 Correct 1 ms 3672 KB Output is correct
7 Correct 1 ms 3676 KB Output is correct
8 Correct 1 ms 3676 KB Output is correct
9 Correct 1 ms 3676 KB Output is correct
10 Correct 1 ms 3676 KB Output is correct
11 Correct 1 ms 3676 KB Output is correct
12 Correct 2 ms 3676 KB Output is correct
13 Correct 1 ms 3676 KB Output is correct
14 Correct 1 ms 3676 KB Output is correct
15 Correct 1 ms 3676 KB Output is correct
16 Correct 1 ms 3676 KB Output is correct
17 Correct 1 ms 3676 KB Output is correct
18 Correct 1 ms 3812 KB Output is correct
19 Correct 199 ms 18288 KB Output is correct
20 Correct 208 ms 16904 KB Output is correct
21 Correct 207 ms 18428 KB Output is correct
22 Correct 1 ms 3676 KB Output is correct
23 Correct 184 ms 15952 KB Output is correct
24 Correct 196 ms 18000 KB Output is correct
25 Correct 169 ms 16724 KB Output is correct
26 Correct 1 ms 3676 KB Output is correct
27 Correct 209 ms 22744 KB Output is correct
28 Correct 176 ms 15184 KB Output is correct
29 Correct 189 ms 15352 KB Output is correct
30 Correct 203 ms 15696 KB Output is correct
31 Correct 162 ms 14672 KB Output is correct
32 Correct 137 ms 14004 KB Output is correct
33 Correct 1 ms 3676 KB Output is correct
34 Correct 1 ms 3676 KB Output is correct
35 Correct 1 ms 3676 KB Output is correct
36 Correct 1 ms 3676 KB Output is correct
37 Correct 224 ms 16688 KB Output is correct
38 Correct 200 ms 15700 KB Output is correct
39 Correct 179 ms 15440 KB Output is correct
40 Correct 173 ms 15188 KB Output is correct
41 Correct 178 ms 15188 KB Output is correct
42 Correct 199 ms 15552 KB Output is correct
43 Correct 187 ms 15684 KB Output is correct
44 Correct 192 ms 15580 KB Output is correct
45 Correct 233 ms 15748 KB Output is correct
46 Correct 177 ms 15700 KB Output is correct
47 Correct 170 ms 15016 KB Output is correct
48 Correct 182 ms 15956 KB Output is correct
49 Correct 184 ms 18004 KB Output is correct
50 Correct 166 ms 16908 KB Output is correct
51 Correct 1 ms 3676 KB Output is correct
52 Correct 193 ms 22776 KB Output is correct
53 Correct 182 ms 15192 KB Output is correct
54 Correct 172 ms 15176 KB Output is correct
55 Correct 178 ms 15696 KB Output is correct
56 Correct 161 ms 14676 KB Output is correct
57 Correct 120 ms 14044 KB Output is correct
58 Correct 1 ms 3672 KB Output is correct
59 Correct 1 ms 3676 KB Output is correct
60 Correct 1 ms 3676 KB Output is correct
61 Correct 1 ms 3676 KB Output is correct
62 Correct 1 ms 3676 KB Output is correct
63 Correct 2 ms 3672 KB Output is correct
64 Correct 1 ms 3676 KB Output is correct
65 Correct 188 ms 18264 KB Output is correct
66 Correct 201 ms 16724 KB Output is correct
67 Correct 198 ms 18272 KB Output is correct
68 Correct 2 ms 3676 KB Output is correct
69 Correct 1 ms 3676 KB Output is correct
70 Correct 1 ms 3672 KB Output is correct
71 Correct 1 ms 3676 KB Output is correct
72 Correct 1 ms 3676 KB Output is correct
73 Correct 1 ms 3676 KB Output is correct
74 Correct 2 ms 3672 KB Output is correct
75 Correct 1 ms 3676 KB Output is correct
76 Correct 1 ms 3676 KB Output is correct
77 Correct 1 ms 3676 KB Output is correct
78 Correct 1 ms 3816 KB Output is correct
79 Correct 1 ms 3676 KB Output is correct
80 Correct 1 ms 3676 KB Output is correct
81 Correct 1 ms 3812 KB Output is correct
82 Correct 1 ms 3784 KB Output is correct
83 Correct 1 ms 3676 KB Output is correct