답안 #623723

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
623723 2022-08-06T12:24:07 Z IWTIM Džumbus (COCI19_dzumbus) C++14
110 / 110
71 ms 63296 KB
# include <bits/stdc++.h>
using namespace std;
using ll = long long;
using db = long double; // or double, if TL is tight
using str = string; // yay python! 
 
// pairs
using pii = pair<int,int>;
using pl = pair<ll,ll>;
using pd = pair<db,db>;
#define mp make_pair
#define f first
#define s second
 
#define tcT template<class T
#define tcTU tcT, class U
// ^ lol this makes everything look weird but I'll try it
tcT> using V = vector<T>; 
tcT, size_t SZ> using AR = array<T,SZ>; 
using vi = V<int>;
using vb = V<bool>;
using vl = V<ll>;
using vd = V<db>;
using vs = V<str>;
using vpi = V<pii>;
using vpl = V<pl>;
using vpd = V<pd>;
 
// vectors
// oops size(x), rbegin(x), rend(x) need C++17
#define sz(x) int((x).size())
#define f first
#define s second
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define rall(x) x.rbegin(), x.rend() 
#define sor(x) sort(all(x)) 
#define rsz resize
#define ins insert 
#define pb push_back
#define eb emplace_back
#define ft front()
#define bk back()
 
