답안 #296123

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
296123 2020-09-10T09:58:39 Z maximath_1 Power Plant (JOI20_power) C++11
100 / 100
1088 ms 34228 KB
#include <iostream>
#include <array>
#include <vector>
#include <algorithm>
#include <string.h>
#include <set>
#include <math.h>
#include <numeric>
#include <assert.h>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <bitset>
#include <queue>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define ld long double
#define endl "\n"
typedef tree<pair<int, int>, null_type, less<pair<int, int> >, rb_tree_tag, tree_order_statistics_node_update> OST;
typedef tree<pair<int, int>, null_type, less_equal<pair<int, int> >, rb_tree_tag, tree_order_statistics_node_update> OST_multiset;
const ll inf = 1e9 + 69;
const ld pi = 3.14159265358979323L;
const ld eps = 1e-15;
const int N = 1e5 + 5;

void setIn(string s){freopen(s.c_str(), "r", stdin);}
void setOut(string s){freopen(s.c_str(), "w", stdout);}
void unsyncIO(){cin.tie(0) -> sync_with_stdio(0);}
void setIO(string s = ""){
	unsyncIO();
	if(s.size()){
		setIn(s + ".in");
		setOut(s + ".out");
	}
}

#define gc getchar//_unlocked
#define pc putchar//_unlocked
int inp(){
	char c = gc(); bool neg = false;
	for(; c < '0'||'9' < c; c = gc())
		if(c == '-') neg=true;
	int rs = c - '0'; c = gc();
	for(; '0' <= c && c <= '9'; c = gc())
		rs = (rs << 1) + (rs << 3) + (c - '0');
	if(neg) rs = -rs;
	return rs;
}
void pri(int _n){
	int N = _n, rev, count = 0;
	rev = N;
	if(N == 0) {pc('0'); return;}
	while(rev % 10 == 0) {count ++; rev /= 10;}
	rev = 0;
	while(N != 0) {rev = (rev << 3) + (rev << 1) + N % 10; N /= 10;}
	while(rev != 0) {pc(rev % 10 + '0'); rev /= 10;}
	while(count --) pc('0');
}

int n;
vector<int> adj[200005];
bitset<200005> power; //powerhouse

vector<int> ct[200005]; //centroid tree
int remov[200005], sz[200005];

void subtree(int nw, int bf){
	sz[nw] = 1;
	for(int nx : adj[nw]) if(nx != bf && !remov[nx]){
		subtree(nx, nw);
		sz[nw] += sz[nx];
	}
}

int find_centroid(int nw){
	subtree(nw, -1);
	int tsz = sz[nw];
	for(bool found = 0; !found;){
		found = 1;
		for(int nx : adj[nw]) if(sz[nx] <= sz[nw] && !remov[nx]){
			if(sz[nx] >= tsz / 2){
				found = 0;
				nw = nx; break;
			}
		}
	}
	return nw;
}

int build_centroid_tree(int nw){
	nw = find_centroid(nw);
	remov[nw] = true;
	for(int nx : adj[nw]) if(!remov[nx]){
		nx = build_centroid_tree(nx);
		ct[nw].push_back(nx);
	}
	return nw;
}

ll dfs(int nw, int bf, int cnt_par){
	ll res;
	if(power[nw]) res = 1ll - cnt_par;
	else res = -100000000000000069ll;

	ll sm = 0, cnt = 0;
	for(int nx : adj[nw]) if(nx != bf && !remov[nx]){
		ll cr = dfs(nx, nw, cnt_par + power[nw]);
		if(cr < -1000000000000069ll) {cr = 0; cnt --;}
		sm += cr;
		cnt ++;
	}

	if(cnt > 0) sm += (cnt_par + power[nw]) * 1ll * (cnt - 1);
	else sm = -100000000000000069ll;

	return max(res, sm);
}

int eval(int rt){
	int res = dfs(rt, -1, 0);
	remov[rt] = 1;
	for(int nx : ct[rt])
		res = max(res, eval(nx));
	return res;
}

