답안 #292920

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
292920 2020-09-07T14:48:47 Z arnold518 Power Plant (JOI20_power) C++14
100 / 100
311 ms 28536 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 2e5;

int N;
vector<int> adj[MAXN+10];
int T[MAXN+10];

int ans=0;
int dp[MAXN+10];

void dfs(int now, int bef)
{
	dp[now]=0;
	for(int nxt : adj[now])
	{
		if(nxt==bef) continue;
		dfs(nxt, now);
		if(T[nxt]) dp[now]+=max(1, dp[nxt]-1);
		else dp[now]+=dp[nxt];
	}
}

void dfs2(int now, int bef)
{
	if(T[now])
	{
		int v=0;
		for(int nxt : adj[now])
		{
			if(T[nxt]) v=max(v, max(1, dp[nxt]-1));
			else v=max(v, dp[nxt]);
		}
		ans=max(ans, v+1);
	}
	for(int nxt : adj[now])
	{
		if(nxt==bef) continue;

		int p=dp[nxt], q=dp[now];

		if(T[nxt]) dp[now]-=max(1, dp[nxt]-1);
		else dp[now]-=dp[nxt];

		if(T[now]) dp[nxt]+=max(1, dp[now]-1);
		else dp[nxt]+=dp[now];

		dfs2(nxt, now);

		dp[nxt]=p; dp[now]=q;
	}
}

