# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
585734 |
2022-06-29T09:25:27 Z |
반딧불(#8385) |
Star Trek (CEOI20_startrek) |
C++17 |
|
1000 ms |
8328 KB |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
ll mpow(ll x, ll y){
if(!y) return 1;
if(y%2) return mpow(x, y-1) * x % MOD;
ll tmp = mpow(x, y/2);
return tmp*tmp%MOD;
}
int n;
ll k;
vector<int> link[1002];
ll mat[1002][1002];
ll depth[1002];
ll group[1002], groupCnt[2];
ll ans;
ll DP[2][1002];
ll find1[1002], find2[1002];
void dfs(int x, int p, int r){
if(r==1){
group[x] = depth[x]%2;
groupCnt[group[x]]++;
}
for(auto y: link[x]){
if(y==p) continue;
depth[y] = depth[x] + 1;
dfs(y, x, r);
if(!mat[r][y]) mat[r][x] = 1;
}
}
int dfsFind(int x, int p, int dp, int r){ /// ������ ������ �ϴ� ��� ���� �� ���� ã��
if(dp % 2 == 0){ /// my turn
int cnt = 0;
for(auto y: link[x]){
if(y!=p && !mat[r][y]) cnt++;
}
if(cnt > 1) return 0;
assert(cnt == 1);
for(auto y: link[x]){
if(y!=p && !mat[r][y]) return dfsFind(y, x, dp+1, r);
}
exit(1);
}
else{ /// your turn
int ret = 1;
for(auto y: link[x]){
if(y==p) continue;
ret += dfsFind(y, x, dp+1, r);
}
return ret;
}
}
int dfsFind2(int x, int p, int dp, int r){
if(dp % 2 == 0){ /// my turn
int ret = 1;
for(auto y: link[x]){
if(y==p) continue;
ret += dfsFind2(y, x, dp+1, r);
}
return ret;
}
else{ /// your turn
int cnt = 0;
for(auto y: link[x]){
if(y!=p && !mat[r][y]) cnt++;
}
if(cnt > 1) return 0;
assert(cnt == 1);
for(auto y: link[x]){
if(y!=p && !mat[r][y]) return dfsFind2(y, x, dp+1, r);
}
exit(1);
}
}
int main(){
scanf("%d %lld", &n, &k);
for(int i=1; i<n; i++){
int x, y;
scanf("%d %d", &x, &y);
link[x].push_back(y);
link[y].push_back(x);
}
// for(int i=n; i>=1; i--){
// for(int x=0; x<=n+1; x++) depth[x] = DP[x] = 0;
// dfs(i), DProot[i] = DP[i];
// }
// for(int i=1; i<=n; i++) printf("%d ", DProot[i]);
for(int i=1; i<=n; i++){
dfs(i, -1, i);
if(mat[i][i]) find1[i] = groupCnt[!group[i]] - dfsFind(i, -1, 0, i);
else find2[i] = dfsFind2(i, -1, 0, i);
DP[0][i] = mat[i][i];
}
for(int turn=1; turn<=k; turn++){
int b = turn%2;
/// ��ó��
ll Wsum = 0; /// �̱�� ������
for(int i=1; i<=n; i++) Wsum = (Wsum + DP[!b][i]) % MOD;
ll Lsum = (mpow(n, 2*turn-1) - Wsum + MOD) % MOD; /// ���� ������
/// ���� ���
for(int i=1; i<=n; i++){
DP[b][i] = 0;
if(mat[i][i]){ /// ���⼭ �����ϸ� �̱�� ���
/// ? -> W
DP[b][i] = (DP[b][i] + Wsum * n) % MOD;
/// my turn -> L
DP[b][i] = (DP[b][i] + groupCnt[group[i]] * Lsum) % MOD;
/// your turn -> L
DP[b][i] = (DP[b][i] + find1[i] * Lsum) % MOD;
}
else{
DP[b][i] = (DP[b][i] + find2[i] * Lsum) % MOD;
}
}
}
// if(DP[1]){ /// ���������� �̱� ��
// ll W = 0; /// ? -> W
// for(int i=1; i<=n; i++) if(DProot[i]) W++;
// ans += W*n;
//
// ll myturn = 0; /// my turn -> L
// for(int i=1; i<=n; i++) if(depth[i] % 2 == 0) myturn++;
// ans += myturn * (n-W);
//
// ll dw = 0;
// for(int i=1; i<=n; i++) if(DP[i]) dw++;
// ll tmp = n - myturn - dfsFind(1); /// your turn -> L
// ans += tmp * (n-W);
// }
// else{ /// ���������� �� ��
// ll L = 0;
// for(int i=1; i<=n; i++) if(!DProot[i]) L++;
// ans += L * dfsFind2(1);
// }
printf("%lld", DP[k%2][1]);
}
Compilation message
startrek.cpp: In function 'int main()':
startrek.cpp:86:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
86 | scanf("%d %lld", &n, &k);
| ~~~~~^~~~~~~~~~~~~~~~~~~
startrek.cpp:89:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
89 | scanf("%d %d", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
25 ms |
8172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
340 KB |
Output is correct |
3 |
Execution timed out |
1082 ms |
340 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
852 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
852 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
852 KB |
Output is correct |
7 |
Correct |
23 ms |
8184 KB |
Output is correct |
8 |
Correct |
30 ms |
8276 KB |
Output is correct |
9 |
Correct |
19 ms |
8268 KB |
Output is correct |
10 |
Correct |
18 ms |
8148 KB |
Output is correct |
11 |
Correct |
24 ms |
8236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
852 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
852 KB |
Output is correct |
7 |
Correct |
23 ms |
8184 KB |
Output is correct |
8 |
Correct |
30 ms |
8276 KB |
Output is correct |
9 |
Correct |
19 ms |
8268 KB |
Output is correct |
10 |
Correct |
18 ms |
8148 KB |
Output is correct |
11 |
Correct |
24 ms |
8236 KB |
Output is correct |
12 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
852 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
852 KB |
Output is correct |
7 |
Correct |
23 ms |
8184 KB |
Output is correct |
8 |
Correct |
30 ms |
8276 KB |
Output is correct |
9 |
Correct |
19 ms |
8268 KB |
Output is correct |
10 |
Correct |
18 ms |
8148 KB |
Output is correct |
11 |
Correct |
24 ms |
8236 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
25 ms |
8160 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
3 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
724 KB |
Output is correct |
17 |
Correct |
1 ms |
852 KB |
Output is correct |
18 |
Correct |
1 ms |
724 KB |
Output is correct |
19 |
Correct |
1 ms |
724 KB |
Output is correct |
20 |
Correct |
1 ms |
852 KB |
Output is correct |
21 |
Correct |
23 ms |
8252 KB |
Output is correct |
22 |
Correct |
30 ms |
8284 KB |
Output is correct |
23 |
Correct |
18 ms |
8136 KB |
Output is correct |
24 |
Correct |
21 ms |
8168 KB |
Output is correct |
25 |
Correct |
25 ms |
8208 KB |
Output is correct |
26 |
Correct |
634 ms |
8284 KB |
Output is correct |
27 |
Correct |
762 ms |
8328 KB |
Output is correct |
28 |
Correct |
589 ms |
6668 KB |
Output is correct |
29 |
Correct |
667 ms |
8232 KB |
Output is correct |
30 |
Correct |
427 ms |
8284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
852 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
852 KB |
Output is correct |
7 |
Correct |
23 ms |
8184 KB |
Output is correct |
8 |
Correct |
30 ms |
8276 KB |
Output is correct |
9 |
Correct |
19 ms |
8268 KB |
Output is correct |
10 |
Correct |
18 ms |
8148 KB |
Output is correct |
11 |
Correct |
24 ms |
8236 KB |
Output is correct |
12 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
25 ms |
8172 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
340 KB |
Output is correct |
5 |
Execution timed out |
1082 ms |
340 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |