Submission #1049033

# Submission time Handle Problem Language Result Execution time Memory
1049033 2024-08-08T12:16:45 Z crafticat Power Plant (JOI20_power) C++17
47 / 100
1500 ms 16088 KB
#include <bits/stdc++.h>

using namespace std;

#include <bits/stdc++.h>

#include <utility>

using namespace std;

using ll = long long;
using pi = pair<int,int>;
using pl = pair<ll,ll>;

#define temp(T) template<class T>
temp(T) using V = vector<T>;
using vi = V<int>;
using vl = V<ll>;
using vvi = V<vi>;
using vpi = V<pi>;
using vb = V<bool>;
using vvb = V<vb>;
vi dx = {0,1,-1,0};
vi dy = {1,0,0,-1};

#define sz(x) x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define pb push_back
#define eb emplace_back
#define f first
#define s second

#define lb lower_bound
#define ub upper_bound

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define rep(a) F0R(_,a)
#define each(a,x) for (auto& a: x)

using str = string;

const int MOD = 1e9 + 7;
const ll BIG = 1e18;
const int INF = 1e9 + 2;

temp(T) bool ckmin(T& a, const T& b) {
    return b < a ? a = b, 1 : 0; } // set a = min(a,b)
temp(T) bool ckmax(T& a, const T& b) {
    return a < b ? a = b, 1 : 0; } // set a = max(a,b)

void unsyncIO() { cin.tie(0)->sync_with_stdio(0); }
// FILE I/O
void setIn(str s) { freopen(s.c_str(),"r",stdin); }
void setOut(str s) { freopen(s.c_str(),"w",stdout); }
void setIO(str s = "") {
    unsyncIO();
    if (sz(s)) setIn(s+".in"), setOut(s+".out"); // for USACO
}

#define read(x) int x; cin >> x

temp(T) void pr(T x) {
    cout << to_string(x);
}
temp(T) void prln(T x) {
    cout << to_string(x) << "\n";
}


vi readV(int n) {
    vi input(n);
    F0R(i,n)
        cin >> input[i];
    return input;
}

vi dp2;
vi up;
vvi g;
string active;

void dfs(int x, int p) {
    each(y, g[x]) {
        if (y == p) continue;
        dfs(y, x);
        if (active[y] == '1') dp2[x] += 1;
        else dp2[x] += dp2[y];
    }
}

void dfs2(int x, int p) {
    if (p != -1) {
        if (active[p] == '1') up[x] = 1;
        else up[x] = dp2[p] - (active[x] == '1' ? 1 : dp2[x]) + up[x];
    }

    each(y, g[x]) {
        if (y == p) continue;
        dfs2(y, x);
    }
}
int test(int x, int p) {
    int ans = 0;
    each(y, g[x]) {
        if (y == p) continue;
        int cs = test(y, x);
        if (active[y] == '1') { cs--; if (cs < 1) cs = 1;}
        ans += cs;
    }
    return ans;
}

int main() {
    setIO();

    read(n);

    g.rsz(n + 1);
    F0R(i, n -1) {
        int a, b; cin >> a >> b;
        g[a].pb(b);
        g[b].pb(a);
    }

    cin >> active;
    active = " " + active;
    dp2.rsz(n + 1);
    up.rsz(n + 1);

    dfs(1, -1);
    dfs2(1, -1);

    int ans =0;
    FOR(i, 1, n + 1) {
        int sum = test(i, -1);
        if (active[i] == '1' and sum > 2) sum--;
        if (active[i] == '1' and sum == 0) sum = 1;
        ckmax(ans, sum);
    }

    cout << ans;

    return 0;
}

Compilation message

power.cpp: In function 'void setIn(str)':
power.cpp:60:28: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   60 | void setIn(str s) { freopen(s.c_str(),"r",stdin); }
      |                     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
power.cpp: In function 'void setOut(str)':
power.cpp:61:29: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   61 | void setOut(str s) { freopen(s.c_str(),"w",stdout); }
      |                      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 604 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 604 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 24 ms 604 KB Output is correct
