답안 #585703

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
585703 2022-06-29T08:45:46 Z 반딧불(#8385) Star Trek (CEOI20_startrek) C++17
38 / 100
69 ms 15588 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[100002];
int depth[100002];
int DP[100002];
int DProot[100002];
ll ans;

void dfs(int x, int p=-1){
    for(auto y: link[x]){
        if(y==p) continue;
        depth[y] = depth[x] + 1;
        dfs(y, x);
        if(!DP[y]) DP[x] = 1;
    }
}

void dfs2(int x, int p=-1, bool winUp = 0){
    DProot[x] = (DP[x] || winUp);
    int cnt = 0;
    for(auto y: link[x]) if(y!=p) cnt += !DP[y];
    for(auto y: link[x]){
        if(y==p) continue;
        dfs2(y, x, !winUp && !(cnt-!DP[y]));
    }
}

int dfsFind(int x, int p=-1){ /// ������ ������ �ϴ� ��� ���� �� ���� ã��
    if(depth[x] % 2 == 0){ /// my turn
        int cnt = 0;
        for(auto y: link[x]){
            if(y!=p && !DP[y]) cnt++;
        }
        if(cnt > 1) return 0;
        assert(cnt == 1);
        for(auto y: link[x]){
            if(y!=p && !DP[y]) return dfsFind(y, x);
        }
        exit(1);
    }
    else{ /// your turn
        int ret = 1;
        for(auto y: link[x]){
            if(y==p) continue;
            ret += dfsFind(y, x);
        }
        return ret;
    }
}

int dfsFind2(int x, int p=-1){
    if(depth[x] % 2 == 0){ /// my turn
        int ret = 1;
        for(auto y: link[x]){
            if(y==p) continue;
            ret += dfsFind2(y, x);
        }
        return ret;
    }
    else{ /// your turn
        int cnt = 0;
        for(auto y: link[x]){
            if(y!=p && !DP[y]) cnt++;
        }
        if(cnt > 1) return 0;
        assert(cnt == 1);
        for(auto y: link[x]){
            if(y!=p && !DP[y]) return dfsFind2(y, x);
        }
        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);
    }
    dfs(1);
    dfs2(1);
//    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]);

    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", ans%MOD);
}

Compilation message

startrek.cpp: In function 'int main()':
startrek.cpp:89:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   89 |     scanf("%d %lld", &n, &k);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~
startrek.cpp:92:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   92 |         scanf("%d %d", &x, &y);
      |         ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 2 ms 2660 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 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 2596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 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 2596 KB Output is correct
12 Correct 57 ms 10544 KB Output is correct
13 Correct 69 ms 15588 KB Output is correct
14 Correct 41 ms 8032 KB Output is correct
15 Correct 50 ms 7776 KB Output is correct
16 Correct 52 ms 7832 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 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 2596 KB Output is correct
12 Correct 2 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 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 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 2596 KB Output is correct
12 Correct 57 ms 10544 KB Output is correct
13 Correct 69 ms 15588 KB Output is correct
14 Correct 41 ms 8032 KB Output is correct
15 Correct 50 ms 7776 KB Output is correct
16 Correct 52 ms 7832 KB Output is correct
17 Correct 2 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 1 ms 2644 KB Output is correct
2 Incorrect 2 ms 2644 KB Output isn't correct
3 Halted 0 ms 0 KB -