답안 #680555

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
680555 2023-01-11T07:11:57 Z Cross_Ratio Love Polygon (BOI18_polygon) C++14
100 / 100
282 ms 30148 KB
#include <bits/stdc++.h>
using namespace std;
map<string, int> M;
vector<vector<int>> adj;
int type[100005];
bool vis[100005];
vector<int> V;
int no1 = -1, no2 = -1;
array<int, 2> dfs(int c, int p) {
    int d0 = 0, d1 = -1e9;
    int sum = 0;
    vis[c] = true;
    V.push_back(c);
    int cnt = 0;
    for(int n : adj[c]) {
        if(n==p || vis[n]) continue;
        if(c==no1&&n==no2) continue;
        if(c==no2&&n==no1) continue;
        array<int, 2> k = dfs(n, c);
        d0 += max(k[0], k[1]);
        sum += k[1];
        d1 = max(d1, k[0] - k[1]);
        cnt++;
    }
    d1 = sum + d1;
    if(cnt==0) return {0, 0};
    return {d0, 1 + d1};
}
bool vis2[100005];
array<int, 2> dfs2(int c, int p) {
    vis2[c] = true;
    for(int n :adj[c]) {
        if(n==p) continue;
        if(n==c) return {-2, c};
        if(vis2[n]) return {c, n};
        array<int, 2> k = dfs2(n, c);
        if(k[0]==-2) return k;
        if(k[0]>=0) return k;
    }
    return {-1, -1};
}
set<array<int, 2>> Ed;
array<int, 2> Edge[100005];
signed main() {
    cin.sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int N;
    cin >> N;
    int cnt = 0;
    int i, j;
    adj.resize(N);
    for(i=0;i<N;i++) {
        string s1, s2;
        cin >> s1 >> s2;
        int v1 = -1, v2 = -1;
        if(M.count(s1)) v1 = M[s1];
        else {
            M[s1] = v1 = cnt;
            cnt++;
        }
        if(M.count(s2)) v2 = M[s2];
        else {
            M[s2] = v2 = cnt;
            cnt++;
        }
        //adj[v1].push_back(v2);
        //adj[v2].push_back(v1);
        Edge[i] = {v1, v2};
        Ed.insert(Edge[i]);
    }
    if(N%2==1) {
        cout << "-1";
        return 0;
    }
    for(i=0;i<N;i++) type[i] = -1;
    int ans = 0;
    for(i=0;i<N;i++) {
        array<int, 2> re = {Edge[i][1], Edge[i][0]};
        if(Edge[i][0]==Edge[i][1]) continue;
        if(vis[Edge[i][0]] || vis[Edge[i][1]]) continue;
        if(Ed.find(re)!=Ed.end()) {
            vis[Edge[i][0]] = vis[Edge[i][1]] = true;
            vis2[Edge[i][0]] = vis2[Edge[i][1]] = true;
            ans += 2;
        }
    }
    for(i=0;i<N;i++) {
        if(!vis[Edge[i][0]] && !vis[Edge[i][1]] && Edge[i][0]!=Edge[i][1]) {
            adj[Edge[i][0]].push_back(Edge[i][1]);
            adj[Edge[i][1]].push_back(Edge[i][0]);
        }
    }
    for(i=0;i<N;i++) {
        if(!vis2[i]) {
            array<int, 2> backedge = dfs2(i, -1);
            int x = backedge[0], y = backedge[1];
            //cout << "BackEdge is " << x << ' ' << y  << '\n';
            if(x<0) {
                if(x==-1) y = i;
                V.clear();
                array<int, 2> k = dfs(y, -1);
                for(int n : V) vis2[n] = true;
                ans += max(k[0], k[1]);
                //cout << k[0] << ' ' << k[1] << '\n';
                continue;
            }
            V.clear();
            vis[x] = vis[y] = true;
            int ma = 0;
            int val = 0;
            no1 = no2 = -1;
            for(int n : adj[y]) {
                if(!vis[n]) {
                    array<int, 2> k = dfs(n, -1);
                    val += max(k[0], k[1]);
                }
            }
            for(int n : adj[x]) {
                if(!vis[n]) {
                    array<int, 2> k = dfs(n, -1);
                    val += max(k[0], k[1]);
                }
            }
            val++;
            ma = max(ma, val);
            no1 = y, no2 = x;
            vis[x] = vis[y] = false;
            for(int k : V) vis2[k] = true;
            for(int k : V) vis[k] = false;
            V.clear();
            val = 0;
            if(!vis[x]) {
                array<int, 2> k = dfs(x, -1);
                val += max(k[0], k[1]);
            }
            if(!vis[y]) {
                array<int, 2> k = dfs(y, -1);
                val += max(k[0], k[1]);
            }
            for(int n : V) vis2[n] = true;
            V.clear();
            ma = max(ma, val);
            ans += ma;
            //cout << i << " : " << ans << '\n';
        }
    }
    cout << N - ans;
}

