이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <limits.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
#include <string>
#include <unordered_map>
#include <map>
#include <queue>
#include <set>
#include <stack>
using namespace std;
#define fi first
#define se second
typedef pair<int,int> ii;
int n;
int A[100003], T[100003], H[100003], col[100003];
bool task2 = 1, task3 = 1, task4 = 1, task5 = 1;
int con[1003][1003];
void create_graph()
{
for(int i = 1; i < n; i++)
{
// printf("%d %d\n", H[i], T[i]);
if(T[i] == 0) con[i][H[i]] = con[H[i]][i] = 1, col[i] = col[H[i]]^1;
if(T[i] == 1)
{
col[i] = col[H[i]];
for(int j = 0; j < n; j++)
con[i][j] = con[j][i] = con[H[i]][j];
}
if(T[i] == 2)
{
for(int j = 0; j < n; j++)
con[i][j] = con[j][i] = con[H[i]][j];
con[i][H[i]] = con[H[i]][i] = 1;
}
}
}
//////////////////////////////////////////////////
int mx_bf = 0;
bool in[11];
void bruteforce(int idx, int sum)
{
if(idx == n)
{
bool bol = 1;
for(int i = 0; i < n; i++)
for(int j = i+1; j < n; j++)
if(in[i] && in[j] && con[i][j] == 1)
bol = 0;
if(bol) mx_bf = max(sum, mx_bf);
}
else
{
in[idx] = 1;
bruteforce(idx+1, sum+A[idx]);
in[idx] = 0;
bruteforce(idx+1, sum);
}
}
int solve_bf()
{
create_graph();
bruteforce(0, 0);
return mx_bf;
}
//////////////////////////////////////////////////
int dp[100003][2];
vector<int> chi[100003];
void dfs_tree(int u)
{
int sum0 = 0, sum1 = 0;
for(int v : chi[u])
{
dfs_tree(v);
sum0 += dp[v][0];
sum1 += max(dp[v][0],dp[v][1]);
}
dp[u][0] = sum1;
dp[u][1] = sum0+A[u];
}
int solve_tree()
{
for(int i = 1; i < n; i++) chi[H[i]].push_back(i);
dfs_tree(0);
return max(dp[0][1], dp[0][0]);
}
//////////////////////////////////////////////////
int match[1003], visited[1003];
int bimatch(int u)
{
if(visited[u]) return 0;
// printf("MATCHING %d\n", u);
visited[u] = 1;
for(int v = 0; v < n; v++)
{
if(!con[u][v]) continue;
if(match[v] == -1)
{
// printf(" MATCHED : %d\n", v);
match[u] = v;
match[v] = u;
return 1;
}
if(bimatch(match[v]))
{
// printf(" MATCHED : %d\n", v);
match[u] = v;
match[v] = u;
return 1;
}
}
return 0;
}
int solve_bipartide()
{
create_graph();
memset(visited, 0, sizeof visited);
memset(match, -1, sizeof match);
int hit = 0;
for(int i = 0; i < n; i++)
{
if(col[i]) continue;
memset(visited, 0, sizeof visited);
hit += bimatch(i);
// printf("%d\n", hit);
}
return n-hit;
}
//////////////////////////////////////////////////
int solve(int task)
{
int sum = 0;
int mx = 0;
for(int i = 0; i < n; i++)
{
sum += A[i];
mx = max(mx, A[i]);
}
if(task == 1) return solve_bf();
if(task == 2) return sum;
if(task == 3) return mx;
if(task == 4) return solve_tree();
if(task == 5) return solve_bipartide();
return -1;
}
// Find out best sample
int findSample(int N,int confidence[],int host[],int protocol[])
{
n = N;
for(int i = 0; i < n; i++)
{
A[i] = confidence[i];
if(A[i] != 1) task5 = 0;
}
for(int i = 1; i < n; i++)
{
T[i] = protocol[i], H[i] = host[i];
if(T[i] != 1) task2 = 0;
if(T[i] != 2) task3 = 0;
if(T[i] != 0) task4 = 0;
if(T[i] == 2) task5 = 0;
// printf("%d\n", T[i]);
}
if(task5) return solve(5);
if(task2) return solve(2);
if(task3) return solve(3);
if(task4) return solve(4);
if(n <= 10) return solve(1);
return -1;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |