답안 #311801

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
311801 2020-10-11T19:12:36 Z MarcoMeijer Power Plant (JOI20_power) C++14
100 / 100
309 ms 45752 KB
#include <bits/stdc++.h>
using namespace std;

// macros
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<ll, ll> lll;
typedef tuple<int, int, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<ll> vll;
typedef vector<lll> vlll;
#define REP(a,b,c) for(int a=int(b); a<int(c); a++)
#define RE(a,c) REP(a,0,c)
#define RE1(a,c) REP(a,1,c+1)
#define REI(a,b,c) REP(a,b,c+1)
#define REV(a,b,c) for(int a=int(c-1); a>=int(b); a--)
#define FOR(a,b) for(auto& a : b)
#define all(a) a.begin(), a.end()
#define INF 1e9
#define EPS 1e-9
#define pb push_back
#define popb pop_back
#define fi first
#define se second
#define sz size()
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

// input
template<class T> void IN(T& x) {cin >> x;}
template<class H, class... T> void IN(H& h, T&... t) {IN(h); IN(t...); }

// output
template<class T1, class T2> void OUT(const pair<T1,T2>& x);
template<class T> void OUT(const T& x) {cout << x;}
template<class H, class... T> void OUT(const H& h, const T&... t) {OUT(h); OUT(t...); }
template<class T1, class T2> void OUT(const pair<T1,T2>& x) {OUT(x.fi,' ',x.se);}
template<class... T> void OUTL(const T&... t) {OUT(t..., "\n"); }
template<class H> void OUTLS(const H& h) {OUTL(h); }
template<class H, class... T> void OUTLS(const H& h, const T&... t) {OUT(h,' '); OUTLS(t...); }

//===================//
//  Added libraries  //
//===================//

//===================//
//end added libraries//
//===================//

void program();
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    program();
}


//===================//
//   begin program   //
//===================//

const int MX = 5e5;

int n, m=0;
int a[MX], b[MX];
int rev[MX];
vi adj[MX];
int p[MX];
string s;

int dp[MX];
int sm[MX], skip[MX];
int getAns(int e);
int getSum(int e) {
    int p=a[e], u=b[e];
    if(sm[u] == -1) {
        sm[u] = 0;
        FOR(v,adj[u]) if(b[v] != p) sm[u] += getAns(v);
        skip[u] = p;
    } else if(skip[u] != -1 && skip[u] != p) {
        FOR(v,adj[u]) if(b[v] == skip[u]) sm[u] += getAns(v);
        skip[u] = -1;
    }
    int res = sm[u];
    if(skip[u] != p) res -= getAns(rev[e]);
    return res;
}
int getAns(int e) {
    if(dp[e] != -1) return dp[e];

    int p=a[e], u=b[e];

    dp[e] = 0;
    if(s[u]) {
        dp[e] = getSum(e)-1;
        dp[e] = max(dp[e], 1);
    } else {
        dp[e] = getSum(e);
    }

    return dp[e];
}

void program() {
    IN(n);
    RE(i,n-1) {
        int u, v;
        IN(u,v); u--; v--;
        a[m] = u; b[m] = v; rev[m] = m+1;
        adj[u].pb(m++);
        a[m] = v; b[m] = u; rev[m] = m-1;
        adj[v].pb(m++);
    }
    IN(s); RE(i,n) s[i]-='0';
    RE(i,m) dp[i] = sm[i] = -1;

    int ans = 0;
    RE(u,n) {
        if(!s[u]) continue;
        ans = max(ans, 1);
        FOR(v,adj[u]) ans = max(ans, 1+getAns(v));
    }
    OUTL(ans);
}

Compilation message

