Submission #1141863

#TimeUsernameProblemLanguageResultExecution timeMemory
1141863KasymKBalanced Tree (info1cup18_balancedtree)C++20
0 / 100
26 ms3652 KiB
#include "bits/stdc++.h"
using namespace std;
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define tr(i, c) for(auto i = c.begin(); i != c.end(); ++i)
#define wr puts("----------------")
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
const int N = 1e5+5;
const int INF = 1e9;
vector<int> adj[N];
int val[N], d[N], d2[N];
bool vis[N];

void solve(){
    int n;
    scanf("%d", &n);
    for(int i = 0; i <= n; ++i)
        adj[i].clear(), val[i]=-1, d[i]=INF, d2[i]=INF, vis[i]=0;
    for(int i = 1; i < n; ++i){
        int a, b;
        scanf("%d%d", &a, &b);
        adj[a].pb(b);
        adj[b].pb(a);
    }
    bool ok=1;
    for(int i = 1; i <= n; ++i)
        scanf("%d", val+i), ok&=(val[i]!=-1);
    assert(ok);
    if(count(val+1, val+n+1, 0)==0 or count(val+1, val+n+1, 1)==0){
        puts("1");
        for(int i = 1; i <= n; ++i)
            printf("%d ", val[i]);
        puts("");
        return;
    }
    if(count(val+1, val+n+1, 0)==1 or count(val+1, val+n+1, 1)==1){
        puts("-1");
        return;
    }
    queue<int> q;
    for(int i = 1; i <= n; ++i)
        if(val[i]==0)
            q.push(i);
    while(!q.empty()){
        int x=q.front();
        q.pop();
        tr(it, adj[x])
        if(!vis[*it])
            d[*it]=(d[x]!=INF?d[x]+1:1), vis[*it]=1, q.push(*it);
    }
    for(int i = 1; i <= n; ++i){
        vis[i]=0;
        if(val[i]==1)
            q.push(i);
    }
    while(!q.empty()){
        int x=q.front();
        q.pop();
        tr(it, adj[x])
        if(!vis[*it])
            d2[*it]=(d2[x]!=INF?d2[x]+1:1), vis[*it]=1, q.push(*it);
    }
    // for(int i = 1; i <= n; ++i)
    //     if(val[i]==0)
    //         printf("%d ", d[i]);
    //     else
    //         printf("%d ", d2[i]);
    // puts("");
    // wr;
    int D=-INF;
    for(int i = 1; i <= n; ++i)
        if(val[i]==0)
            umax(D, d[i]);
        else
            umax(D, d2[i]);
    printf("%d\n", D);
    for(int i = 1; i <= n; ++i)
        printf("%d ", val[i]);
    puts("");
}

int main(){
    int tt;
    scanf("%d", &tt);
    while(tt--)
        solve();
    return 0;
}

Compilation message (stderr)

balancedtree.cpp: In function 'void solve()':
balancedtree.cpp:23:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   23 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
balancedtree.cpp:28:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   28 |         scanf("%d%d", &a, &b);
      |         ~~~~~^~~~~~~~~~~~~~~~
balancedtree.cpp:34:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   34 |         scanf("%d", val+i), ok&=(val[i]!=-1);
      |         ~~~~~^~~~~~~~~~~~~
balancedtree.cpp: In function 'int main()':
balancedtree.cpp:91:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   91 |     scanf("%d", &tt);
      |     ~~~~~^~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...