답안 #955631

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
955631 2024-03-31T07:26:45 Z Neco_arc Power Plant (JOI20_power) C++17
100 / 100
134 ms 33968 KB
#include <bits/stdc++.h>

#define ll long long
#define ull unsigned long long
#define all(x) x.begin(), x.end()
#define Neco "Power Plant"
#define resp(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
#define getbit(x,i) ((x >> i)&1)
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
#define cntbit(x) __builtin_popcountll(x)
#define fi(i, a, b) for(int i = a; i <= b; i++)
#define fid(i, a, b) for(int i = a; i >= b; i--)
#define maxn (int) 2e5 + 7

using namespace std;

const ll mod = 1e9 + 7; //972663749
const ll base = 911382323;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

ll GetRandom(ll l, ll r)
{
    return uniform_int_distribution<ll> (l, r)(rng);
}

int n;
int cl[maxn];
vector<int> edges[maxn];

namespace Sub1
{
    int a[maxn];

    int calc(int u, int par) {
        int ans = 0;
        for(int v : edges[u]) if(v != par) ans += calc(v, u) > 0;
        if(a[u]) ans = max(ans, 1);
        return ans;
    }

    int solve()
    {
        int ans = 1;
        fi(x, 1, (1 << n) - 1)
        {
            fi(i, 1, n) {
                a[i] = 0;
                if(getbit(x, i - 1)) a[i] = cl[i];
            }

            int sum = 0;
            fi(i, 1, n) if(cl[i])
            {
                int w = calc(i, 0);
                if(w > 1) sum --;
                else sum += (w == 1) && a[i];
            }
            ans = max(ans, sum);
        }
        return ans;
    }

}

namespace Sub2
{
    int res = 1;
    int dp[maxn];

    void dfs(int u, int par)
    {
        vector<int> P;
        for(int v : edges[u]) if(v != par) {
            dfs(v, u);
            P.push_back(dp[v]);
        }

        sort(all(P), greater<int>());

        int ans = cl[u];
        int sum = 0;

        if(P.size() >= 1)
        {
            res = max(res, P[0] + cl[u]), ans = max(ans, P[0] + cl[u]);

            fi(i, 0, P.size() - 1) {
                sum += P[i];
                if(i > 0) res = max(res, sum - cl[u]), ans = max(ans, sum - cl[u]);
            }
            dp[u] = sum;
        }

        if(cl[u]) dp[u] = max(dp[u] - 1, 1);
    }

    int solve()
    {
        res = 1;
        fi(i, 1, n) dp[i] = 0;
        dfs(1, 0);
        return res;
    }

}


void Input()
{
    cin >> n;

    fi(i, 1, n - 1) {
        int x, y;
        cin >> x >> y;
        edges[x].push_back(y), edges[y].push_back(x);
    }

    fi(i, 1, n) {
        char x; cin >> x;
        cl[i] = x - '0';
    }
}

pair<int, int> E[maxn];
void gen()
{
    n = GetRandom(2, 16);

    fi(i, 1, n) edges[i].clear();

    fi(i, 1, n - 1) {
        int x = i + 1, y = GetRandom(1, i);
        edges[x].push_back(y), edges[y].push_back(x);
        E[i] = {x, y};
    }

    fi(i, 1, n) cl[i] = GetRandom(0, 1);
}

void Print()
{
    cout << n << '\n';
    fi(i, 1, n - 1) {
        cout << E[i].first << " " << E[i].second << '\n';
    }
    fi(i, 1, n) cout << cl[i];
    cout << '\n';
    cout << "-----------------------\n";
}


void solve()
{

    Input();
//    gen();

cout << Sub2::solve();
////        cout << Sub1::solve() << " " << Sub2::solve();
//
//    if(Sub1::solve() != Sub2::solve()) {
//        cout << Sub1::solve() << " " << Sub2::solve();
//        Print();
//        exit(0);
//    } else cerr << "OK\n";


}


int main()
{

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    if(fopen(Neco".inp", "r")) {
        freopen(Neco".inp", "r", stdin);
        freopen(Neco".out", "w", stdout);
    }


    int nTest = 1;
//    cin >> nTest;


    while(nTest--)
    {
        solve();
    }


    return 0;
}

Compilation message

power.cpp: In function 'int Sub1::solve()':
power.cpp:50:32: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
   50 |                 if(getbit(x, i - 1)) a[i] = cl[i];
      |                              ~~^~~
power.cpp:8:28: note: in definition of macro 'getbit'
    8 | #define getbit(x,i) ((x >> i)&1)
      |                            ^
