답안 #1031987

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1031987 2024-07-23T09:27:47 Z 김은성(#10960) Power Plant (JOI20_power) C++17
0 / 100
3 ms 5156 KB
#include <bits/stdc++.h>
using namespace std;
bool ch[200009];
int ch2[200009], cnt = 0;
vector<int> graph[200009];
int a[200009], sz[200009], dp[200009], ans = 0;
char s[200009];
int XX =0;
void getsize(int v){
    ch2[v] = cnt;
    sz[v] = 1;
    for(int i=0; i<graph[v].size(); i++){
        int u = graph[v][i];
        if(!ch[u] && ch2[u] != cnt){
            getsize(u);
            sz[v] += sz[u];
        }
    }
}
int centroid(int v, int crit){
    ch2[v] = cnt;
    //printf("v=%d sz=%d crit=%d\n", v, sz[v], crit);
    for(int i=0; i<graph[v].size(); i++){
        int u = graph[v][i];
        if(!ch[u] && ch2[u] != cnt){
            if(sz[u] > crit)
                return centroid(u, crit);
        }
    }
    return v;
}
void treedp(int v){
    ch2[v] = cnt;
    int sum = 0, mx = 0;
    for(int i=0; i<graph[v].size(); i++){
        int u = graph[v][i];
        if(!ch[u] && ch2[u] != cnt){
            treedp(u);
            sum += dp[u];
            //printf("v=%d sum=%d u=%d\n", v, sum, u);
            mx = max(mx, dp[u]);
        }
    }
    dp[v] = max(max(a[v], mx), sum - a[v]);
    XX = max(XX, dp[v]);
    //printf("v=%d sum=%d dp[v]=%d\n", v, sum,dp[v]);
}
void solve(int v){
    cnt++;
    getsize(v);
    cnt++;
    int c=centroid(v, sz[v]/2), i, u, temp = 1;
    ch[c]=1;
    cnt++;
    XX=0;
    treedp(c);
    //temp = max(mx + mx2 - a[c], temp);
    temp = max(XX, temp);
    //printf("c=%d temp[c]=%d\n", c, temp);
   // printf("c=%d\n", c);
    if(a[c]){
        int temp2 = 0;
        for(i=0; i<graph[c].size(); i++){
            u = graph[c][i];
            if(!ch[u]){
                temp2 = max(temp2, dp[u]);
            }
        }
        //printf("temp2=%d\n", temp2);
        temp = max(temp, temp2 + 1);
    }
    ans = max(ans, temp);
    for(i=0; i<graph[c].size(); i++){
        u = graph[c][i];
        if(!ch[u]){
            solve(u);
        }
    }
}
int main(){
    int n, u, b, i;
    scanf("%d", &n);
    for(i=1; i<n; i++){
        scanf("%d %d", &u, &b);
        graph[u].push_back(b);
        graph[b].push_back(u);
    }
    scanf(" %s", s+1);
    int sum =0 ;
    for(i=1; i<=n; i++){
        //printf("s=%d\n", s[i]);
        a[i] = s[i] - '0';
        sum += a[i];
    }
    if(sum>=2)
        ans = 2;
    else
        ans = 1;
    solve(1);
    printf("%d\n", ans);
    return 0;
}

Compilation message

power.cpp: In function 'void getsize(int)':
power.cpp:12:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   12 |     for(int i=0; i<graph[v].size(); i++){
      |                  ~^~~~~~~~~~~~~~~~
power.cpp: In function 'int centroid(int, int)':
power.cpp:23:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   23 |     for(int i=0; i<graph[v].size(); i++){
      |                  ~^~~~~~~~~~~~~~~~
power.cpp: In function 'void treedp(int)':
power.cpp:35:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   35 |     for(int i=0; i<graph[v].size(); i++){
      |                  ~^~~~~~~~~~~~~~~~
power.cpp: In function 'void solve(int)':
power.cpp:63:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   63 |         for(i=0; i<graph[c].size(); i++){
      |                  ~^~~~~~~~~~~~~~~~
power.cpp:73:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |     for(i=0; i<graph[c].size(); i++){
      |              ~^~~~~~~~~~~~~~~~
power.cpp: In function 'int main()':
power.cpp:82:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   82 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
power.cpp:84:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   84 |         scanf("%d %d", &u, &b);
      |         ~~~~~^~~~~~~~~~~~~~~~~
power.cpp:88:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   88 |     scanf(" %s", s+1);
      |     ~~~~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4956 KB Output is correct
2 Correct 3 ms 4956 KB Output is correct
3 Correct 3 ms 4956 KB Output is correct
4 Correct 3 ms 4956 KB Output is correct
5 Correct 2 ms 5156 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 2 ms 4952 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 2 ms 4956 KB Output is correct
11 Correct 2 ms 4956 KB Output is correct
12 Incorrect 3 ms 5064 KB Output isn't correct
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4956 KB Output is correct
2 Correct 3 ms 4956 KB Output is correct
3 Correct 3 ms 4956 KB Output is correct
4 Correct 3 ms 4956 KB Output is correct
5 Correct 2 ms 5156 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 2 ms 4952 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 2 ms 4956 KB Output is correct
11 Correct 2 ms 4956 KB Output is correct
12 Incorrect 3 ms 5064 KB Output isn't correct
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4956 KB Output is correct
2 Correct 3 ms 4956 KB Output is correct
3 Correct 3 ms 4956 KB Output is correct
4 Correct 3 ms 4956 KB Output is correct
5 Correct 2 ms 5156 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 2 ms 4952 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 2 ms 4956 KB Output is correct
11 Correct 2 ms 4956 KB Output is correct
12 Incorrect 3 ms 5064 KB Output isn't correct
13 Halted 0 ms 0 KB -