AtCoder Grand Contest 010

Submission #1575391

Source codeソースコード

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int TAM = 100004;

vector <ll> adj [TAM];
ll val [TAM], pt [TAM];
int n;

void form( int x ){
    int nxt;
    for( int i = 0 ; i < adj[x].size() ; i ++ ){
        nxt = adj[x][i];
        if( nxt == pt[x] ) continue;
        pt[nxt] = x;
        //cout << " parent " << nxt << " = " << pt[nxt] << endl ;
        form(nxt);
    }
}

ll dn, up;
void solve ( ll cap , ll sum ){
    dn = sum - cap;
    up = cap - dn;
}

bool pos = 1;

ll find_up( int x ){
    //cout << " IN " << x << endl ;
    if( adj[x].size() == 1 ) return val[x];
    ll sum = 0, cap = val[x];
    int nxt;
    ll v;
    for( int i = 0 ; i < adj[x].size() ; i ++ ){
        nxt = adj[x][i];
        if( nxt == pt[x] ) continue;
        v = find_up(nxt);
        sum += v
        if( v > cap ) pos = 0;
        //cout << x << " -> " << nxt << " = " << v << endl ;
    }
    solve( cap , sum );
    if( dn + up != cap or dn < 0 or up < 0 ) pos = 0;
    return up;
}

int main(){

    int a, b;
    scanf("%d",&n);
    for( int i = 1 ; i <= n ; i ++ )
        scanf("%lld",&val[i]);
    for( int i = 0 ; i < n-1 ; i ++ ){
        scanf("%d%d",&a,&b);
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    if( n == 2 ){
        if( val[1] == val[2] ) printf("YES\n");
        else printf("NO\n");
        return 0;
    }

    int rt;
    for( int i = 1 ; i <= n ; i ++ ){
        if( adj[i].size() > 1 ){
            pt [i] = i;
            form(i);
            rt = i;
            break;
        }
    }
    if( !find_up(rt) and pos ) printf("YES\n");
    else printf("NO\n");

    return 0;
}

Submission

Task問題 C - Cleaning
User nameユーザ名 vjudge1
Created time投稿日時
Language言語 C++14 (GCC 5.4.1)
Status状態 CE
Score得点 -
Source lengthソースコード長 1650 Byte
File nameファイル名
Exec time実行時間 -
Memory usageメモリ使用量 -

Compiler messageコンパイルメッセージ

./Main.cpp: In function ‘ll find_up(int)’:
./Main.cpp:40:9: error: expected ‘;’ before ‘if’
if( v > cap ) pos = 0;
^
./Main.cpp: In function ‘int main()’:
./Main.cpp:51:19: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
^
./Main.cpp:53:30: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld",&val[i]);
^
./Main.cpp:55:28: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&a,&b);
^

Test case