답안 #577762

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
577762 2022-06-15T08:42:24 Z amunduzbaev Star Trek (CEOI20_startrek) C++17
38 / 100
91 ms 17112 KB
#include "bits/stdc++.h"
using namespace std;
 
#define ar array
typedef int64_t ll;

const int N = 1e5 + 5;
const int mod = 1e9 + 7;
vector<int> edges[N];
int dp[N], sub[N], up[N];
int is[N][2], cnt[N];
ar<int, 2> C{};

void dfs(int u, int p = -1){
	cnt[u] = 1;
	for(auto x : edges[u]){
		if(x == p) continue;
		dfs(x, u);
		if(dp[x] == 0) dp[u] = 1;
		cnt[u] += cnt[x];
	}
	sub[u] = dp[u];
}

void re(int u, int p = -1){
	if(up[u] == 0) dp[u] = 1;
	ar<int, 2> c {};
	c[up[u]]++;
	for(auto x : edges[u]){
		if(x == p) continue;
		c[dp[x]]++;
	}
	
	for(auto x : edges[u]){
		if(x == p) continue;
		c[dp[x]]--;
		if(c[0]) up[x] = 1;
		c[dp[x]]++;
	}
	
	for(auto x : edges[u]){
		if(x == p) continue;
		re(x, u);
	}
}

int n;
void dfs2(int u, int p = -1){
	int f = 0, v = -1;
	for(auto x : edges[u]){
		if(x == p) continue;
		dfs2(x, u);
		if(!sub[x]){
			f++, v = x;
		}
	}
	
	if(f > 1){
		is[u][1] = cnt[u] * 1ll * n % mod;
	} else if(f == 1) {
		is[u][1] = (cnt[u] - cnt[v]) * 1ll * n % mod;
		is[u][1] = (is[u][1] + is[v][0]) % mod, is[u][0] = (is[u][0] + is[v][1]) % mod;
	} else {
		is[u][1] = C[0], is[u][0] = C[1];
		for(auto x : edges[u]){
			if(x == p) continue;
			is[u][0] = (is[u][0] + is[x][1]) % mod, is[u][1] = (is[u][1] + is[x][0]) % mod;
		}
	}
}

signed main(){
	ios::sync_with_stdio(0); cin.tie(0);
	
	int d; cin>>n>>d;
	for(int i=1;i<n;i++){
		int a, b; cin>>a>>b;
		edges[a].push_back(b);
		edges[b].push_back(a);
	}
	
	dfs(1);
	up[1] = 1;
	re(1);
	for(int i=1;i<=n;i++){
		C[dp[i]]++;
	}
	
	dfs2(1);
	cout<<is[1][1]<<"\n";
}

/*

5 1
1 2
2 3
2 4
1 5

*/

# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Incorrect 2 ms 2644 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 2644 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2772 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2772 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 70 ms 11728 KB Output is correct
13 Correct 91 ms 17112 KB Output is correct
14 Correct 50 ms 9300 KB Output is correct
15 Correct 91 ms 9324 KB Output is correct
16 Correct 70 ms 9340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2772 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 1 ms 2644 KB Output is correct
13 Incorrect 2 ms 2644 KB Output isn't correct
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2772 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 70 ms 11728 KB Output is correct
13 Correct 91 ms 17112 KB Output is correct
14 Correct 50 ms 9300 KB Output is correct
15 Correct 91 ms 9324 KB Output is correct
16 Correct 70 ms 9340 KB Output is correct
17 Correct 1 ms 2644 KB Output is correct
18 Incorrect 2 ms 2644 KB Output isn't correct
19 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Incorrect 2 ms 2644 KB Output isn't correct
3 Halted 0 ms 0 KB -