답안 #580928

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
580928 2022-06-22T06:28:54 Z 조영욱(#8362) 구슬과 끈 (APIO14_beads) C++17
28 / 100
1000 ms 9940 KB
#include <bits/stdc++.h>
using namespace std;

int n;
typedef pair<int,int> P;
vector<P> adj[200001];
long long dp[200001][2];
vector<P> son[200001];
const long long nnn=-1e16;
const long long nn=-1e12-7;
int rr;

void dfs(int v,int prev) {
    dp[v][0]=nnn;
    dp[v][1]=nnn;
    vector<P> son;
    for(int i=0;i<adj[v].size();i++) {
        P nt=adj[v][i];
        if (nt.first!=prev) {
            son.push_back(nt);
            dfs(nt.first,v);
        }
    }
    if (1) {
        long long ret=0;
        long long mx=0;
        for(int i=0;i<son.size();i++) {
            ret+=max(dp[son[i].first][0],dp[son[i].first][1]+son[i].second);
        }
        dp[v][0]=ret+mx;
    }
    if (1) {
        long long ret=0;
        long long mx=nn;
        for(int i=0;i<son.size();i++) {
            ret+=max(dp[son[i].first][0],dp[son[i].first][1]+son[i].second);
            mx=max(mx,dp[son[i].first][0]+son[i].second-max(dp[son[i].first][0],dp[son[i].first][1]+son[i].second));
        }
        dp[v][1]=ret+mx;
    }
}

int main(void) {
    scanf("%d",&n);
    for(int i=1;i<n;i++) {
        int u,v,w;
        scanf("%d %d %d",&u,&v,&w);
        u--;
        v--;
        adj[u].push_back(P(v,w));
        adj[v].push_back(P(u,w));
    }
    long long ret=0;
    for(int r=0;r<n;r++) {
        rr=r;
        dfs(r,-1);
        ret=max(ret,dp[r][0]);
    }
    printf("%lld",ret);
}

Compilation message

beads.cpp: In function 'void dfs(int, int)':
beads.cpp:17:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   17 |     for(int i=0;i<adj[v].size();i++) {
      |                 ~^~~~~~~~~~~~~~
beads.cpp:27:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   27 |         for(int i=0;i<son.size();i++) {
      |                     ~^~~~~~~~~~~
beads.cpp:35:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   35 |         for(int i=0;i<son.size();i++) {
      |                     ~^~~~~~~~~~~
beads.cpp: In function 'int main()':
beads.cpp:44:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   44 |     scanf("%d",&n);
      |     ~~~~~^~~~~~~~~
beads.cpp:47:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 |         scanf("%d %d %d",&u,&v,&w);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 7 ms 9696 KB Output is correct
3 Correct 6 ms 9684 KB Output is correct
4 Correct 7 ms 9684 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 6 ms 9664 KB Output is correct
8 Correct 6 ms 9708 KB Output is correct
9 Correct 5 ms 9704 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
11 Correct 6 ms 9712 KB Output is correct
12 Correct 6 ms 9684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 7 ms 9696 KB Output is correct
3 Correct 6 ms 9684 KB Output is correct
4 Correct 7 ms 9684 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 6 ms 9664 KB Output is correct
8 Correct 6 ms 9708 KB Output is correct
9 Correct 5 ms 9704 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
11 Correct 6 ms 9712 KB Output is correct
12 Correct 6 ms 9684 KB Output is correct
13 Correct 6 ms 9708 KB Output is correct
14 Correct 6 ms 9684 KB Output is correct
15 Correct 6 ms 9708 KB Output is correct
16 Correct 8 ms 9684 KB Output is correct
17 Correct 7 ms 9684 KB Output is correct
18 Correct 7 ms 9684 KB Output is correct
19 Correct 7 ms 9684 KB Output is correct
20 Correct 6 ms 9704 KB Output is correct
21 Correct 6 ms 9608 KB Output is correct
22 Correct 7 ms 9684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 7 ms 9696 KB Output is correct
3 Correct 6 ms 9684 KB Output is correct
4 Correct 7 ms 9684 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 6 ms 9664 KB Output is correct
8 Correct 6 ms 9708 KB Output is correct
9 Correct 5 ms 9704 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
11 Correct 6 ms 9712 KB Output is correct
12 Correct 6 ms 9684 KB Output is correct
13 Correct 6 ms 9708 KB Output is correct
14 Correct 6 ms 9684 KB Output is correct
15 Correct 6 ms 9708 KB Output is correct
16 Correct 8 ms 9684 KB Output is correct
17 Correct 7 ms 9684 KB Output is correct
18 Correct 7 ms 9684 KB Output is correct
19 Correct 7 ms 9684 KB Output is correct
20 Correct 6 ms 9704 KB Output is correct
21 Correct 6 ms 9608 KB Output is correct
22 Correct 7 ms 9684 KB Output is correct
23 Execution timed out 1089 ms 9940 KB Time limit exceeded
24 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 7 ms 9696 KB Output is correct
3 Correct 6 ms 9684 KB Output is correct
4 Correct 7 ms 9684 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 6 ms 9664 KB Output is correct
8 Correct 6 ms 9708 KB Output is correct
9 Correct 5 ms 9704 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
11 Correct 6 ms 9712 KB Output is correct
12 Correct 6 ms 9684 KB Output is correct
13 Correct 6 ms 9708 KB Output is correct
14 Correct 6 ms 9684 KB Output is correct
15 Correct 6 ms 9708 KB Output is correct
16 Correct 8 ms 9684 KB Output is correct
17 Correct 7 ms 9684 KB Output is correct
18 Correct 7 ms 9684 KB Output is correct
19 Correct 7 ms 9684 KB Output is correct
20 Correct 6 ms 9704 KB Output is correct
21 Correct 6 ms 9608 KB Output is correct
22 Correct 7 ms 9684 KB Output is correct
23 Execution timed out 1089 ms 9940 KB Time limit exceeded
24 Halted 0 ms 0 KB -