int main(){
	setIO();

	cin >> n;
	for(int u, v, i = 1; i < n; i ++){
		cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	string s; cin >> s;
	int count = 0;
	for(int i = 1; i <= n; i ++){
		if(s[i - 1] == '1'){
			power[i] = 1;
			count ++;
		}
	}

	if(count <= 2) {cout << count << endl; return 0;}

	int rt = build_centroid_tree(1);
	memset(remov, 0, sizeof(remov));
	cout << max(eval(rt), 2) << endl;
	
	return 0;
}

Compilation message

power.cpp: In function 'void setIn(std::string)':
power.cpp:30:29: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   30 | void setIn(string s){freopen(s.c_str(), "r", stdin);}
      |                      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
power.cpp: In function 'void setOut(std::string)':
power.cpp:31:30: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   31 | void setOut(string s){freopen(s.c_str(), "w", stdout);}
      |                       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 7 ms 9728 KB Output is correct
3 Correct 7 ms 10496 KB Output is correct
4 Correct 7 ms 10496 KB Output is correct
5 Correct 7 ms 10496 KB Output is correct
6 Correct 7 ms 10496 KB Output is correct
7 Correct 7 ms 10496 KB Output is correct
8 Correct 7 ms 10496 KB Output is correct
9 Correct 7 ms 10528 KB Output is correct
10 Correct 7 ms 10496 KB Output is correct
11 Correct 7 ms 10496 KB Output is correct
12 Correct 7 ms 10496 KB Output is correct
13 Correct 7 ms 10496 KB Output is correct
14 Correct 7 ms 10496 KB Output is correct
15 Correct 7 ms 10496 KB Output is correct
16 Correct 7 ms 10496 KB Output is correct
17 Correct 7 ms 10496 KB Output is correct
18 Correct 8 ms 10496 KB Output is correct
19 Correct 7 ms 9728 KB Output is correct
20 Correct 7 ms 10496 KB Output is correct
21 Correct 7 ms 10496 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 7 ms 9728 KB Output is correct
3 Correct 7 ms 10496 KB Output is correct
4 Correct 7 ms 10496 KB Output is correct
5 Correct 7 ms 10496 KB Output is correct
6 Correct 7 ms 10496 KB Output is correct
7 Correct 7 ms 10496 KB Output is correct
8 Correct 7 ms 10496 KB Output is correct
9 Correct 7 ms 10528 KB Output is correct
10 Correct 7 ms 10496 KB Output is correct
11 Correct 7 ms 10496 KB Output is correct
12 Correct 7 ms 10496 KB Output is correct
13 Correct 7 ms 10496 KB Output is correct
14 Correct 7 ms 10496 KB Output is correct
15 Correct 7 ms 10496 KB Output is correct
16 Correct 7 ms 10496 KB Output is correct
17 Correct 7 ms 10496 KB Output is correct
18 Correct 8 ms 10496 KB Output is correct
19 Correct 7 ms 9728 KB Output is correct
20 Correct 7 ms 10496 KB Output is correct
21 Correct 7 ms 10496 KB Output is correct
22 Correct 9 ms 10624 KB Output is correct
23 Correct 9 ms 10624 KB Output is correct
24 Correct 9 ms 10624 KB Output is correct
25 Correct 9 ms 10624 KB Output is correct
26 Correct 9 ms 10624 KB Output is correct
27 Correct 9 ms 10624 KB Output is correct
28 Correct 8 ms 9856 KB Output is correct
29 Correct 9 ms 10752 KB Output is correct
30 Correct 10 ms 10752 KB Output is correct
31 Correct 9 ms 10656 KB Output is correct
32 Correct 9 ms 10624 KB Output is correct
33 Correct 9 ms 10624 KB Output is correct
34 Correct 9 ms 10752 KB Output is correct
35 Correct 9 ms 10624 KB Output is correct
36 Correct 9 ms 10624 KB Output is correct
37 Correct 8 ms 10624 KB Output is correct
38 Correct 8 ms 10624 KB Output is correct
39 Correct 9 ms 10624 KB Output is correct
40 Correct 9 ms 10624 KB Output is correct
41 Correct 9 ms 10752 KB Output is correct
42 Correct 11 ms 10624 KB Output is correct
43 Correct 9 ms 10624 KB Output is correct
44 Correct 9 ms 10624 KB Output is correct
45 Correct 9 ms 10624 KB Output is correct
46 Correct 10 ms 10624 KB Output is correct
47 Correct 9 ms 10656 KB Output is correct
48 Correct 9 ms 10752 KB Output is correct
49 Correct 9 ms 10752 KB Output is correct
50 Correct 9 ms 10624 KB Output is correct
51 Correct 9 ms 10624 KB Output is correct
52 Correct 9 ms 10624 KB Output is correct
53 Correct 9 ms 10624 KB Output is correct
54 Correct 9 ms 10624 KB Output is correct
55 Correct 9 ms 10624 KB Output is correct
56 Correct 8 ms 10624 KB Output is correct
57 Correct 9 ms 10752 KB Output is correct
58 Correct 8 ms 10624 KB Output is correct
59 Correct 9 ms 10624 KB Output is correct
60 Correct 8 ms 9856 KB Output is correct
61 Correct 7 ms 9856 KB Output is correct
62 Correct 10 ms 10752 KB Output is correct
63 Correct 9 ms 10624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 7 ms 9728 KB Output is correct
3 Correct 7 ms 10496 KB Output is correct
4 Correct 7 ms 10496 KB Output is correct
5 Correct 7 ms 10496 KB Output is correct
6 Correct 7 ms 10496 KB Output is correct
7 Correct 7 ms 10496 KB Output is correct
8 Correct 7 ms 10496 KB Output is correct
9 Correct 7 ms 10528 KB Output is correct
10 Correct 7 ms 10496 KB Output is correct
11 Correct 7 ms 10496 KB Output is correct
12 Correct 7 ms 10496 KB Output is correct
13 Correct 7 ms 10496 KB Output is correct
14 Correct 7 ms 10496 KB Output is correct
15 Correct 7 ms 10496 KB Output is correct
16 Correct 7 ms 10496 KB Output is correct
17 Correct 7 ms 10496 KB Output is correct
18 Correct 8 ms 10496 KB Output is correct
19 Correct 7 ms 9728 KB Output is correct
20 Correct 7 ms 10496 KB Output is correct
21 Correct 7 ms 10496 KB Output is correct
22 Correct 9 ms 10624 KB Output is correct
23 Correct 9 ms 10624 KB Output is correct
24 Correct 9 ms 10624 KB Output is correct
25 Correct 9 ms 10624 KB Output is correct
26 Correct 9 ms 10624 KB Output is correct
27 Correct 9 ms 10624 KB Output is correct
28 Correct 8 ms 9856 KB Output is correct
29 Correct 9 ms 10752 KB Output is correct
30 Correct 10 ms 10752 KB Output is correct
31 Correct 9 ms 10656 KB Output is correct
32 Correct 9 ms 10624 KB Output is correct
33 Correct 9 ms 10624 KB Output is correct
34 Correct 9 ms 10752 KB Output is correct
35 Correct 9 ms 10624 KB Output is correct
36 Correct 9 ms 10624 KB Output is correct
37 Correct 8 ms 10624 KB Output is correct
38 Correct 8 ms 10624 KB Output is correct
39 Correct 9 ms 10624 KB Output is correct
40 Correct 9 ms 10624 KB Output is correct
41 Correct 9 ms 10752 KB Output is correct
42 Correct 11 ms 10624 KB Output is correct
43 Correct 9 ms 10624 KB Output is correct
44 Correct 9 ms 10624 KB Output is correct
45 Correct 9 ms 10624 KB Output is correct
46 Correct 10 ms 10624 KB Output is correct
47 Correct 9 ms 10656 KB Output is correct
48 Correct 9 ms 10752 KB Output is correct
49 Correct 9 ms 10752 KB Output is correct
50 Correct 9 ms 10624 KB Output is correct
51 Correct 9 ms 10624 KB Output is correct
52 Correct 9 ms 10624 KB Output is correct
53 Correct 9 ms 10624 KB Output is correct
54 Correct 9 ms 10624 KB Output is correct
55 Correct 9 ms 10624 KB Output is correct
56 Correct 8 ms 10624 KB Output is correct
57 Correct 9 ms 10752 KB Output is correct
58 Correct 8 ms 10624 KB Output is correct
59 Correct 9 ms 10624 KB Output is correct
60 Correct 8 ms 9856 KB Output is correct
61 Correct 7 ms 9856 KB Output is correct
62 Correct 10 ms 10752 KB Output is correct
63 Correct 9 ms 10624 KB Output is correct
64 Correct 674 ms 22588 KB Output is correct
65 Correct 630 ms 22584 KB Output is correct
66 Correct 630 ms 22564 KB Output is correct
67 Correct 664 ms 22712 KB Output is correct
68 Correct 615 ms 22692 KB Output is correct
69 Correct 631 ms 22580 KB Output is correct
70 Correct 635 ms 22596 KB Output is correct
71 Correct 652 ms 22724 KB Output is correct
72 Correct 130 ms 17880 KB Output is correct
73 Correct 1079 ms 33496 KB Output is correct
74 Correct 1088 ms 33336 KB Output is correct
75 Correct 608 ms 23156 KB Output is correct
76 Correct 599 ms 23232 KB Output is correct
77 Correct 762 ms 22436 KB Output is correct
78 Correct 781 ms 22256 KB Output is correct
79 Correct 749 ms 22208 KB Output is correct
80 Correct 729 ms 22456 KB Output is correct
81 Correct 260 ms 22260 KB Output is correct
82 Correct 312 ms 22648 KB Output is correct
83 Correct 945 ms 27356 KB Output is correct
84 Correct 948 ms 28584 KB Output is correct
85 Correct 909 ms 28228 KB Output is correct
86 Correct 781 ms 23760 KB Output is correct
87 Correct 771 ms 23592 KB Output is correct
88 Correct 553 ms 23028 KB Output is correct
89 Correct 533 ms 23112 KB Output is correct
90 Correct 560 ms 23012 KB Output is correct
91 Correct 545 ms 23196 KB Output is correct
92 Correct 545 ms 23464 KB Output is correct
93 Correct 619 ms 28548 KB Output is correct
94 Correct 593 ms 30444 KB Output is correct
95 Correct 575 ms 26352 KB Output is correct
96 Correct 592 ms 24484 KB Output is correct
97 Correct 505 ms 22804 KB Output is correct
98 Correct 472 ms 22376 KB Output is correct
99 Correct 506 ms 22324 KB Output is correct
100 Correct 574 ms 24996 KB Output is correct
101 Correct 525 ms 22960 KB Output is correct
102 Correct 516 ms 22536 KB Output is correct
103 Correct 579 ms 22576 KB Output is correct
104 Correct 147 ms 21740 KB Output is correct
105 Correct 1048 ms 32824 KB Output is correct
106 Correct 139 ms 21740 KB Output is correct
107 Correct 139 ms 21740 KB Output is correct
108 Correct 133 ms 17756 KB Output is correct
109 Correct 90 ms 18540 KB Output is correct
110 Correct 1054 ms 34228 KB Output is correct
111 Correct 489 ms 21192 KB Output is correct