답안 #298768

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
298768 2020-09-14T01:49:28 Z Pro_ktmr Power Plant (JOI20_power) C++14
100 / 100
293 ms 29432 KB
#pragma GCC target ("avx2")
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize ("O3")
#include "bits/stdc++.h"
#include <unordered_set>
#include <unordered_map>
#include <random>
using namespace std;
typedef long long ll;
const ll MOD = 1'000'000'007LL; /*998'244'353LL;*/
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
const int dx[4]={ 1,0,-1,0 };
const int dy[4]={ 0,1,0,-1 };

int N;
int A[200000], B[200000];
vector<pair<int,int>> E[200000];
bool gen[200000];

bool SUM[200000] = {};
int sum[200000];

int dp[400000];
int solve(int n, int p, int e){
    if(dp[e] != -1) return dp[e];
    if(gen[n]){
        dp[e] = 1;
        int tmp = -1;
        if(SUM[n]) tmp += sum[n] - solve(p, n, e%N + N*((e/N)^1));
        else{
            rep(i, E[n].size()){
                if(E[n][i].first == p) continue;
                tmp += solve(E[n][i].first, n, E[n][i].second);
            }
        }
        dp[e] = max(dp[e], tmp);
    }
    else{
        dp[e] = 0;
        int tmp = 0;
        if(SUM[n]) tmp += sum[n] - solve(p, n, e%N + N*((e/N)^1));
        else{
            rep(i, E[n].size()){
                if(E[n][i].first == p) continue;
                tmp += solve(E[n][i].first, n, E[n][i].second);
            }
        }
        dp[e] = max(dp[e], tmp);
    }
    return dp[e];
}

void dfs(int n, int p){
    sum[n] = 0;
    rep(i, E[n].size()){
        sum[n] += solve(E[n][i].first, n, E[n][i].second);
    }
    SUM[n] = true;
    rep(i, E[n].size()){
        if(E[n][i].first == p) continue;
        dfs(E[n][i].first, n);
    }
}

signed main(){
    scanf("%d", &N);
    rep(i, N-1){
        scanf("%d%d", &A[i], &B[i]);
        A[i]--; B[i]--;
        E[A[i]].pb({B[i],i});
        E[B[i]].pb({A[i],N+i});
    }
    rep(i, N){
        char C;
        scanf(" %c", &C);
        gen[i] = C-'0';
    }

    memset(dp, -1, sizeof(dp));
    rep(i, E[0].size()) solve(E[0][i].first, 0, E[0][i].second);
    dfs(0, -1);

    int ans = 0;
    rep(i, N){
        if(!gen[i]) continue;
        int tmp = 0;
        rep(j, E[i].size()){
            tmp = max(tmp, solve(E[i][j].first, i, E[i][j].second));
        }
        ans = max(ans, tmp+1);
    }
    printf("%d\n", ans);
}

Compilation message

power.cpp: In function 'int solve(int, int, int)':
power.cpp:14:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   14 | #define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
      |                           ^