#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)
const int MOD = 998244353;
const int MX = 2e5+5;
const ll BIG = 1e18; // not too close to LLONG_MAX
const db PI = acos((db)-1);
const int dx[4]{1,0,-1,0}, dy[4]{0,1,0,-1}; // for every grid problem!!
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); 
template<class T> using pqg = priority_queue<T,vector<T>,greater<T>>;
struct DSU {
	vi e; void init(int N) { e = vi(N,-1); }
	int get(int x) { return e[x] < 0 ? x : e[x] = get(e[x]); } 
	bool sameSet(int a, int b) { return get(a) == get(b); }
	int size(int x) { return -e[get(x)]; }
	bool unite(int x, int y) { // union by size
		x = get(x), y = get(y); if (x == y) return 0;
		if (e[x] > e[y]) swap(x,y);
		e[x] += e[y]; e[y] = x; return 1;
	}
};
/*
inline namespace Helpers {
	//////////// is_iterable
	// https://stackoverflow.com/questions/13830158/check-if-a-variable-type-is-iterable
	// this gets used only when we can call begin() and end() on that type
	tcT, class = void> struct is_iterable : false_type {};
	tcT> struct is_iterable<T, void_t<decltype(begin(declval<T>())),
	                                  decltype(end(declval<T>()))
	                                 >
	                       > : true_type {};
	tcT> constexpr bool is_iterable_v = is_iterable<T>::value;
 
	//////////// is_readable
	tcT, class = void> struct is_readable : false_type {};
	tcT> struct is_readable<T,
	        typename std::enable_if_t<
	            is_same_v<decltype(cin >> declval<T&>()), istream&>
	        >
	    > : true_type {};
	tcT> constexpr bool is_readable_v = is_readable<T>::value;
 
	//////////// is_printable
	// // https://nafe.es/posts/2020-02-29-is-printable/
	tcT, class = void> struct is_printable : false_type {};
	tcT> struct is_printable<T,
	        typename std::enable_if_t<
	            is_same_v<decltype(cout << declval<T>()), ostream&>
	        >
	    > : true_type {};
	tcT> constexpr bool is_printable_v = is_printable<T>::value;
}*/
#define int long long
#define pb push_back
const int N = 3e5 + 5, inf = 1e17;
int t,n,a[N], fix[N],sz[N],d[N];
vector <int> v[N];
vector <int> dp[N][3], dp1[N][3];
void cmin(int &x, int y) {
    if (x > y) x = y;
}
void dfsz(int a, int p) {
	sz[a] = 1;
	for (int to : v[a]) {
		if (to == p) continue;
		dfsz(to, a);
		sz[a] += sz[to];	
	}
	dp[a][0] = vector <int> (sz[a] + 10, inf);
	dp[a][1] = vector <int> (sz[a] + 10, inf);
}
void dfss(int a, int p) {
	fix[a] = 1;
	for (int to : v[a]) {
		if (to == p) continue;
		dfss(to,a);
	}
}
void forest_tree() {
	for (int i = 1; i <= n; i++) {
		if (!fix[i]) dfss(i, 0), v[n + 1].pb(i);
	}
	n++;
	for (int to : v[n]) {
		v[to].pb(n);
	}
	
}
void dfs(int a, int p) {
	if (v[a].size() == 1 && a != n) {
		dp[a][0][0] = 0;
		dp[a][1][0] = d[a];
		return ;
	}
//	cout<<a<<"\n";
	dp[a][0][0] = 0;
	dp[a][1][0] = d[a];
	int cursz = 0;
	int id = 0;
	dp1[a][0] = dp1[a][1] = vector <int> (sz[a] + 10, inf);
	for (int to : v[a]) {
		id++;
		if (to == p) continue;
		dfs(to,a);
		cursz += sz[to];
		int prsz = cursz - sz[to];
		for (int i = 0; i <= prsz + 1; i++) {
			cmin(dp1[a][0][i], dp[a][0][i]);
			cmin(dp1[a][1][i], dp[a][1][i]);
		}
		for (int i = 0; i <= prsz; i++) {
			for (int j = 0; j <= sz[to]; j++) {
				cmin(dp[a][0][i + j], dp1[a][0][i] + min(dp[to][1][j], dp[to][0][j]));
			}
		}
		for (int i = 0; i <= prsz + 1; i++) {
			for (int j = 0; j <= sz[to]; j++) {
				/* 0 ---- > 0 */ cmin(dp[a][1][i + j], dp1[a][0][i] + dp[to][0][j] + d[a]); // prsz + j
				/* 0 -----> 0 */ cmin(dp[a][1][i + j + 2], dp1[a][0][i] + dp[to][0][j] + d[a] + d[to]);
				/* 0 -----> 1 */ cmin(dp[a][1][i + j + 1], dp1[a][0][i] + dp[to][1][j] + d[a]);     //   prsz + j
				/* 1 -----> 0 */ cmin(dp[a][1][i + j], dp1[a][1][i] + dp[to][0][j]); 
				
				/* 1 -----> 0 */ cmin(dp[a][1][i + j + 1], dp1[a][1][i] + dp[to][0][j] + d[to]);
			    //	if (a == 10 && to == 12 && i == 2 && j == 0) cout<<i + j + 1<<" "<<dp1[a][1][i] + dp[to][0][j] + d[to]<<" "<<dp[10][1][3]<<endl;
				/* 1 -----> 1 */ cmin(dp[a][1][i + j], dp1[a][1][i] + dp[to][1][j]);
			}
		}
		if (a == 10) {
		   // cout<<dp[10][1][3]<<endl;
		}
	}
	//if(a == 10) cout<<"\n";
}
main() {
    std::ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    int m;
    cin>>n>>m;
    for (int i = 1; i <= n; i++) {
    	cin>>d[i];
	}
	for (int i = 1; i <= m; i++) {
		int a, b;
		cin>>a>>b;
		v[a].pb(b);
		v[b].pb(a);
	}
	forest_tree();
	
	dfsz(n, 0);
	dfs(n,0);
	//exit(0);
	//cout<<dp[10][1][3]<<"\n";
	int q;
	cin>>q;
	while (q--) {
		int val;
		cin>>val;
		int le = 0;
		int ri = n;
		int ans = 0;
		while (le <= ri) {
			int mid = (le + ri) / 2;
			if (dp[n][0][mid] <= val) {
				le = mid + 1;
				ans = mid;
			} else ri = mid - 1;
		}
		cout<<ans<<"\n";
	}
}

Compilation message

dzumbus.cpp:183:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  183 | main() {
      | ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 58744 KB Output is correct
2 Correct 34 ms 60244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 58744 KB Output is correct
2 Correct 34 ms 60244 KB Output is correct
3 Correct 65 ms 63296 KB Output is correct
4 Correct 67 ms 61276 KB Output is correct
5 Correct 71 ms 60340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 57 ms 51728 KB Output is correct
2 Correct 54 ms 51480 KB Output is correct
3 Correct 57 ms 51960 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 58744 KB Output is correct
2 Correct 34 ms 60244 KB Output is correct
3 Correct 65 ms 63296 KB Output is correct
4 Correct 67 ms 61276 KB Output is correct
5 Correct 71 ms 60340 KB Output is correct
6 Correct 57 ms 51728 KB Output is correct
7 Correct 54 ms 51480 KB Output is correct
8 Correct 57 ms 51960 KB Output is correct
9 Correct 64 ms 52552 KB Output is correct
10 Correct 63 ms 52924 KB Output is correct
11 Correct 71 ms 52772 KB Output is correct