답안 #633175

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
633175 2022-08-21T18:38:13 Z SOCIOPATE Love Polygon (BOI18_polygon) C++17
0 / 100
187 ms 16980 KB
#ifdef LOCAL
    #define _GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

#define pll pair<ll, ll>
#define pii pair<int, int>
#define pdd pair<ld, ld>
#define ff first
#define ss second
#define all(v) v.begin(),v.end()

typedef tree<
    int,
    null_type,
    less<int>,
    rb_tree_tag,
    tree_order_statistics_node_update> ordset;

#pragma GCC optimize("-O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-Os")

ll INF = 2e18;
ll mod = 1e9 + 7;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

ll binpow(ll n, ll m){
    if(m == 0) return 1ll;
    if(m & 1ll) return n * binpow(n, m / 2ll);
    ll b = binpow(n, m / 2ll);
    return b*b;
}

vector<vector<int>> a;
vector<int> dp, dpmark;

void dfs(int v, int p){
    int sum = 0;
    //cout << v << ' ' << p << '\n';
    for(int u : a[v]){
        if(u == p) continue;
        dfs(u, v);
        sum += max(dp[u], dpmark[u]);
    }
    dp[v] = sum;
    for(int u : a[v]){
        if(u == p) continue;
        dpmark[v] = max(dpmark[v], sum - max(dp[u], dpmark[u]) + dp[u] + 1);
    }
}

void solve(){
    int n;
    cin >> n;
    if(n & 1){
        cout << -1;
        return;
    }
    map<string, int> names;
    vector<int> to(n);
    int cur = 0;
    for(int i = 0; i < n; i++){
        string s, t;
        cin >> s >> t;
        if(names.find(s) == names.end()) names[s] = cur++;
        if(names.find(t) == names.end()) names[t] = cur++;
        to[names[s]] = names[t];
    }
    int cnt = n;
    for(int i = 0; i < n; i++){
        if(to[i] != -1 && to[i] != i && to[to[i]] == i){
            cnt -= 2;
            to[to[i]] = -1;
            to[i] = -1;
        }
    }
    a.resize(n);
    dp.resize(n, 0);
    dpmark.resize(n, 0);
    int st = -1;
    for(int i = 0; i < n; i++){
        if(to[i] != -1){
            st = i;
            a[i].push_back(to[i]);
            a[to[i]].push_back(i);
        }
    }
    //cout << st << '\n';
    if(st != -1) dfs(st, -1);
    cout << max(dpmark[st], dp[st]) + (cnt - max(dpmark[st], dp[st]))*2;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
        //freopen("output.txt", "w", stdout);
    #endif
	int tt;
    //cin >> tt;
	tt = 1;
	while (tt--) {
        solve();
		//solve();
		#ifdef LOCAL
            cout << "__________________________________" << endl;
		#endif
	}
	#ifdef LOCAL
        cout << "finished in " << clock() * 1.0 / CLOCKS_PER_SEC << "sec" << '\n';
	#endif
	return 0;
}





# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Incorrect 1 ms 212 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 187 ms 16980 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Incorrect 1 ms 212 KB Output isn't correct
3 Halted 0 ms 0 KB -