답안 #743846

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
743846 2023-05-18T04:59:59 Z maomao90 Wells (CEOI21_wells) C++17
0 / 100
188 ms 392396 KB
#include <bits/stdc++.h> 
using namespace std;

template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
#define REP(i, s, e) for (int i = s; i < e; i++)
#define RREP(i, s, e) for (int i = s; i >= e; i--)
typedef long long ll;
typedef long double ld;
#define MP make_pair
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
#define MT make_tuple
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;

#ifdef DEBUG
#define debug(args...) printf(args)
#else
#define debug(args...)
#endif

#define INF 1000000005
#define LINF 1000000000000000005
#define MOD 1000000007
#define MAXN 10005

int n, k;
vi adj[MAXN];
ll ans;

int lvl[MAXN];
ii da[MAXN];
ii getd(int u, int p, int cd) {
	lvl[u] = cd;
	ii mx = MP(cd, u);
	for (int v : adj[u]) {
		if (v == p) continue;
		mxto(mx, getd(v, u, cd + 1));
	}
	da[u] = mx;
	return mx;
}

bool bad[MAXN];
bool cancel(int u, int p, int cd) {
	bool found = cd >= k;
	for (int v : adj[u]) {
		if (v == p) continue;
		found |= cancel(v, u, cd + 1);
	}
	if (!found) bad[u] = 1;
	return found;
}

int memo[MAXN][MAXN];
int dfs(int u, int rem, int p) {
	if (memo[u][rem] != -1) return memo[u][rem];
	int cnt = 0;
	int mx = -1, smx = -1;
	for (int v : adj[u]) {
		if (v == p || bad[v]) continue;
		int tmp = dfs(v, rem == 0 ? k : rem - 1, u);
		if (tmp == -1) return memo[u][rem] = -1;
		cnt += tmp;
		if (tmp == 0) {
			if (mx <= da[v].FI) {
				smx = mx;
				mx = da[v].FI;
			} else {
				mxto(smx, da[v].FI);
			}
		}
	}
	debug("%d %d: ", u, rem);
	if (rem == 0) {
		debug("1\n");
		return memo[u][rem] = 1;
	}
	if (smx != -1 && mx + smx - 2 * lvl[u] >= k) {
		debug("-1\n");
		return memo[u][rem] = -1;
	}
	if (cnt > 1) {
		if (rem * 2 != k + 1) {
			debug("-1\n");
			return memo[u][rem] = -1;
		}
	}
	debug("%d\n", cnt >= 1);
	return memo[u][rem] = cnt >= 1;
}

int main() {
	scanf("%d%d", &n, &k);
	REP (i, 1, n) {
		int a, b; scanf("%d%d", &a, &b);
		adj[a].pb(b);
		adj[b].pb(a);
	}
	k--;
	memset(memo, -1, sizeof memo);
	int u = getd(1, -1, 0).SE;
	getd(u, -1, 0);
	cancel(u, -1, 0);
	debug("%d\n", u);
	bool pos = 0;
	if (bad[u]) {
		pos = 1;
		ans = 1;
		REP (i, 0, n) {
			ans = ans * 2 % MOD;
		}
	} else {
		REP (i, 0, k + 1) {
			ans += dfs(u, i, -1) != -1;
			ans %= MOD;
		}
		if (ans != 0) pos = 1;
		REP (i, 1, n + 1) {
			if (bad[i]) {
				ans = ans * 2 % MOD;
			}
		}
	}
	if (!pos) {
		printf("NO\n");
	} else {
		printf("YES\n");
	}
	printf("%lld\n", ans);
	return 0;
}

/*
8 5
1 2
2 3
3 4
2 7
2 5
5 6
7 8
*/

Compilation message

wells.cpp: In function 'int main()':
wells.cpp:103:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  103 |  scanf("%d%d", &n, &k);
      |  ~~~~~^~~~~~~~~~~~~~~~
wells.cpp:105:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  105 |   int a, b; scanf("%d%d", &a, &b);
      |             ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 165 ms 392204 KB Output is correct
2 Partially correct 155 ms 392268 KB Output is partially correct
3 Partially correct 155 ms 392396 KB Output is partially correct
4 Partially correct 188 ms 392304 KB Output is partially correct
5 Partially correct 160 ms 392356 KB Output is partially correct
6 Partially correct 156 ms 392256 KB Output is partially correct
7 Incorrect 157 ms 392248 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 165 ms 392204 KB Output is correct
2 Partially correct 155 ms 392268 KB Output is partially correct
3 Partially correct 155 ms 392396 KB Output is partially correct
4 Partially correct 188 ms 392304 KB Output is partially correct
5 Partially correct 160 ms 392356 KB Output is partially correct
6 Partially correct 156 ms 392256 KB Output is partially correct
7 Incorrect 157 ms 392248 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 165 ms 392204 KB Output is correct
2 Partially correct 155 ms 392268 KB Output is partially correct
3 Partially correct 155 ms 392396 KB Output is partially correct
4 Partially correct 188 ms 392304 KB Output is partially correct
5 Partially correct 160 ms 392356 KB Output is partially correct
6 Partially correct 156 ms 392256 KB Output is partially correct
7 Incorrect 157 ms 392248 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 165 ms 392204 KB Output is correct
2 Partially correct 155 ms 392268 KB Output is partially correct
3 Partially correct 155 ms 392396 KB Output is partially correct
4 Partially correct 188 ms 392304 KB Output is partially correct
5 Partially correct 160 ms 392356 KB Output is partially correct
6 Partially correct 156 ms 392256 KB Output is partially correct
7 Incorrect 157 ms 392248 KB Output isn't correct
8 Halted 0 ms 0 KB -