power.cpp:34:13: note: in expansion of macro 'rep'
   34 |             rep(i, E[n].size()){
      |             ^~~
power.cpp:14:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   14 | #define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
      |                                  ~~~^~~~
power.cpp:34:13: note: in expansion of macro 'rep'
   34 |             rep(i, E[n].size()){
      |             ^~~
power.cpp:14:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   14 | #define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
      |                           ^
power.cpp:46:13: note: in expansion of macro 'rep'
   46 |             rep(i, E[n].size()){
      |             ^~~
power.cpp:14:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   14 | #define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
      |                                  ~~~^~~~
power.cpp:46:13: note: in expansion of macro 'rep'
   46 |             rep(i, E[n].size()){
      |             ^~~
power.cpp: In function 'void dfs(int, int)':
power.cpp:14:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   14 | #define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
      |                           ^
power.cpp:58:5: note: in expansion of macro 'rep'
   58 |     rep(i, E[n].size()){
      |     ^~~
power.cpp:14:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   14 | #define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
      |                                  ~~~^~~~
power.cpp:58:5: note: in expansion of macro 'rep'
   58 |     rep(i, E[n].size()){
      |     ^~~
power.cpp:14:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   14 | #define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
      |                           ^
power.cpp:62:5: note: in expansion of macro 'rep'
   62 |     rep(i, E[n].size()){
      |     ^~~
power.cpp:14:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   14 | #define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
      |                                  ~~~^~~~
power.cpp:62:5: note: in expansion of macro 'rep'
   62 |     rep(i, E[n].size()){
      |     ^~~
power.cpp: In function 'int main()':
power.cpp:14:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   14 | #define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
      |                           ^
power.cpp:70:5: note: in expansion of macro 'rep'
   70 |     rep(i, N-1){
      |     ^~~
power.cpp:14:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   14 | #define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
      |                           ^
power.cpp:76:5: note: in expansion of macro 'rep'
   76 |     rep(i, N){
      |     ^~~
power.cpp:14:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   14 | #define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
      |                           ^
power.cpp:83:5: note: in expansion of macro 'rep'
   83 |     rep(i, E[0].size()) solve(E[0][i].first, 0, E[0][i].second);
      |     ^~~
power.cpp:14:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   14 | #define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
      |                                  ~~~^~~~
power.cpp:83:5: note: in expansion of macro 'rep'
   83 |     rep(i, E[0].size()) solve(E[0][i].first, 0, E[0][i].second);
      |     ^~~
power.cpp:14:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   14 | #define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
      |                           ^
power.cpp:87:5: note: in expansion of macro 'rep'
   87 |     rep(i, N){
      |     ^~~
power.cpp:14:27: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   14 | #define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
      |                           ^
power.cpp:90:9: note: in expansion of macro 'rep'
   90 |         rep(j, E[i].size()){
      |         ^~~
power.cpp:14:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   14 | #define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
      |                                  ~~~^~~~
power.cpp:90:9: note: in expansion of macro 'rep'
   90 |         rep(j, E[i].size()){
      |         ^~~
power.cpp:69:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   69 |     scanf("%d", &N);
      |     ~~~~~^~~~~~~~~~
power.cpp:71:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   71 |         scanf("%d%d", &A[i], &B[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~
power.cpp:78:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   78 |         scanf(" %c", &C);
      |         ~~~~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 6656 KB Output is correct
2 Correct 5 ms 6656 KB Output is correct
3 Correct 5 ms 6656 KB Output is correct
4 Correct 5 ms 6784 KB Output is correct
5 Correct 5 ms 6656 KB Output is correct
6 Correct 5 ms 6656 KB Output is correct
7 Correct 5 ms 6656 KB Output is correct
8 Correct 5 ms 6656 KB Output is correct
9 Correct 5 ms 6656 KB Output is correct
10 Correct 5 ms 6656 KB Output is correct
11 Correct 5 ms 6656 KB Output is correct
12 Correct 5 ms 6656 KB Output is correct
13 Correct 5 ms 6656 KB Output is correct
14 Correct 5 ms 6656 KB Output is correct
15 Correct 5 ms 6656 KB Output is correct
16 Correct 5 ms 6656 KB Output is correct
17 Correct 5 ms 6656 KB Output is correct
18 Correct 5 ms 6656 KB Output is correct
19 Correct 5 ms 6656 KB Output is correct
20 Correct 5 ms 6656 KB Output is correct
21 Correct 5 ms 6656 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 6656 KB Output is correct
2 Correct 5 ms 6656 KB Output is correct
3 Correct 5 ms 6656 KB Output is correct
4 Correct 5 ms 6784 KB Output is correct
5 Correct 5 ms 6656 KB Output is correct
6 Correct 5 ms 6656 KB Output is correct
7 Correct 5 ms 6656 KB Output is correct
8 Correct 5 ms 6656 KB Output is correct
9 Correct 5 ms 6656 KB Output is correct
10 Correct 5 ms 6656 KB Output is correct
11 Correct 5 ms 6656 KB Output is correct
12 Correct 5 ms 6656 KB Output is correct
13 Correct 5 ms 6656 KB Output is correct
14 Correct 5 ms 6656 KB Output is correct
15 Correct 5 ms 6656 KB Output is correct
16 Correct 5 ms 6656 KB Output is correct
17 Correct 5 ms 6656 KB Output is correct
18 Correct 5 ms 6656 KB Output is correct
19 Correct 5 ms 6656 KB Output is correct
20 Correct 5 ms 6656 KB Output is correct
21 Correct 5 ms 6656 KB Output is correct
22 Correct 6 ms 6784 KB Output is correct
23 Correct 6 ms 6784 KB Output is correct
24 Correct 6 ms 6784 KB Output is correct
25 Correct 6 ms 6784 KB Output is correct
26 Correct 6 ms 6784 KB Output is correct
27 Correct 6 ms 6784 KB Output is correct
28 Correct 6 ms 6784 KB Output is correct
29 Correct 6 ms 6784 KB Output is correct
30 Correct 7 ms 6784 KB Output is correct
31 Correct 6 ms 6784 KB Output is correct
32 Correct 6 ms 6784 KB Output is correct
33 Correct 6 ms 6784 KB Output is correct
34 Correct 6 ms 6784 KB Output is correct
35 Correct 6 ms 6784 KB Output is correct
36 Correct 6 ms 6784 KB Output is correct
37 Correct 6 ms 6784 KB Output is correct
38 Correct 6 ms 6784 KB Output is correct
39 Correct 6 ms 6784 KB Output is correct
40 Correct 7 ms 6784 KB Output is correct
41 Correct 6 ms 6816 KB Output is correct
42 Correct 6 ms 6784 KB Output is correct
43 Correct 6 ms 6784 KB Output is correct
44 Correct 6 ms 6784 KB Output is correct
45 Correct 6 ms 6784 KB Output is correct
46 Correct 6 ms 6708 KB Output is correct
47 Correct 6 ms 6784 KB Output is correct
48 Correct 6 ms 6784 KB Output is correct
49 Correct 6 ms 6784 KB Output is correct
50 Correct 6 ms 6784 KB Output is correct
51 Correct 6 ms 6784 KB Output is correct
52 Correct 6 ms 6784 KB Output is correct
53 Correct 6 ms 6784 KB Output is correct
54 Correct 6 ms 6784 KB Output is correct
55 Correct 6 ms 6784 KB Output is correct
56 Correct 7 ms 6784 KB Output is correct
57 Correct 6 ms 6784 KB Output is correct
58 Correct 6 ms 6784 KB Output is correct
59 Correct 6 ms 6784 KB Output is correct
60 Correct 7 ms 6784 KB Output is correct
61 Correct 6 ms 6912 KB Output is correct
62 Correct 6 ms 6784 KB Output is correct
63 Correct 6 ms 6912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 6656 KB Output is correct
2 Correct 5 ms 6656 KB Output is correct
3 Correct 5 ms 6656 KB Output is correct
4 Correct 5 ms 6784 KB Output is correct
5 Correct 5 ms 6656 KB Output is correct
6 Correct 5 ms 6656 KB Output is correct
7 Correct 5 ms 6656 KB Output is correct
8 Correct 5 ms 6656 KB Output is correct
9 Correct 5 ms 6656 KB Output is correct
10 Correct 5 ms 6656 KB Output is correct
11 Correct 5 ms 6656 KB Output is correct
12 Correct 5 ms 6656 KB Output is correct
13 Correct 5 ms 6656 KB Output is correct
14 Correct 5 ms 6656 KB Output is correct
15 Correct 5 ms 6656 KB Output is correct
16 Correct 5 ms 6656 KB Output is correct
17 Correct 5 ms 6656 KB Output is correct
18 Correct 5 ms 6656 KB Output is correct
19 Correct 5 ms 6656 KB Output is correct
20 Correct 5 ms 6656 KB Output is correct
21 Correct 5 ms 6656 KB Output is correct
22 Correct 6 ms 6784 KB Output is correct
23 Correct 6 ms 6784 KB Output is correct
24 Correct 6 ms 6784 KB Output is correct
25 Correct 6 ms 6784 KB Output is correct
26 Correct 6 ms 6784 KB Output is correct
27 Correct 6 ms 6784 KB Output is correct
28 Correct 6 ms 6784 KB Output is correct
29 Correct 6 ms 6784 KB Output is correct
30 Correct 7 ms 6784 KB Output is correct
31 Correct 6 ms 6784 KB Output is correct
32 Correct 6 ms 6784 KB Output is correct
33 Correct 6 ms 6784 KB Output is correct
34 Correct 6 ms 6784 KB Output is correct
35 Correct 6 ms 6784 KB Output is correct
36 Correct 6 ms 6784 KB Output is correct
37 Correct 6 ms 6784 KB Output is correct
38 Correct 6 ms 6784 KB Output is correct
39 Correct 6 ms 6784 KB Output is correct
40 Correct 7 ms 6784 KB Output is correct
41 Correct 6 ms 6816 KB Output is correct
42 Correct 6 ms 6784 KB Output is correct
43 Correct 6 ms 6784 KB Output is correct
44 Correct 6 ms 6784 KB Output is correct
45 Correct 6 ms 6784 KB Output is correct
46 Correct 6 ms 6708 KB Output is correct
47 Correct 6 ms 6784 KB Output is correct
48 Correct 6 ms 6784 KB Output is correct
49 Correct 6 ms 6784 KB Output is correct
50 Correct 6 ms 6784 KB Output is correct
51 Correct 6 ms 6784 KB Output is correct
52 Correct 6 ms 6784 KB Output is correct
53 Correct 6 ms 6784 KB Output is correct
54 Correct 6 ms 6784 KB Output is correct
55 Correct 6 ms 6784 KB Output is correct
56 Correct 7 ms 6784 KB Output is correct
57 Correct 6 ms 6784 KB Output is correct
58 Correct 6 ms 6784 KB Output is correct
59 Correct 6 ms 6784 KB Output is correct
60 Correct 7 ms 6784 KB Output is correct
61 Correct 6 ms 6912 KB Output is correct
62 Correct 6 ms 6784 KB Output is correct
63 Correct 6 ms 6912 KB Output is correct
64 Correct 266 ms 19320 KB Output is correct
65 Correct 272 ms 19448 KB Output is correct
66 Correct 265 ms 19576 KB Output is correct
67 Correct 268 ms 19320 KB Output is correct
68 Correct 281 ms 19448 KB Output is correct
69 Correct 273 ms 19448 KB Output is correct
70 Correct 267 ms 19324 KB Output is correct
71 Correct 264 ms 19704 KB Output is correct
72 Correct 263 ms 19448 KB Output is correct
73 Correct 292 ms 28536 KB Output is correct
74 Correct 285 ms 28536 KB Output is correct
75 Correct 266 ms 20600 KB Output is correct
76 Correct 270 ms 20732 KB Output is correct
77 Correct 266 ms 19320 KB Output is correct
78 Correct 266 ms 19324 KB Output is correct
79 Correct 264 ms 19448 KB Output is correct
80 Correct 274 ms 19448 KB Output is correct
81 Correct 231 ms 19592 KB Output is correct
82 Correct 252 ms 19568 KB Output is correct
83 Correct 293 ms 23476 KB Output is correct
84 Correct 284 ms 25080 KB Output is correct
85 Correct 280 ms 24952 KB Output is correct
86 Correct 271 ms 20472 KB Output is correct
87 Correct 271 ms 20088 KB Output is correct
88 Correct 260 ms 20216 KB Output is correct
89 Correct 258 ms 20088 KB Output is correct
90 Correct 253 ms 19960 KB Output is correct
91 Correct 252 ms 20088 KB Output is correct
92 Correct 260 ms 20600 KB Output is correct
93 Correct 244 ms 22000 KB Output is correct
94 Correct 247 ms 23272 KB Output is correct
95 Correct 242 ms 22376 KB Output is correct
96 Correct 261 ms 21148 KB Output is correct
97 Correct 262 ms 19832 KB Output is correct
98 Correct 258 ms 19704 KB Output is correct
99 Correct 265 ms 19448 KB Output is correct
100 Correct 270 ms 21880 KB Output is correct
101 Correct 269 ms 20220 KB Output is correct
102 Correct 274 ms 19576 KB Output is correct
103 Correct 281 ms 19716 KB Output is correct
104 Correct 142 ms 19960 KB Output is correct
105 Correct 278 ms 25976 KB Output is correct
106 Correct 146 ms 19820 KB Output is correct
107 Correct 152 ms 19812 KB Output is correct
108 Correct 285 ms 29048 KB Output is correct
109 Correct 145 ms 19940 KB Output is correct
110 Correct 285 ms 29432 KB Output is correct
111 Correct 260 ms 19492 KB Output is correct