power.cpp: In function 'void Sub2::dfs(int, int)':
power.cpp:12:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   12 | #define fi(i, a, b) for(int i = a; i <= b; i++)
......
   89 |             fi(i, 0, P.size() - 1) {
      |                ~~~~~~~~~~~~~~~~~~     
power.cpp:89:13: note: in expansion of macro 'fi'
   89 |             fi(i, 0, P.size() - 1) {
      |             ^~
power.cpp: In function 'int main()':
power.cpp:181:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  181 |         freopen(Neco".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
power.cpp:182:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  182 |         freopen(Neco".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8536 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 2 ms 8536 KB Output is correct
4 Correct 3 ms 8540 KB Output is correct
5 Correct 2 ms 8540 KB Output is correct
6 Correct 2 ms 8540 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 2 ms 8540 KB Output is correct
9 Correct 2 ms 8636 KB Output is correct
10 Correct 2 ms 8580 KB Output is correct
11 Correct 2 ms 8536 KB Output is correct
12 Correct 1 ms 8540 KB Output is correct
13 Correct 2 ms 8536 KB Output is correct
14 Correct 2 ms 8540 KB Output is correct
15 Correct 2 ms 8536 KB Output is correct
16 Correct 2 ms 8540 KB Output is correct
17 Correct 3 ms 8540 KB Output is correct
18 Correct 2 ms 8540 KB Output is correct
19 Correct 2 ms 8664 KB Output is correct
20 Correct 2 ms 8540 KB Output is correct
21 Correct 2 ms 8540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8536 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 2 ms 8536 KB Output is correct
4 Correct 3 ms 8540 KB Output is correct
5 Correct 2 ms 8540 KB Output is correct
6 Correct 2 ms 8540 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 2 ms 8540 KB Output is correct
9 Correct 2 ms 8636 KB Output is correct
10 Correct 2 ms 8580 KB Output is correct
11 Correct 2 ms 8536 KB Output is correct
12 Correct 1 ms 8540 KB Output is correct
13 Correct 2 ms 8536 KB Output is correct
14 Correct 2 ms 8540 KB Output is correct
15 Correct 2 ms 8536 KB Output is correct
16 Correct 2 ms 8540 KB Output is correct
17 Correct 3 ms 8540 KB Output is correct
18 Correct 2 ms 8540 KB Output is correct
19 Correct 2 ms 8664 KB Output is correct
20 Correct 2 ms 8540 KB Output is correct
21 Correct 2 ms 8540 KB Output is correct
22 Correct 2 ms 8540 KB Output is correct
23 Correct 2 ms 8764 KB Output is correct
24 Correct 2 ms 8540 KB Output is correct
25 Correct 2 ms 8536 KB Output is correct
26 Correct 2 ms 8540 KB Output is correct
27 Correct 2 ms 8540 KB Output is correct
28 Correct 2 ms 8540 KB Output is correct
29 Correct 2 ms 8796 KB Output is correct
30 Correct 3 ms 8792 KB Output is correct
31 Correct 2 ms 8540 KB Output is correct
32 Correct 2 ms 8536 KB Output is correct
33 Correct 2 ms 8680 KB Output is correct
34 Correct 3 ms 8540 KB Output is correct
35 Correct 2 ms 8540 KB Output is correct
36 Correct 2 ms 8540 KB Output is correct
37 Correct 2 ms 8540 KB Output is correct
38 Correct 3 ms 8540 KB Output is correct
39 Correct 2 ms 8796 KB Output is correct
40 Correct 2 ms 8796 KB Output is correct
41 Correct 2 ms 8796 KB Output is correct
42 Correct 3 ms 8796 KB Output is correct
43 Correct 3 ms 8540 KB Output is correct
44 Correct 2 ms 8796 KB Output is correct
45 Correct 2 ms 8540 KB Output is correct
46 Correct 2 ms 8536 KB Output is correct
47 Correct 3 ms 8540 KB Output is correct
48 Correct 2 ms 8796 KB Output is correct
49 Correct 2 ms 8796 KB Output is correct
50 Correct 2 ms 8796 KB Output is correct
51 Correct 2 ms 8796 KB Output is correct
52 Correct 3 ms 8536 KB Output is correct
53 Correct 2 ms 8796 KB Output is correct
54 Correct 2 ms 8796 KB Output is correct
55 Correct 2 ms 8540 KB Output is correct
56 Correct 2 ms 8760 KB Output is correct
57 Correct 2 ms 8788 KB Output is correct
58 Correct 2 ms 8540 KB Output is correct
59 Correct 3 ms 8540 KB Output is correct
60 Correct 2 ms 8688 KB Output is correct
61 Correct 2 ms 8540 KB Output is correct
62 Correct 2 ms 8796 KB Output is correct
63 Correct 3 ms 8796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8536 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 2 ms 8536 KB Output is correct
4 Correct 3 ms 8540 KB Output is correct
5 Correct 2 ms 8540 KB Output is correct
6 Correct 2 ms 8540 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 2 ms 8540 KB Output is correct
9 Correct 2 ms 8636 KB Output is correct
10 Correct 2 ms 8580 KB Output is correct
11 Correct 2 ms 8536 KB Output is correct
12 Correct 1 ms 8540 KB Output is correct
13 Correct 2 ms 8536 KB Output is correct
14 Correct 2 ms 8540 KB Output is correct
15 Correct 2 ms 8536 KB Output is correct
16 Correct 2 ms 8540 KB Output is correct
17 Correct 3 ms 8540 KB Output is correct
18 Correct 2 ms 8540 KB Output is correct
19 Correct 2 ms 8664 KB Output is correct
20 Correct 2 ms 8540 KB Output is correct
21 Correct 2 ms 8540 KB Output is correct
22 Correct 2 ms 8540 KB Output is correct
23 Correct 2 ms 8764 KB Output is correct
24 Correct 2 ms 8540 KB Output is correct
25 Correct 2 ms 8536 KB Output is correct
26 Correct 2 ms 8540 KB Output is correct
27 Correct 2 ms 8540 KB Output is correct
28 Correct 2 ms 8540 KB Output is correct
29 Correct 2 ms 8796 KB Output is correct
30 Correct 3 ms 8792 KB Output is correct
31 Correct 2 ms 8540 KB Output is correct
32 Correct 2 ms 8536 KB Output is correct
33 Correct 2 ms 8680 KB Output is correct
34 Correct 3 ms 8540 KB Output is correct
35 Correct 2 ms 8540 KB Output is correct
36 Correct 2 ms 8540 KB Output is correct
37 Correct 2 ms 8540 KB Output is correct
38 Correct 3 ms 8540 KB Output is correct
39 Correct 2 ms 8796 KB Output is correct
40 Correct 2 ms 8796 KB Output is correct
41 Correct 2 ms 8796 KB Output is correct
42 Correct 3 ms 8796 KB Output is correct
43 Correct 3 ms 8540 KB Output is correct
44 Correct 2 ms 8796 KB Output is correct
45 Correct 2 ms 8540 KB Output is correct
46 Correct 2 ms 8536 KB Output is correct
47 Correct 3 ms 8540 KB Output is correct
48 Correct 2 ms 8796 KB Output is correct
49 Correct 2 ms 8796 KB Output is correct
50 Correct 2 ms 8796 KB Output is correct
51 Correct 2 ms 8796 KB Output is correct
52 Correct 3 ms 8536 KB Output is correct
53 Correct 2 ms 8796 KB Output is correct
54 Correct 2 ms 8796 KB Output is correct
55 Correct 2 ms 8540 KB Output is correct
56 Correct 2 ms 8760 KB Output is correct
57 Correct 2 ms 8788 KB Output is correct
58 Correct 2 ms 8540 KB Output is correct
59 Correct 3 ms 8540 KB Output is correct
60 Correct 2 ms 8688 KB Output is correct
61 Correct 2 ms 8540 KB Output is correct
62 Correct 2 ms 8796 KB Output is correct
63 Correct 3 ms 8796 KB Output is correct
64 Correct 76 ms 17456 KB Output is correct
65 Correct 91 ms 17424 KB Output is correct
66 Correct 81 ms 17488 KB Output is correct
67 Correct 83 ms 17684 KB Output is correct
68 Correct 73 ms 17524 KB Output is correct
69 Correct 72 ms 17564 KB Output is correct
70 Correct 76 ms 17380 KB Output is correct
71 Correct 76 ms 17376 KB Output is correct
72 Correct 79 ms 17548 KB Output is correct
73 Correct 94 ms 32560 KB Output is correct
74 Correct 99 ms 32648 KB Output is correct
75 Correct 73 ms 17412 KB Output is correct
76 Correct 70 ms 17276 KB Output is correct
77 Correct 84 ms 17288 KB Output is correct
78 Correct 91 ms 17236 KB Output is correct
79 Correct 73 ms 17308 KB Output is correct
80 Correct 93 ms 17440 KB Output is correct
81 Correct 82 ms 18420 KB Output is correct
82 Correct 78 ms 18008 KB Output is correct
83 Correct 96 ms 24140 KB Output is correct
84 Correct 120 ms 26800 KB Output is correct
85 Correct 109 ms 26708 KB Output is correct
86 Correct 78 ms 19360 KB Output is correct
87 Correct 83 ms 18612 KB Output is correct
88 Correct 88 ms 18768 KB Output is correct
89 Correct 69 ms 18776 KB Output is correct
90 Correct 83 ms 18692 KB Output is correct
91 Correct 72 ms 18772 KB Output is correct
92 Correct 90 ms 19284 KB Output is correct
93 Correct 82 ms 22684 KB Output is correct
94 Correct 79 ms 24380 KB Output is correct
95 Correct 99 ms 23032 KB Output is correct
96 Correct 76 ms 20564 KB Output is correct
97 Correct 68 ms 18768 KB Output is correct
98 Correct 67 ms 18104 KB Output is correct
99 Correct 65 ms 17748 KB Output is correct
100 Correct 72 ms 21840 KB Output is correct
101 Correct 70 ms 19368 KB Output is correct
102 Correct 70 ms 18364 KB Output is correct
103 Correct 90 ms 18256 KB Output is correct
104 Correct 49 ms 19804 KB Output is correct
105 Correct 92 ms 28888 KB Output is correct
106 Correct 61 ms 19652 KB Output is correct
107 Correct 57 ms 19564 KB Output is correct
108 Correct 110 ms 33620 KB Output is correct
109 Correct 50 ms 19912 KB Output is correct
110 Correct 134 ms 33968 KB Output is correct
111 Correct 68 ms 17692 KB Output is correct