int main()
{
	scanf("%d", &N);
	for(int i=1; i<N; i++)
	{
		int u, v;
		scanf("%d%d", &u, &v);
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	for(int i=1; i<=N; i++) scanf("%1d", &T[i]);

	for(int i=1; i<=N; i++)
	{
		if(!T[i]) continue;
		dfs(i, i);
		dfs2(i, i);
		break;
	}
	printf("%d\n", ans);
}

Compilation message

power.cpp: In function 'int main()':
power.cpp:61:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   61 |  scanf("%d", &N);
      |  ~~~~~^~~~~~~~~~
power.cpp:65:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   65 |   scanf("%d%d", &u, &v);
      |   ~~~~~^~~~~~~~~~~~~~~~
power.cpp:69:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   69 |  for(int i=1; i<=N; i++) scanf("%1d", &T[i]);
      |                          ~~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4992 KB Output is correct
2 Correct 3 ms 4992 KB Output is correct
3 Correct 3 ms 4992 KB Output is correct
4 Correct 4 ms 4992 KB Output is correct
5 Correct 3 ms 4992 KB Output is correct
6 Correct 4 ms 4992 KB Output is correct
7 Correct 4 ms 5120 KB Output is correct
8 Correct 3 ms 4992 KB Output is correct
9 Correct 3 ms 4992 KB Output is correct
10 Correct 4 ms 4992 KB Output is correct
11 Correct 4 ms 4992 KB Output is correct
12 Correct 3 ms 4992 KB Output is correct
13 Correct 4 ms 4992 KB Output is correct
14 Correct 4 ms 4992 KB Output is correct
15 Correct 4 ms 4992 KB Output is correct
16 Correct 4 ms 4992 KB Output is correct
17 Correct 3 ms 4992 KB Output is correct
18 Correct 4 ms 4992 KB Output is correct
19 Correct 3 ms 4992 KB Output is correct
20 Correct 4 ms 4992 KB Output is correct
21 Correct 4 ms 4992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4992 KB Output is correct
2 Correct 3 ms 4992 KB Output is correct
3 Correct 3 ms 4992 KB Output is correct
4 Correct 4 ms 4992 KB Output is correct
5 Correct 3 ms 4992 KB Output is correct
6 Correct 4 ms 4992 KB Output is correct
7 Correct 4 ms 5120 KB Output is correct
8 Correct 3 ms 4992 KB Output is correct
9 Correct 3 ms 4992 KB Output is correct
10 Correct 4 ms 4992 KB Output is correct
11 Correct 4 ms 4992 KB Output is correct
12 Correct 3 ms 4992 KB Output is correct
13 Correct 4 ms 4992 KB Output is correct
14 Correct 4 ms 4992 KB Output is correct
15 Correct 4 ms 4992 KB Output is correct
16 Correct 4 ms 4992 KB Output is correct
17 Correct 3 ms 4992 KB Output is correct
18 Correct 4 ms 4992 KB Output is correct
19 Correct 3 ms 4992 KB Output is correct
20 Correct 4 ms 4992 KB Output is correct
21 Correct 4 ms 4992 KB Output is correct
22 Correct 5 ms 5120 KB Output is correct
23 Correct 5 ms 5120 KB Output is correct
24 Correct 5 ms 5120 KB Output is correct
25 Correct 5 ms 5120 KB Output is correct
26 Correct 5 ms 5120 KB Output is correct
27 Correct 5 ms 5120 KB Output is correct
28 Correct 5 ms 5120 KB Output is correct
29 Correct 5 ms 5248 KB Output is correct
30 Correct 5 ms 5120 KB Output is correct
31 Correct 5 ms 5120 KB Output is correct
32 Correct 5 ms 5120 KB Output is correct
33 Correct 5 ms 5120 KB Output is correct
34 Correct 5 ms 5120 KB Output is correct
35 Correct 5 ms 5120 KB Output is correct
36 Correct 5 ms 5120 KB Output is correct
37 Correct 5 ms 5120 KB Output is correct
38 Correct 5 ms 5120 KB Output is correct
39 Correct 5 ms 5120 KB Output is correct
40 Correct 5 ms 5120 KB Output is correct
41 Correct 5 ms 5120 KB Output is correct
42 Correct 6 ms 5120 KB Output is correct
43 Correct 5 ms 5120 KB Output is correct
44 Correct 6 ms 5120 KB Output is correct
45 Correct 5 ms 5120 KB Output is correct
46 Correct 5 ms 5120 KB Output is correct
47 Correct 5 ms 5120 KB Output is correct
48 Correct 5 ms 5120 KB Output is correct
49 Correct 5 ms 5120 KB Output is correct
50 Correct 5 ms 5120 KB Output is correct
51 Correct 4 ms 5120 KB Output is correct
52 Correct 5 ms 5120 KB Output is correct
53 Correct 5 ms 5248 KB Output is correct
54 Correct 5 ms 5120 KB Output is correct
55 Correct 5 ms 5120 KB Output is correct
56 Correct 5 ms 5120 KB Output is correct
57 Correct 5 ms 5248 KB Output is correct
58 Correct 5 ms 5120 KB Output is correct
59 Correct 4 ms 5120 KB Output is correct
60 Correct 5 ms 5248 KB Output is correct
61 Correct 4 ms 5120 KB Output is correct
62 Correct 5 ms 5248 KB Output is correct
63 Correct 5 ms 5120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4992 KB Output is correct
2 Correct 3 ms 4992 KB Output is correct
3 Correct 3 ms 4992 KB Output is correct
4 Correct 4 ms 4992 KB Output is correct
5 Correct 3 ms 4992 KB Output is correct
6 Correct 4 ms 4992 KB Output is correct
7 Correct 4 ms 5120 KB Output is correct
8 Correct 3 ms 4992 KB Output is correct
9 Correct 3 ms 4992 KB Output is correct
10 Correct 4 ms 4992 KB Output is correct
11 Correct 4 ms 4992 KB Output is correct
12 Correct 3 ms 4992 KB Output is correct
13 Correct 4 ms 4992 KB Output is correct
14 Correct 4 ms 4992 KB Output is correct
15 Correct 4 ms 4992 KB Output is correct
16 Correct 4 ms 4992 KB Output is correct
17 Correct 3 ms 4992 KB Output is correct
18 Correct 4 ms 4992 KB Output is correct
19 Correct 3 ms 4992 KB Output is correct
20 Correct 4 ms 4992 KB Output is correct
21 Correct 4 ms 4992 KB Output is correct
22 Correct 5 ms 5120 KB Output is correct
23 Correct 5 ms 5120 KB Output is correct
24 Correct 5 ms 5120 KB Output is correct
25 Correct 5 ms 5120 KB Output is correct
26 Correct 5 ms 5120 KB Output is correct
27 Correct 5 ms 5120 KB Output is correct
28 Correct 5 ms 5120 KB Output is correct
29 Correct 5 ms 5248 KB Output is correct
30 Correct 5 ms 5120 KB Output is correct
31 Correct 5 ms 5120 KB Output is correct
32 Correct 5 ms 5120 KB Output is correct
33 Correct 5 ms 5120 KB Output is correct
34 Correct 5 ms 5120 KB Output is correct
35 Correct 5 ms 5120 KB Output is correct
36 Correct 5 ms 5120 KB Output is correct
37 Correct 5 ms 5120 KB Output is correct
38 Correct 5 ms 5120 KB Output is correct
39 Correct 5 ms 5120 KB Output is correct
40 Correct 5 ms 5120 KB Output is correct
41 Correct 5 ms 5120 KB Output is correct
42 Correct 6 ms 5120 KB Output is correct
43 Correct 5 ms 5120 KB Output is correct
44 Correct 6 ms 5120 KB Output is correct
45 Correct 5 ms 5120 KB Output is correct
46 Correct 5 ms 5120 KB Output is correct
47 Correct 5 ms 5120 KB Output is correct
48 Correct 5 ms 5120 KB Output is correct
49 Correct 5 ms 5120 KB Output is correct
50 Correct 5 ms 5120 KB Output is correct
51 Correct 4 ms 5120 KB Output is correct
52 Correct 5 ms 5120 KB Output is correct
53 Correct 5 ms 5248 KB Output is correct
54 Correct 5 ms 5120 KB Output is correct
55 Correct 5 ms 5120 KB Output is correct
56 Correct 5 ms 5120 KB Output is correct
57 Correct 5 ms 5248 KB Output is correct
58 Correct 5 ms 5120 KB Output is correct
59 Correct 4 ms 5120 KB Output is correct
60 Correct 5 ms 5248 KB Output is correct
61 Correct 4 ms 5120 KB Output is correct
62 Correct 5 ms 5248 KB Output is correct
63 Correct 5 ms 5120 KB Output is correct
64 Correct 246 ms 13048 KB Output is correct
65 Correct 242 ms 12920 KB Output is correct
66 Correct 242 ms 13000 KB Output is correct
67 Correct 242 ms 12952 KB Output is correct
68 Correct 251 ms 12920 KB Output is correct
69 Correct 237 ms 12920 KB Output is correct
70 Correct 249 ms 13048 KB Output is correct
71 Correct 250 ms 12920 KB Output is correct
72 Correct 241 ms 13084 KB Output is correct
73 Correct 290 ms 25592 KB Output is correct
74 Correct 279 ms 20664 KB Output is correct
75 Correct 263 ms 12920 KB Output is correct
76 Correct 251 ms 12792 KB Output is correct
77 Correct 266 ms 12920 KB Output is correct
78 Correct 284 ms 12792 KB Output is correct
79 Correct 311 ms 12920 KB Output is correct
80 Correct 262 ms 12792 KB Output is correct
81 Correct 222 ms 13296 KB Output is correct
82 Correct 258 ms 13044 KB Output is correct
83 Correct 269 ms 18212 KB Output is correct
84 Correct 269 ms 20196 KB Output is correct
85 Correct 265 ms 20584 KB Output is correct
86 Correct 261 ms 13944 KB Output is correct
87 Correct 250 ms 13944 KB Output is correct
88 Correct 252 ms 14200 KB Output is correct
89 Correct 265 ms 14192 KB Output is correct
90 Correct 251 ms 13944 KB Output is correct
91 Correct 246 ms 14300 KB Output is correct
92 Correct 239 ms 14564 KB Output is correct
93 Correct 235 ms 17012 KB Output is correct
94 Correct 226 ms 18676 KB Output is correct
95 Correct 229 ms 17268 KB Output is correct
96 Correct 240 ms 15572 KB Output is correct
97 Correct 254 ms 13944 KB Output is correct
98 Correct 241 ms 13560 KB Output is correct
99 Correct 239 ms 13436 KB Output is correct
100 Correct 273 ms 16488 KB Output is correct
101 Correct 262 ms 14456 KB Output is correct
102 Correct 259 ms 13688 KB Output is correct
103 Correct 253 ms 13560 KB Output is correct
104 Correct 137 ms 13644 KB Output is correct
105 Correct 289 ms 28536 KB Output is correct
106 Correct 139 ms 13676 KB Output is correct
107 Correct 145 ms 13672 KB Output is correct
108 Correct 276 ms 28536 KB Output is correct
109 Correct 159 ms 13680 KB Output is correct
110 Correct 279 ms 28536 KB Output is correct
111 Correct 225 ms 12924 KB Output is correct