power.cpp: In function 'int getAns(int)':
power.cpp:94:9: warning: unused variable 'p' [-Wunused-variable]
   94 |     int p=a[e], u=b[e];
      |         ^
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12160 KB Output is correct
2 Correct 8 ms 12160 KB Output is correct
3 Correct 8 ms 12160 KB Output is correct
4 Correct 10 ms 12160 KB Output is correct
5 Correct 8 ms 12160 KB Output is correct
6 Correct 8 ms 12160 KB Output is correct
7 Correct 8 ms 12160 KB Output is correct
8 Correct 8 ms 12160 KB Output is correct
9 Correct 8 ms 12160 KB Output is correct
10 Correct 9 ms 12160 KB Output is correct
11 Correct 8 ms 12160 KB Output is correct
12 Correct 8 ms 12160 KB Output is correct
13 Correct 8 ms 12160 KB Output is correct
14 Correct 8 ms 12160 KB Output is correct
15 Correct 8 ms 12160 KB Output is correct
16 Correct 8 ms 12160 KB Output is correct
17 Correct 8 ms 12160 KB Output is correct
18 Correct 8 ms 12160 KB Output is correct
19 Correct 8 ms 12160 KB Output is correct
20 Correct 8 ms 12160 KB Output is correct
21 Correct 9 ms 12160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12160 KB Output is correct
2 Correct 8 ms 12160 KB Output is correct
3 Correct 8 ms 12160 KB Output is correct
4 Correct 10 ms 12160 KB Output is correct
5 Correct 8 ms 12160 KB Output is correct
6 Correct 8 ms 12160 KB Output is correct
7 Correct 8 ms 12160 KB Output is correct
8 Correct 8 ms 12160 KB Output is correct
9 Correct 8 ms 12160 KB Output is correct
10 Correct 9 ms 12160 KB Output is correct
11 Correct 8 ms 12160 KB Output is correct
12 Correct 8 ms 12160 KB Output is correct
13 Correct 8 ms 12160 KB Output is correct
14 Correct 8 ms 12160 KB Output is correct
15 Correct 8 ms 12160 KB Output is correct
16 Correct 8 ms 12160 KB Output is correct
17 Correct 8 ms 12160 KB Output is correct
18 Correct 8 ms 12160 KB Output is correct
19 Correct 8 ms 12160 KB Output is correct
20 Correct 8 ms 12160 KB Output is correct
21 Correct 9 ms 12160 KB Output is correct
22 Correct 9 ms 12288 KB Output is correct
23 Correct 9 ms 12288 KB Output is correct
24 Correct 9 ms 12288 KB Output is correct
25 Correct 9 ms 12288 KB Output is correct
26 Correct 9 ms 12288 KB Output is correct
27 Correct 9 ms 12416 KB Output is correct
28 Correct 8 ms 12288 KB Output is correct
29 Correct 9 ms 12416 KB Output is correct
30 Correct 9 ms 12416 KB Output is correct
31 Correct 9 ms 12288 KB Output is correct
32 Correct 10 ms 12288 KB Output is correct
33 Correct 9 ms 12288 KB Output is correct
34 Correct 9 ms 12288 KB Output is correct
35 Correct 9 ms 12288 KB Output is correct
36 Correct 9 ms 12288 KB Output is correct
37 Correct 9 ms 12288 KB Output is correct
38 Correct 9 ms 12288 KB Output is correct
39 Correct 9 ms 12416 KB Output is correct
40 Correct 9 ms 12416 KB Output is correct
41 Correct 9 ms 12356 KB Output is correct
42 Correct 9 ms 12288 KB Output is correct
43 Correct 9 ms 12288 KB Output is correct
44 Correct 9 ms 12288 KB Output is correct
45 Correct 10 ms 12288 KB Output is correct
46 Correct 9 ms 12288 KB Output is correct
47 Correct 9 ms 12416 KB Output is correct
48 Correct 10 ms 12416 KB Output is correct
49 Correct 9 ms 12416 KB Output is correct
50 Correct 9 ms 12288 KB Output is correct
51 Correct 9 ms 12416 KB Output is correct
52 Correct 9 ms 12288 KB Output is correct
53 Correct 9 ms 12288 KB Output is correct
54 Correct 9 ms 12288 KB Output is correct
55 Correct 9 ms 12288 KB Output is correct
56 Correct 9 ms 12288 KB Output is correct
57 Correct 9 ms 12416 KB Output is correct
58 Correct 9 ms 12288 KB Output is correct
59 Correct 9 ms 12288 KB Output is correct
60 Correct 9 ms 12416 KB Output is correct
61 Correct 9 ms 12288 KB Output is correct
62 Correct 9 ms 12416 KB Output is correct
63 Correct 9 ms 12288 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12160 KB Output is correct
2 Correct 8 ms 12160 KB Output is correct
3 Correct 8 ms 12160 KB Output is correct
4 Correct 10 ms 12160 KB Output is correct
5 Correct 8 ms 12160 KB Output is correct
6 Correct 8 ms 12160 KB Output is correct
7 Correct 8 ms 12160 KB Output is correct
8 Correct 8 ms 12160 KB Output is correct
9 Correct 8 ms 12160 KB Output is correct
10 Correct 9 ms 12160 KB Output is correct
11 Correct 8 ms 12160 KB Output is correct
12 Correct 8 ms 12160 KB Output is correct
13 Correct 8 ms 12160 KB Output is correct
14 Correct 8 ms 12160 KB Output is correct
15 Correct 8 ms 12160 KB Output is correct
16 Correct 8 ms 12160 KB Output is correct
17 Correct 8 ms 12160 KB Output is correct
18 Correct 8 ms 12160 KB Output is correct
19 Correct 8 ms 12160 KB Output is correct
20 Correct 8 ms 12160 KB Output is correct
21 Correct 9 ms 12160 KB Output is correct
22 Correct 9 ms 12288 KB Output is correct
23 Correct 9 ms 12288 KB Output is correct
24 Correct 9 ms 12288 KB Output is correct
25 Correct 9 ms 12288 KB Output is correct
26 Correct 9 ms 12288 KB Output is correct
27 Correct 9 ms 12416 KB Output is correct
28 Correct 8 ms 12288 KB Output is correct
29 Correct 9 ms 12416 KB Output is correct
30 Correct 9 ms 12416 KB Output is correct
31 Correct 9 ms 12288 KB Output is correct
32 Correct 10 ms 12288 KB Output is correct
33 Correct 9 ms 12288 KB Output is correct
34 Correct 9 ms 12288 KB Output is correct
35 Correct 9 ms 12288 KB Output is correct
36 Correct 9 ms 12288 KB Output is correct
37 Correct 9 ms 12288 KB Output is correct
38 Correct 9 ms 12288 KB Output is correct
39 Correct 9 ms 12416 KB Output is correct
40 Correct 9 ms 12416 KB Output is correct
41 Correct 9 ms 12356 KB Output is correct
42 Correct 9 ms 12288 KB Output is correct
43 Correct 9 ms 12288 KB Output is correct
44 Correct 9 ms 12288 KB Output is correct
45 Correct 10 ms 12288 KB Output is correct
46 Correct 9 ms 12288 KB Output is correct
47 Correct 9 ms 12416 KB Output is correct
48 Correct 10 ms 12416 KB Output is correct
49 Correct 9 ms 12416 KB Output is correct
50 Correct 9 ms 12288 KB Output is correct
51 Correct 9 ms 12416 KB Output is correct
52 Correct 9 ms 12288 KB Output is correct
53 Correct 9 ms 12288 KB Output is correct
54 Correct 9 ms 12288 KB Output is correct
55 Correct 9 ms 12288 KB Output is correct
56 Correct 9 ms 12288 KB Output is correct
57 Correct 9 ms 12416 KB Output is correct
58 Correct 9 ms 12288 KB Output is correct
59 Correct 9 ms 12288 KB Output is correct
60 Correct 9 ms 12416 KB Output is correct
61 Correct 9 ms 12288 KB Output is correct
62 Correct 9 ms 12416 KB Output is correct
63 Correct 9 ms 12288 KB Output is correct
64 Correct 248 ms 30396 KB Output is correct
65 Correct 237 ms 30136 KB Output is correct
66 Correct 236 ms 30260 KB Output is correct
67 Correct 241 ms 30136 KB Output is correct
68 Correct 232 ms 30116 KB Output is correct
69 Correct 209 ms 30132 KB Output is correct
70 Correct 191 ms 30148 KB Output is correct
71 Correct 187 ms 30148 KB Output is correct
72 Correct 189 ms 30132 KB Output is correct
73 Correct 295 ms 42808 KB Output is correct
74 Correct 291 ms 37944 KB Output is correct
75 Correct 251 ms 30008 KB Output is correct
76 Correct 236 ms 30016 KB Output is correct
77 Correct 260 ms 30004 KB Output is correct
78 Correct 255 ms 30008 KB Output is correct
79 Correct 254 ms 30016 KB Output is correct
80 Correct 221 ms 30008 KB Output is correct
81 Correct 198 ms 30452 KB Output is correct
82 Correct 220 ms 30200 KB Output is correct
83 Correct 284 ms 35260 KB Output is correct
84 Correct 259 ms 37452 KB Output is correct
85 Correct 255 ms 37956 KB Output is correct
86 Correct 237 ms 31032 KB Output is correct
87 Correct 214 ms 31112 KB Output is correct
88 Correct 237 ms 31348 KB Output is correct
89 Correct 235 ms 31304 KB Output is correct
90 Correct 225 ms 31132 KB Output is correct
91 Correct 223 ms 31864 KB Output is correct
92 Correct 219 ms 31656 KB Output is correct
93 Correct 221 ms 34052 KB Output is correct
94 Correct 213 ms 35824 KB Output is correct
95 Correct 219 ms 34676 KB Output is correct
96 Correct 222 ms 32676 KB Output is correct
97 Correct 225 ms 31248 KB Output is correct
98 Correct 221 ms 30696 KB Output is correct
99 Correct 238 ms 30516 KB Output is correct
100 Correct 224 ms 33700 KB Output is correct
101 Correct 226 ms 31668 KB Output is correct
102 Correct 232 ms 30980 KB Output is correct
103 Correct 232 ms 30680 KB Output is correct
104 Correct 105 ms 30952 KB Output is correct
105 Correct 213 ms 45752 KB Output is correct
106 Correct 129 ms 30824 KB Output is correct
107 Correct 138 ms 30828 KB Output is correct
108 Correct 309 ms 45624 KB Output is correct
109 Correct 106 ms 30956 KB Output is correct
110 Correct 289 ms 45624 KB Output is correct
111 Correct 234 ms 30024 KB Output is correct