Submission #115780

# Submission time Handle Problem Language Result Execution time Memory
115780 2019-06-09T05:47:26 Z thecodingwizard Friend (IOI14_friend) C++11
46 / 100
119 ms 5880 KB
#include <bits/stdc++.h>
#include "friend.h"

using namespace std;

#define FOR(a, b, c) for (int a = b; a < c; a++)
#define ll long long
#define vi vector<int>
#define pb push_back
#define mp make_pair

int memo[1000][2];
bool friends[1000][1000];
bool visited[1000];
int *A;
int n;
void dfsVisited(int u, int p) {
	visited[u] = true;
	for (int v = 0; v < n; v++) {
		if (!friends[v][u]) continue;
		if (v == p || v == u) continue;
		dfsVisited(v, u);
	}
}
int run(int u, int canTake, int p) {
	if (memo[u][canTake] != -1) return memo[u][canTake];
	int ans = 0;
	// Option 1: Don't take u
	int opAns = 0;
	for (int v = 0; v < n; v++) {
		if (!friends[v][u]) continue;
		if (v == p || v == u) continue;
		opAns += run(v, 1, u);
	}
	ans = opAns;

	// Option 2: Take u
	if (canTake) {
		opAns = A[u];
		for (int v = 0; v < n; v++) {
			if (!friends[v][u]) continue;
			if (v == p || v == u) continue;
			opAns += run(v, 0, u);
		}
		ans = max(ans, opAns);
	}
	return memo[u][canTake] = ans;
}

int match[1000];
int aug(int u) {
    if (visited[u]) return 0;
    visited[u] = true;
    for (int v = 0; v < n; v++) {
        if (!friends[u][v] || u == v) continue;
        if (match[v] == -1 || aug(match[v])) {
            match[v] = u; return 1;
        }
    }
    return 0;
}

// Find out best sample
int findSample(int n2, int confidence[], int host[], int protocol[]) {
	n = n2;
	FOR(i, 0, n) FOR(j, 0, n) friends[i][j] = false;

	A = confidence;
	FOR(i, 1, n) {
		int h = host[i], p = protocol[i];
		if (p == 0) {
			// IAmYourFriend
			friends[i][h] = true;
			friends[h][i] = true;
		} else if (p == 1) {
			// MyFriendsAreYourFriends
			for (int u = 0; u < n; u++) {
				if (!friends[u][h]) continue;
				friends[u][i] = true;
				friends[i][u] = true;
			}
		} else {
			// WeAreYourFriends
			friends[i][h] = true;
			friends[h][i] = true;
			for (int u = 0; u < n; u++) {
				if (!friends[u][h]) continue;
				friends[u][i] = true;
				friends[i][u] = true;
			}
		}
	}

	if (n <= 10) {
		// Subtask 1
		int ans = 0;
		for (int i = 0; i < (1 << n); i++) {
			int curAns = 0;
			bool invalid = false;
			for (int j = 0; j < n; j++) {
				if (!(i & (1 << j))) continue;
				for (int f = 0; f < n; f++) {
					if (!friends[j][f] || j == f) continue;

					if (i & (1 << f)) {
						invalid = true;
						break;
					}
				}
				if (invalid) break;
				curAns += confidence[j];
			}
			if (!invalid) ans = max(ans, curAns);
		}
		return ans;
	} else {
		bool isSubtaskTwo = true;
		for (int i = 1; i < n; i++) {
			if (protocol[i] != 1) isSubtaskTwo = false;
		}
		if (isSubtaskTwo) {
			// Subtask two: Sum all confidence values
			int ans = 0;
			for (int i = 0; i < n; i++) ans += confidence[i];
			return ans;
		}

		bool isSubtaskThree = true;
		for (int i = 1; i < n; i++) {
			if (protocol[i] != 2) isSubtaskThree = false;
		}
		if (isSubtaskThree) {
			// Subtask Three: We Are Your Friends, entire graph is connected
			int ans = 0;
			for (int i = 0; i < n; i++) ans = max(ans, confidence[i]);
			return ans;
		}

        bool isSubtaskFour = true;
        for (int i = 1; i < n; i++) {
            if (protocol[i] != 0) isSubtaskFour = false;
        }
        if (isSubtaskFour) {
            // Subtask Four: Tree
            // Define dp[i][0] = max confidence of subtree of i, cannot take i
            //        dp[i][1] = max confidence of subtree of i, can take i
            for (int i = 0; i < n; i++) visited[i] = false;
            for (int i = 0; i < n; i++) memo[i][0] = memo[i][1] = -1;
            int ans = 0;
            for (int i = 0; i < n; i++) {
                if (!visited[i]) {
                    dfsVisited(i, i);
                    ans += run(i, 1, i);
                }
            }
            return ans;
        }

        // Subtask Five: It's a bipartite graph, asking you to find maximum independent set
        int mcbm = 0;
        for (int i = 0; i < n; i++) match[i] = -1;
        for (int i = 0; i < n; i++) {
            for (int i = 0; i < n; i++) visited[i] = false;
            mcbm += aug(i);
        }
        return n - mcbm;
	}
}
# 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 2 ms 384 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 2 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 2 ms 640 KB Output is correct
3 Correct 3 ms 768 KB Output is correct
4 Correct 4 ms 1152 KB Output is correct
5 Correct 5 ms 1280 KB Output is correct
6 Correct 3 ms 768 KB Output is correct
7 Correct 3 ms 768 KB Output is correct
8 Correct 4 ms 1152 KB Output is correct
9 Correct 3 ms 1024 KB Output is correct
10 Correct 5 ms 1280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 740 KB Output is correct
2 Correct 8 ms 1348 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 5 ms 1024 KB Output is correct
5 Correct 7 ms 1280 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 640 KB Output is correct
8 Correct 3 ms 640 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 9 ms 1408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 15 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 8 ms 1408 KB Output is correct
6 Correct 11 ms 1408 KB Output is correct
7 Correct 3 ms 512 KB Output is correct
8 Correct 10 ms 1280 KB Output is correct
9 Correct 14 ms 384 KB Output is correct
10 Correct 3 ms 640 KB Output is correct
11 Correct 3 ms 768 KB Output is correct
12 Correct 9 ms 1280 KB Output is correct
13 Correct 9 ms 1152 KB Output is correct
14 Correct 2 ms 512 KB Output is correct
15 Correct 9 ms 1280 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 5 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 8 ms 1408 KB Output is correct
10 Correct 4 ms 1280 KB Output is correct
11 Incorrect 3 ms 768 KB Output isn't correct
12 Halted 0 ms 0 KB -
# 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 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 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 2 ms 384 KB Output is correct
12 Runtime error 119 ms 5880 KB Execution killed with signal 11 (could be triggered by violating memory limits)
13 Halted 0 ms 0 KB -