Compilation message

polygon.cpp: In function 'int main()':
polygon.cpp:51:12: warning: unused variable 'j' [-Wunused-variable]
   51 |     int i, j;
      |            ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 259 ms 26860 KB Output is correct
5 Correct 241 ms 19412 KB Output is correct
6 Correct 271 ms 27988 KB Output is correct
7 Correct 194 ms 15868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 243 ms 19632 KB Output is correct
2 Correct 249 ms 21580 KB Output is correct
3 Correct 154 ms 16524 KB Output is correct
4 Correct 187 ms 15948 KB Output is correct
5 Correct 265 ms 24716 KB Output is correct
6 Correct 233 ms 20360 KB Output is correct
7 Correct 246 ms 19760 KB Output is correct
8 Correct 190 ms 18884 KB Output is correct
9 Correct 210 ms 20388 KB Output is correct
10 Correct 176 ms 20768 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 259 ms 26860 KB Output is correct
20 Correct 241 ms 19412 KB Output is correct
21 Correct 271 ms 27988 KB Output is correct
22 Correct 194 ms 15868 KB Output is correct
23 Correct 243 ms 19632 KB Output is correct
24 Correct 249 ms 21580 KB Output is correct
25 Correct 154 ms 16524 KB Output is correct
26 Correct 187 ms 15948 KB Output is correct
27 Correct 265 ms 24716 KB Output is correct
28 Correct 233 ms 20360 KB Output is correct
29 Correct 246 ms 19760 KB Output is correct
30 Correct 190 ms 18884 KB Output is correct
31 Correct 210 ms 20388 KB Output is correct
32 Correct 176 ms 20768 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 0 ms 340 KB Output is correct
37 Correct 234 ms 20080 KB Output is correct
38 Correct 253 ms 21296 KB Output is correct
39 Correct 237 ms 19760 KB Output is correct
40 Correct 230 ms 22512 KB Output is correct
41 Correct 253 ms 21984 KB Output is correct
42 Correct 243 ms 22468 KB Output is correct
43 Correct 248 ms 22280 KB Output is correct
44 Correct 265 ms 22456 KB Output is correct
45 Correct 282 ms 22560 KB Output is correct
46 Correct 245 ms 22612 KB Output is correct
47 Correct 222 ms 22772 KB Output is correct
48 Correct 258 ms 21816 KB Output is correct
49 Correct 266 ms 23768 KB Output is correct
50 Correct 166 ms 18668 KB Output is correct
51 Correct 186 ms 18100 KB Output is correct
52 Correct 252 ms 26868 KB Output is correct
53 Correct 250 ms 22556 KB Output is correct
54 Correct 244 ms 21916 KB Output is correct
55 Correct 199 ms 21128 KB Output is correct
56 Correct 208 ms 22476 KB Output is correct
57 Correct 153 ms 22980 KB Output is correct
58 Correct 1 ms 340 KB Output is correct
59 Correct 1 ms 340 KB Output is correct
60 Correct 0 ms 332 KB Output is correct
61 Correct 0 ms 336 KB Output is correct
62 Correct 0 ms 340 KB Output is correct
63 Correct 1 ms 332 KB Output is correct
64 Correct 0 ms 340 KB Output is correct
65 Correct 256 ms 29084 KB Output is correct
66 Correct 233 ms 21544 KB Output is correct
67 Correct 263 ms 30148 KB Output is correct
68 Correct 191 ms 18124 KB Output is correct
69 Correct 1 ms 340 KB Output is correct
70 Correct 0 ms 340 KB Output is correct
71 Correct 0 ms 340 KB Output is correct
72 Correct 0 ms 212 KB Output is correct
73 Correct 0 ms 212 KB Output is correct
74 Correct 0 ms 340 KB Output is correct
75 Correct 1 ms 340 KB Output is correct
76 Correct 1 ms 340 KB Output is correct
77 Correct 1 ms 340 KB Output is correct
78 Correct 1 ms 340 KB Output is correct
79 Correct 0 ms 340 KB Output is correct
80 Correct 1 ms 340 KB Output is correct
81 Correct 1 ms 340 KB Output is correct
82 Correct 0 ms 340 KB Output is correct
83 Correct 1 ms 340 KB Output is correct