답안 #632809

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
632809 2022-08-20T20:40:59 Z LeticiaFCS Logičari (COCI21_logicari) C++17
110 / 110
195 ms 19880 KB
#include "bits/stdc++.h"
#define st first
#define nd second
using lint = int64_t;
constexpr int MOD = int(1e9) + 7;
constexpr int INF = 0x3f3f3f3f;
constexpr int NINF = 0xcfcfcfcf;
constexpr lint LINF = 0x3f3f3f3f3f3f3f3f;
const long double PI = acosl(-1.0);
// Returns -1 if a < b, 0 if a = b and 1 if a > b.
int cmp_double(double a, double b = 0, double eps = 1e-9) {
	return a + eps > b ? b + eps > a ? 0 : 1 : -1;
}
using namespace std;

int n;
array<array<lint,2>, 2> clear;
vector<array<array<lint,2>, 2>> dp;
vector<vector<int>> g;

int root = -1, rootColor = -1;
int pairOfRoot = -1, pairOfRootColor = -1;

const int RED = 0, BLUE = 1;

int solve(int u, int color, int parColor, int p){
    auto &DP = dp[u][color][parColor];
    if(DP != -1) return DP;
    
	DP = INF;	
	
    if (u == root && color != rootColor) return DP;
    if (u == pairOfRoot && color != pairOfRootColor) return DP;
    if (u == pairOfRoot && parColor == BLUE && rootColor == BLUE) return DP;
	
    lint sumRed = color;
    for(auto v:g[u])
        if(v != p)
        	sumRed += solve(v, RED, color, u);

    if (parColor == BLUE) return DP = min(DP, sumRed);
    if (u == root && pairOfRootColor == BLUE) return DP = min(DP, sumRed);
    if (u == pairOfRoot && rootColor == BLUE) return DP = min(DP, sumRed);
     
    for(auto v:g[u])
        if(v != p)
        	DP = min(DP, sumRed - solve(v, RED, color, u) + solve(v, BLUE, color, u) );
        	
    return DP;
}

vector<bool> visited;
void findRoot(int u, int p){
	visited[u] = true;
	for(int w: g[u])
		if(w != p){
			if(visited[w]){
				root = u;
				pairOfRoot = w;
			} else
				findRoot(w, u);		
		}
}


int main() {
	cin.tie(nullptr)->sync_with_stdio(false);
	
    cin>>n;
    g.resize(n+1);
    vector<int> degree(n+1);
    for (int i=0; i<n; i++) {
        int a, b;
        cin>>a>>b;
        g[a].push_back(b);
        g[b].push_back(a);
        degree[a]++;
        degree[b]++;
    }
    
    bool bigCycle = true;
	for (int u=1; u<=n; u++) {
		if(degree[u] != 2)
			bigCycle = false;
	}
	if(bigCycle){
		if(n % 4 == 0) cout<<n/2;
		else cout<<"-1";
		return 0;
	}
    
    visited.resize(n+1);
    findRoot(1, -1);
    
    //cout<<root<<" "<<pairOfRoot<<"\n";
    
    g[root].erase(find(g[root].begin(), g[root].end(), pairOfRoot));
	g[pairOfRoot].erase(find(g[pairOfRoot].begin(), g[pairOfRoot].end(), root));
   	
   	
    for(int i=0; i<2; i++)
    	for(int j=0; j<2; j++)
    		clear[i][j] = -1;
    		
    int ans = INF;
    {
    	dp.assign(n+1, clear);
        rootColor = RED;
        pairOfRootColor = RED;
        ans = min(ans, solve(root, rootColor, 0, pairOfRoot));
    }
    {
    	dp.assign(n+1, clear);
        rootColor = RED;
        pairOfRootColor = BLUE;
        ans = min(ans, solve(root, rootColor, 0, pairOfRoot));
    }
    {
    	dp.assign(n+1, clear);
        rootColor = BLUE;
        pairOfRootColor = RED;
        ans = min(ans, solve(root, rootColor, 0, pairOfRoot));
    }
    {
    	dp.assign(n+1, clear);
        rootColor = BLUE;
        pairOfRootColor = BLUE;
        ans = min(ans, solve(root, rootColor, 0, pairOfRoot));
    }
    
    if(ans == INF) ans = -1;
    
    cout<<ans<<"\n";
    
    return 0;
}
/*
[  ]Leu o problema certo???
[  ]Ver se precisa de long long
[  ]Viu o limite dos fors (é n? é m?)
[  ]Tamanho do vetor, será que é 2e5 em vez de 1e5??
[  ]Testar sample
[  ]Testar casos de  borda
[  ]1LL no 1LL << i
[  ]Testar mod (é 1e9+7, mesmo?, será que o mod não ficou negativo?)
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 324 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 38 ms 7272 KB Output is correct
6 Correct 34 ms 7220 KB Output is correct
7 Correct 36 ms 7244 KB Output is correct
8 Correct 34 ms 7244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 316 KB Output is correct
8 Correct 0 ms 316 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 316 KB Output is correct
8 Correct 0 ms 316 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 316 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 456 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 324 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 38 ms 7272 KB Output is correct
6 Correct 34 ms 7220 KB Output is correct
7 Correct 36 ms 7244 KB Output is correct
8 Correct 34 ms 7244 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 316 KB Output is correct
16 Correct 0 ms 316 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 316 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 1 ms 456 KB Output is correct
33 Correct 94 ms 9480 KB Output is correct
34 Correct 94 ms 9480 KB Output is correct
35 Correct 106 ms 9452 KB Output is correct
36 Correct 86 ms 9468 KB Output is correct
37 Correct 18 ms 2516 KB Output is correct
38 Correct 94 ms 9432 KB Output is correct
39 Correct 6 ms 1108 KB Output is correct
40 Correct 90 ms 9300 KB Output is correct
41 Correct 42 ms 10796 KB Output is correct
42 Correct 77 ms 10876 KB Output is correct
43 Correct 195 ms 19880 KB Output is correct
44 Correct 102 ms 15276 KB Output is correct