제출 #281723

#제출 시각아이디문제언어결과실행 시간메모리
281723SamAnd친구 (IOI14_friend)C++17
46 / 100
278 ms65540 KiB
#include "friend.h"
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;

int n;
int conf[N];
vector<int> g[N];

int dp[N][2];
void dfs(int x, int p)
{
    dp[x][1] = conf[x];
    for (int i = 0; i < g[x].size(); ++i)
    {
        int h = g[x][i];
        if (h == p)
            continue;
        dfs(h, x);
        dp[x][0] += max(dp[h][0], dp[h][1]);
        dp[x][1] += dp[h][0];
    }
}

int q1, q2;
int c[N];
void dfs1(int x, int gg)
{
    c[x] = gg;
    if (gg == 1)
        ++q1;
    else
        ++q2;
    for (int i = 0; i < g[x].size(); ++i)
    {
        int h = g[x][i];
        if (!c[h])
            dfs1(h, 3 - gg);
        else
            assert(c[x] != c[h]);
    }
}

int findSample(int n, int confidence[], int host[], int protocol[])
{
    ::n = n;

    for (int i = 0; i < n; ++i)
        conf[i] = confidence[i];

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

    for (int i = 0; i < n; ++i)
        sort(g[i].begin(), g[i].end());

    bool z = true;
    for (int i = 1; i < n; ++i)
    {
        if (protocol[i] != protocol[1])
        {
            z = false;
            break;
        }
    }

    if (z)
    {
        if (protocol[1] == 1)
        {
            int ans = 0;
            for (int i = 0; i < n; ++i)
                ans += confidence[i];
            return ans;
        }
        if (protocol[1] == 2)
        {
            int ans = 0;
            for (int i = 0; i < n; ++i)
                ans = max(ans, confidence[i]);
            return ans;
        }
        dfs(0, 0);
        return max(dp[0][0], dp[0][1]);
    }

    if (n > 10)
    {
        int ans = 0;
        for (int x = 0; x < n; ++x)
        {
            if (c[x])
                continue;
            q1 = q2 = 0;
            dfs1(x, 1);
            ans += max(q1, q2);
        }
        return ans;
    }

    int ans = 0;
    for (int x = 0; x < (1 << n); ++x)
    {
        int yans = 0;
        bool z = true;
        for (int i = 0; i < n; ++i)
        {
            if (!(x & (1 << i)))
                continue;
            yans += confidence[i];
            for (int j = 0; j < n; ++j)
            {
                if (!(x & (1 << j)))
                    continue;
                if (binary_search(g[i].begin(), g[i].end(), j))
                {
                    z = false;
                    break;
                }
            }
            if (!z)
                break;
        }
        if (z)
            ans = max(ans, yans);
    }
    return ans;
}

컴파일 시 표준 에러 (stderr) 메시지

friend.cpp: In function 'void dfs(int, int)':
friend.cpp:14:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   14 |     for (int i = 0; i < g[x].size(); ++i)
      |                     ~~^~~~~~~~~~~~~
friend.cpp: In function 'void dfs1(int, int)':
friend.cpp:34:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   34 |     for (int i = 0; i < g[x].size(); ++i)
      |                     ~~^~~~~~~~~~~~~
friend.cpp: In function 'int findSample(int, int*, int*, int*)':
friend.cpp:60:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |             for (int j = 0; j < g[host[i]].size(); ++j)
      |                             ~~^~~~~~~~~~~~~~~~~~~
friend.cpp:69:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   69 |             for (int j = 0; j < g[host[i]].size(); ++j)
      |                             ~~^~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...