23 Correct 22 ms 600 KB Output is correct
24 Correct 23 ms 604 KB Output is correct
25 Correct 21 ms 608 KB Output is correct
26 Correct 21 ms 600 KB Output is correct
27 Correct 22 ms 460 KB Output is correct
28 Correct 21 ms 604 KB Output is correct
29 Correct 37 ms 604 KB Output is correct
30 Correct 38 ms 600 KB Output is correct
31 Correct 20 ms 584 KB Output is correct
32 Correct 20 ms 348 KB Output is correct
33 Correct 25 ms 352 KB Output is correct
34 Correct 24 ms 604 KB Output is correct
35 Correct 25 ms 604 KB Output is correct
36 Correct 24 ms 600 KB Output is correct
37 Correct 17 ms 612 KB Output is correct
38 Correct 19 ms 604 KB Output is correct
39 Correct 26 ms 604 KB Output is correct
40 Correct 27 ms 676 KB Output is correct
41 Correct 26 ms 604 KB Output is correct
42 Correct 32 ms 600 KB Output is correct
43 Correct 24 ms 600 KB Output is correct
44 Correct 29 ms 604 KB Output is correct
45 Correct 25 ms 600 KB Output is correct
46 Correct 25 ms 640 KB Output is correct
47 Correct 25 ms 604 KB Output is correct
48 Correct 25 ms 604 KB Output is correct
49 Correct 25 ms 600 KB Output is correct
50 Correct 25 ms 604 KB Output is correct
51 Correct 25 ms 604 KB Output is correct
52 Correct 27 ms 848 KB Output is correct
53 Correct 21 ms 600 KB Output is correct
54 Correct 25 ms 644 KB Output is correct
55 Correct 24 ms 604 KB Output is correct
56 Correct 12 ms 472 KB Output is correct
57 Correct 37 ms 744 KB Output is correct
58 Correct 12 ms 616 KB Output is correct
59 Correct 11 ms 616 KB Output is correct
60 Correct 37 ms 604 KB Output is correct
61 Correct 11 ms 604 KB Output is correct
62 Correct 38 ms 604 KB Output is correct
63 Correct 17 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 604 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 24 ms 604 KB Output is correct
23 Correct 22 ms 600 KB Output is correct
24 Correct 23 ms 604 KB Output is correct
25 Correct 21 ms 608 KB Output is correct
26 Correct 21 ms 600 KB Output is correct
27 Correct 22 ms 460 KB Output is correct
28 Correct 21 ms 604 KB Output is correct
29 Correct 37 ms 604 KB Output is correct
30 Correct 38 ms 600 KB Output is correct
31 Correct 20 ms 584 KB Output is correct
32 Correct 20 ms 348 KB Output is correct
33 Correct 25 ms 352 KB Output is correct
34 Correct 24 ms 604 KB Output is correct
35 Correct 25 ms 604 KB Output is correct
36 Correct 24 ms 600 KB Output is correct
37 Correct 17 ms 612 KB Output is correct
38 Correct 19 ms 604 KB Output is correct
39 Correct 26 ms 604 KB Output is correct
40 Correct 27 ms 676 KB Output is correct
41 Correct 26 ms 604 KB Output is correct
42 Correct 32 ms 600 KB Output is correct
43 Correct 24 ms 600 KB Output is correct
44 Correct 29 ms 604 KB Output is correct
45 Correct 25 ms 600 KB Output is correct
46 Correct 25 ms 640 KB Output is correct
47 Correct 25 ms 604 KB Output is correct
48 Correct 25 ms 604 KB Output is correct
49 Correct 25 ms 600 KB Output is correct
50 Correct 25 ms 604 KB Output is correct
51 Correct 25 ms 604 KB Output is correct
52 Correct 27 ms 848 KB Output is correct
53 Correct 21 ms 600 KB Output is correct
54 Correct 25 ms 644 KB Output is correct
55 Correct 24 ms 604 KB Output is correct
56 Correct 12 ms 472 KB Output is correct
57 Correct 37 ms 744 KB Output is correct
58 Correct 12 ms 616 KB Output is correct
59 Correct 11 ms 616 KB Output is correct
60 Correct 37 ms 604 KB Output is correct
61 Correct 11 ms 604 KB Output is correct
62 Correct 38 ms 604 KB Output is correct
63 Correct 17 ms 604 KB Output is correct
64 Execution timed out 1556 ms 16088 KB Time limit exceeded
65 Halted 0 ms 0 KB -