Submission #111959

# Submission time Handle Problem Language Result Execution time Memory
111959 2019-05-17T00:39:20 Z luciocf Friend (IOI14_friend) C++14
69 / 100
55 ms 8028 KB
#include <bits/stdc++.h>
#include "friend.h"
 
using namespace std;
 
const int maxn = 1e3+10;

// ------- MATCHING --------
int match[maxn];

bool mark[maxn];
// -------------------------

int c[maxn];
 
int dp[maxn][2];
 
bool connected[maxn][maxn];
 
vector<int> grafo[maxn];
 
int dfs(int u, int p, bool is)
{
	if (grafo[u].size() == 1 && u != 0)
	{
		if (is) return c[u];
		return 0;
	}
	if (dp[u][is] != -1) return dp[u][is];
 
	int aux = 0;
	if (is) aux = c[u];
 
	for (auto v: grafo[u])
	{
		if (v == p) continue;
 
		if (is) aux += dfs(v, u, 0);
		else aux += max(dfs(v, u, 0), dfs(v, u, 1));
	}
 
	return dp[u][is] = aux;
}

bool dfs_match(int u)
{
	if (mark[u]) return false;
    mark[u] = true;

    for (auto v: grafo[u])
    {
        if (match[v] == -1 || dfs_match(match[v]))
        {
            match[v] = u, match[u] = v;
            return true;
        }
    }

    return false;
}

int get_match(int n)
{
    memset(match, -1, sizeof match);

    int ans = 0;
    for (int i = 0; i < n; i++)
    {
        memset(mark, 0, sizeof mark);

        if (match[i] == -1 && dfs_match(i)) ans++;
    }

    return ans;
}
 
int findSample(int n, int confidence[], int host[], int protocol[])
{
	bool sub5 = 1;
	for (int i = 0; i < n; i++)
	{
		c[i] = confidence[i];

		if (c[i] != 1 || (i > 0 && protocol[i] == 2)) sub5 = 0;
	}

	if (sub5)
	{	
		for (int i = 1; i < n; i++)
		{
			int h = host[i];
	 
			if (protocol[i] == 0)
			{
				connected[i][h] = connected[h][i] = 1;
				grafo[i].push_back(h); grafo[h].push_back(i);
			}
			else
			{
				for (int j = 0; j < n; j++)
				{
					if (connected[h][j])
					{
						connected[i][j] = connected[j][i] = 1;
						grafo[i].push_back(j), grafo[j].push_back(i);
					}
				}
			}
		}

		return n-get_match(n);
	}
 
	if (n > 10)
	{
		if (protocol[1] == 2)
		{
			int mx = 0;
			for (int i = 0; i < n; i++)
				mx = max(mx, c[i]);
			return mx;
		}
		else if (protocol[1] == 1)
		{
			int s = 0;
			for (int i = 0; i < n; i++)
				s += c[i];
 
			return s;
		}
		else
		{
			memset(dp, -1, sizeof dp);
 
			for (int i = 1; i < n; i++)
				grafo[i].push_back(host[i]), grafo[host[i]].push_back(i);
 
			return max(dfs(0, -1, 0), dfs(0, -1, 1));
		}
	}
 
	for (int i = 1; i < n; i++)
	{
		int h = host[i];
 
		if (protocol[i] == 0)
		{
			connected[i][h] = connected[h][i] = 1;
		}
		else if (protocol[i] == 1)
		{
			for (int j = 0; j < n; j++)
				if (connected[h][j])
					connected[i][j] = connected[j][i] = 1;
		}
		else
		{
			for (int j = 0; j < n; j++)
				if (connected[h][j])
					connected[i][j] = connected[j][i] = 1;
			connected[i][h] = connected[h][i] = 1;
		}
	}
 
	int ans = 0;
 
	for (int mask = 0; mask < (1<<n); mask++)
	{
		bool ok = 1;
		for (int i = 0; i < n; i++)
			for (int j = 0; j < n; j++)
				if (connected[i][j] && mask&(1<<i) && mask&(1<<j))
					ok = 0;
 
		if (ok)
		{
			int aux = 0;
			for (int i = 0; i < n; i++)
				if (mask&(1<<i))
					aux += c[i];
			ans = max(ans, aux);
		}
	}
 
	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 3 ms 332 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 3 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 5 ms 1536 KB Output is correct
6 Correct 3 ms 512 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
13 Correct 3 ms 384 KB Output is correct
14 Correct 3 ms 384 KB Output is correct
15 Correct 3 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 3 ms 356 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 2 ms 412 KB Output is correct
9 Correct 5 ms 1408 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 3 ms 896 KB Output is correct
12 Correct 4 ms 1036 KB Output is correct
13 Correct 3 ms 768 KB Output is correct
14 Correct 4 ms 1408 KB Output is correct
15 Correct 3 ms 896 KB Output is correct
16 Correct 3 ms 896 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 2 ms 640 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 5 ms 1152 KB Output is correct
21 Correct 5 ms 1408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 3 ms 384 KB Output is correct
12 Runtime error 55 ms 8028 KB Execution killed with signal 11 (could be triggered by violating memory limits)
13 Halted 0 ms 0 KB -